为每个环境配置 SSH 凭据

All*_*ial 10 ansible

我正在尝试弄清楚如何使用 Ansible 为生产和暂存环境单独配置 SSH 凭据。我知道您可以通过将-ior--inventory-file参数传递给ansible-playbook命令,使用不同的清单文件分别配置服务器 IP 地址和主机名。但是,我看不到ansible.cfg. 目前,凭据存在于/etc/ansible/ansible.cfg

[defaults]
private_key_file=/home/caleb/.ssh/staging_key.pem
remote_user=ubuntu
sudo_user=root
gathering=explicit
Run Code Online (Sandbox Code Playgroud)

如何配置多个 SSH 凭据,一个用于生产,一个用于暂存?

udo*_*dan 16

似乎我的第一个答案并不完全正确。虽然当然可以按照.ssh/config下面描述的方式解决它,但似乎也可以使用 Ansibles Behavioral Inventory Parameters

您应该(根据文档)能够在您的清单中定义密钥文件和用户,无论是每个主机还是每个组。

每组定义:

[some_hosts]
host1.foo
host2.foo

[some_hosts:vars]
ansible_ssh_user=ubuntu
ansible_ssh_private_key_file=/home/caleb/.ssh/staging_key.pem
Run Code Online (Sandbox Code Playgroud)

每个主机的定义:

[some_hosts]
host1.foo     ansible_ssh_user=ubuntu          ansible_ssh_private_key_file=/home/caleb/.ssh/staging_key.pem
host2.foo     ansible_ssh_user=another_user    ansible_ssh_private_key_file=/home/caleb/.ssh/production_key.pem
Run Code Online (Sandbox Code Playgroud)

但是您可以定义多个已经在您的主机组中,.ssh/config并且每个组可以有关于密钥和用户的单独设置。

这是一个快速示例

#Example with a wildcard
Host *.foo.com
  user ubuntu
  IdentityFile /home/caleb/.ssh/staging_key.pem

#Example with multiple hostnames
Host hostname.one hostname.two hostname.three
  user other_user
  IdentityFile /home/caleb/.ssh/production_key.pem
Run Code Online (Sandbox Code Playgroud)

您也可以定义一个默认值,然后用更详细的设置覆盖它。

Host *
  user defaut_username

Host somehost
  user special_username
Run Code Online (Sandbox Code Playgroud)