Ansible Using Custom ssh config File

2019-04-18 17:36发布

I have a custom SSH config file that I typically use as follows

ssh -F ~/.ssh/client_1_config amazon-server-01

Is it possible to assign Ansible to use this config for certain groups? It already has the keys and ports and users all set up. I have this sort of config for multiple clients, and would like to keep the config separate if possible.

标签: ansible
3条回答
聊天终结者
2楼-- · 2019-04-18 17:36

Not fully possible. You can set ssh arguments in the ansible.cfg:

[ssh_connection]
ssh_args = -F ~/.ssh/client_1_config amazon-server-01

Unfortunately it is not possible to define this per group, inventory or anything else specific.

查看更多
对你真心纯属浪费
3楼-- · 2019-04-18 17:46

I believe you can achieve what you want like this in your inventory:

[group1]
server1

[group2]
server2

[group1:vars]
ansible_ssh_user=vagrant
ansible_ssh_common_args='-F ssh1.cfg'

[group2:vars]
ansible_ssh_user=vagrant
ansible_ssh_common_args='-F ssh2.cfg'

You can then be as creative as you want and construct SSH config files such as this:

$ cat ssh1.cfg
Host server1
     HostName 192.168.1.1
     User someuser
     Port 22
     IdentityFile /path/to/id_rsa

References

查看更多
手持菜刀,她持情操
4楼-- · 2019-04-18 17:51

With Ansible 2, you can set a ProxyCommand in the ansible_ssh_common_args inventory variable. Any arguments specified in this variable are added to the sftp/scp/ssh command line when connecting to the relevant host(s). Consider the following inventory group:

[gatewayed]
foo ansible_host=192.0.2.1
bar ansible_host=192.0.2.2

You can create group_vars/gatewayed.yml with the following contents:

ansible_ssh_common_args: '-o ProxyCommand="ssh -W %h:%p -q user@gateway.example.com"'

and do the trick...

You can find further information in: http://docs.ansible.com/ansible/faq.html

查看更多
登录 后发表回答