Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure Ansible to use my local SSH configuration?

Tags:

ssh

ansible

I'm testing out Ansible and am already stuck on a fairly simple thing. I configured my /etc/ansible/hosts to contain the IP of my server:

[web]
1.2.3.4

Now, connecting to it with ansible all -vvvv -m ping fails since my ~/.ssh/config for the specified server uses a custom port, a key file not in the default location, etc. How do I tell Ansible to reuse my SSH configuration for the connection?

like image 373
mart1n Avatar asked Dec 15 '22 18:12

mart1n


2 Answers

It's a little esoteric, so it's understandable you missed it. This is from the Ansible Inventory page:

If you have hosts that run on non-standard SSH ports you can put the port number after the hostname with a colon. Ports listed in your SSH config file won’t be used with the paramiko connection but will be used with the openssh connection.

To make things explicit, it is suggested that you set them if things are not running on the default port

So, in your case:

[web]
1.2.3.4:9000

(Using 9000 as your alt port, of course)

Ansible uses paramiko on systems with a dated version of ssh, such as CentOS/RHEL.

like image 197
tedder42 Avatar answered Dec 21 '22 17:12

tedder42


What tedder42 said plus there are other slightly more advanced ways of defining your ssh config on a per host basis.

[web]
1.2.3.4 ansible_ssh_port=9000

This only makes sense if you're also using the other ansible_ssh special variables like ansible_ssh_user and ansible_ssh_host.

ansible_ssh_host is helpful if you want to be able to refer to a server by a name of your choosing instead of its IP.

[web]
apache ansible_ssh_host=1.2.3.4 ansible_ssh_port=9000

If you end up with multiple hosts with the same alternative ssh port you can makes use of Ansible's group variable function.

[web]
1.2.3.4 
5.6.7.8
9.10.11.12

[web:vars]
ansible_ssh_port=9000

Now Ansible will use port 9000 on all three of the hosts In the web group.

Understanding how to organize your inventory data goes a long way to your success with Ansible.

like image 32
tima Avatar answered Dec 21 '22 16:12

tima