#ssh #ansible #yaml #config-file #utility #development #yaml-config

app ssh-to-ansible

A tool to convert a SSH configuration to an Ansible YAML inventory

3 releases (breaking)

0.4.0 Sep 15, 2024
0.3.0 Nov 12, 2023
0.2.0 Nov 11, 2023

#1514 in Parser implementations

Download history 184/week @ 2024-09-12 32/week @ 2024-09-19 39/week @ 2024-09-26 30/week @ 2024-10-03 1/week @ 2024-10-10

96 downloads per month

Apache-2.0

53KB
1K SLoC

crates.io version build status downloads Coverage Status

ssh-to-ansible

A tool to convert a SSH configuration to an Ansible YAML inventory.

Installation

brew install marccarre/homebrew-ssh-to-ansible/s2a

Or

brew tap marccarre/homebrew-ssh-to-ansible
brew install s2a

Or download from the release page and install manually at your convenience.

Usage

Provide any SSH configuration as an input to s2a, either via stdin or as an input file, optionally define the name of the environment (-e/--environment) for the Ansible inventory, and optionally provide an output YAML file.

s2a works with any well-formed SSH configuration, e.g.:

  • cat ~/.ssh/config | s2a
  • vagrant ssh-config | s2a

Examples

Default options

By default, s2a defaults the environment to be local, reads from stdin and writes to stdout:

$ cat <<EOF | s2a
Host default
  HostName 127.0.0.1
  User vagrant
  Port 50022
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile /Users/me/.vagrant/machines/default/qemu/private_key
  IdentitiesOnly yes
  LogLevel FATAL
  PubkeyAcceptedKeyTypes +ssh-rsa
  HostKeyAlgorithms +ssh-rsa
EOF

local:
  hosts:
    default:
      ansible_host: 127.0.0.1
      ansible_port: 50022
      ansible_user: vagrant
      ansible_ssh_private_key_file: /Users/me/.vagrant/machines/default/qemu/private_key
      ansible_ssh_extra_args: -o HostKeyAlgorithms=+ssh-rsa -o IdentitiesOnly=yes -o LogLevel=FATAL -o PasswordAuthentication=no -o PubkeyAcceptedKeyTypes=+ssh-rsa -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null

Configure the Ansible inventory's environment

$ cat <<EOF | s2a -e dev
Host default
  HostName 127.0.0.1
  User vagrant
  Port 50022
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile /Users/me/.vagrant/machines/default/qemu/private_key
  IdentitiesOnly yes
  LogLevel FATAL
  PubkeyAcceptedKeyTypes +ssh-rsa
  HostKeyAlgorithms +ssh-rsa
EOF

dev:
  hosts:
    default:
      ansible_host: 127.0.0.1
      ansible_port: 50022
      ansible_user: vagrant
      ansible_ssh_private_key_file: /Users/me/.vagrant/machines/default/qemu/private_key
      ansible_ssh_extra_args: -o HostKeyAlgorithms=+ssh-rsa -o IdentitiesOnly=yes -o LogLevel=FATAL -o PasswordAuthentication=no -o PubkeyAcceptedKeyTypes=+ssh-rsa -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null

Configure vars in the Ansible inventory

Provide colon-separated key:value pairs using the --var CLI option:

$ cat <<EOF | ./s2a --var become:true --var http_port:"'8080'" --var num_workers:4 --var user:root
Host default
  HostName 127.0.0.1
  User vagrant
  Port 50022
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile /Users/me/.vagrant/machines/default/qemu/private_key
  IdentitiesOnly yes
  LogLevel FATAL
  PubkeyAcceptedKeyTypes +ssh-rsa
  HostKeyAlgorithms +ssh-rsa
EOF

local:
  hosts:
    default:
      ansible_host: 127.0.0.1
      ansible_port: 50022
      ansible_user: vagrant
      ansible_ssh_private_key_file: /Users/me/.vagrant/machines/default/qemu/private_key
      ansible_ssh_extra_args: -o HostKeyAlgorithms=+ssh-rsa -o IdentitiesOnly=yes -o LogLevel=FATAL -o PasswordAuthentication=no -o PubkeyAcceptedKeyTypes=+ssh-rsa -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null
  vars:
    become: true
    http_port: '8080'
    num_workers: 4
    user: root

Read from input file instead of stdin

$ cat <<EOF > ssh_config
Host default
  HostName 127.0.0.1
  User vagrant
  Port 50022
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile /Users/me/.vagrant/machines/default/qemu/private_key
  IdentitiesOnly yes
  LogLevel FATAL
  PubkeyAcceptedKeyTypes +ssh-rsa
  HostKeyAlgorithms +ssh-rsa
EOF

$ s2a -i ssh_config

local:
  hosts:
    default:
      ansible_host: 127.0.0.1
      ansible_port: 50022
      ansible_user: vagrant
      ansible_ssh_private_key_file: /Users/me/.vagrant/machines/default/qemu/private_key
      ansible_ssh_extra_args: -o HostKeyAlgorithms=+ssh-rsa -o IdentitiesOnly=yes -o LogLevel=FATAL -o PasswordAuthentication=no -o PubkeyAcceptedKeyTypes=+ssh-rsa -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null

Write to output file instead of stdout

$ cat <<EOF | s2a -o local.yaml
Host default
  HostName 127.0.0.1
  User vagrant
  Port 50022
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile /Users/me/.vagrant/machines/default/qemu/private_key
  IdentitiesOnly yes
  LogLevel FATAL
  PubkeyAcceptedKeyTypes +ssh-rsa
  HostKeyAlgorithms +ssh-rsa
EOF

$ cat local.yaml
local:
  hosts:
    default:
      ansible_host: 127.0.0.1
      ansible_port: 50022
      ansible_user: vagrant
      ansible_ssh_private_key_file: /Users/me/.vagrant/machines/default/qemu/private_key
      ansible_ssh_extra_args: -o HostKeyAlgorithms=+ssh-rsa -o IdentitiesOnly=yes -o LogLevel=FATAL -o PasswordAuthentication=no -o PubkeyAcceptedKeyTypes=+ssh-rsa -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null

Help

$ s2a --help
A tool to convert a SSH configuration to an Ansible YAML inventory.

Usage: s2a [OPTIONS]

Options:
  -v, --verbose...
          Increase logging verbosity
  -q, --quiet...
          Decrease logging verbosity
  -e, --environment <ENVIRONMENT>
          Name of the environment to generate [default: local]
      --var <VARS>
          Ansible variables to add to the hosts, as colon-separated name:value pair, e.g., --var new_ssh_port:22222 --var swap_size:3G
  -i, --input-filepath <INPUT_FILEPATH>
          Path of the input SSH configuration to parse [default: stdin]
  -o, --output-filepath <OUTPUT_FILEPATH>
          Path of the output Ansible inventory file to generate [default: stdout]
  -h, --help
          Print help
  -V, --version
          Print version

Development

Setup

brew install just
just setup

Build

cargo build

Lint

just lint

Test

Unit tests

cargo test

Coverage

just cover

Release

export VERSION="X.Y.Z"  # N.B.: no "v" prefix!
git tag -a "${VERSION}" -m "${VERSION}"
git push origin --tags
cargo login
cargo publish --dry-run
cargo publish

Then update the Homebrew Tap at: https://github.com/marccarre/homebrew-ssh-to-ansible

N.B.: in case of release job failure, and a re-release, the tag can be deleted this way (warning: bad practice to delete tags):

git tag -d "${VERSION}"
git push origin --delete "${VERSION}"

Dependencies

~6.5–9MB
~165K SLoC