#ssh #ansible #config-file #utility #input-file #development #command-line-tool

app ssh-to-ansible

将SSH配置转换为Ansible YAML清单的工具

2个不稳定版本

0.3.0 2023年11月12日
0.2.0 2023年11月11日

#2178 in 解析实现

Apache-2.0

42KB
845

crates.io version build status downloads Coverage Status

ssh-to-ansible

将SSH配置转换为Ansible YAML清单的工具。

用法

将任何SSH配置作为输入提供给s2a,无论是通过stdin还是作为输入文件,可选地定义Ansible清单的环境名称(-e/--environment),并可选地提供输出YAML文件。

s2a适用于任何良好形成的SSH配置,例如。

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

示例

默认选项

默认情况下,s2a将环境默认为local,从stdin读取并写入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

配置Ansible清单的环境

$ 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

从输入文件而不是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

写入输出文件而不是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

帮助

$ 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

开发

设置

brew install just
just setup

构建

cargo build

代码风格检查

just lint

测试

单元测试

cargo test

覆盖率

just cover

发布

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

注意:如果在发布作业失败后重新发布,可以删除标签(警告:删除标签是不良实践)

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

依赖项

~5.5MB
~106K SLoC