#command-line #ssh #ansible #development #utility

app ssh-to-ansible

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

2 unstable releases

0.3.0 Nov 12, 2023
0.2.0 Nov 11, 2023

#2 in #vagrant

Download history 33/week @ 2023-11-06 10/week @ 2023-11-13 7/week @ 2023-11-20 6/week @ 2023-11-27 6/week @ 2023-12-04 2/week @ 2023-12-11 6/week @ 2023-12-25 4/week @ 2024-01-29 1/week @ 2024-02-05 12/week @ 2024-02-12 49/week @ 2024-02-19

66 downloads per month

Apache-2.0

42KB
845 lines

crates.io version build status downloads Coverage Status

ssh-to-ansible

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

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

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...
          More output per occurrence
  -q, --quiet...
          Less output per occurrence
  -e, --environment <ENVIRONMENT>
          Name of the environment to generate [default: local]
  -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

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

~5.5MB
~108K SLoC