根据环境配置SSH凭据


10

我试图弄清楚如何使用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

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


您的环境之间有什么变化?只是密钥文件,还是remote_user / sudo_user?
tedder42

@ tedder42 SSH私钥和remote_user。
user369450 2015年

为什么不在.ssh / config中覆盖呢?
udondan 2015年

@udondan是否可以为其中的多个主机指定一次密钥.ssh/config
user369450 2015年

是的,你可以这样做。您可以使用明确的主机名或模式创建多个组。让我在答案中举一个例子。
udondan 2015年

Answers:


16

似乎我的第一个答案并不完全正确。虽然当然可以按照.ssh/config下面所述的方式解决它,但使用Ansibles 行为清单参数似乎也是可能的。

您应该(根据文档)能够按主机或按组定义清单中的密钥文件和用户。

每组的定义:

[some_hosts]
host1.foo
host2.foo

[some_hosts:vars]
ansible_ssh_user=ubuntu
ansible_ssh_private_key_file=/home/caleb/.ssh/staging_key.pem

每个主机的定义:

[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

但是您可以在您的主机中定义多个主机组,.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

同样,您可以定义默认值,以后再使用更详细的设置覆盖它。

Host *
  user defaut_username

Host somehost
  user special_username
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.