ssh-to-ansible

Crates.iossh-to-ansible
lib.rsssh-to-ansible
version0.4.0
sourcesrc
created_at2023-11-11 07:58:55.267485
updated_at2024-09-15 08:48:26.897384
descriptionA tool to convert a SSH configuration to an Ansible YAML inventory.
homepagehttps://github.com/marccarre/ssh-to-ansible
repositoryhttps://github.com/marccarre/ssh-to-ansible
max_upload_size
id1031917
size90,915
Marc Carré (marccarre)

documentation

README

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}"
Commit count: 70

cargo fmt