Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible change ssh port in playbook

Tags:

ansible

Here is the inventory file

--- [de-servers] 192.26.32.32  [uk-servers] 172.21.1.23 172.32.2.11 

and my playbook is look like this:

- name: Install de-servers configurations   hosts: de-servers     roles:     - de-server-setup  - name: Install uk-servers configurations   hosts: uk-servers     roles:     - uk-server-setup  - name: Do some other job on de-servers (cannot be done until uk-servers is installed)   hosts: de-servers   roles:     - de-servers-rest-of-jobs 

In role de-servers-setup role the ssh port is changed from 22 to 8888, so when the last task is called it fails because it cannot connect to host through 22 port. How to overcome this ssh port change?

like image 929
Nasr Avatar asked Dec 17 '15 11:12

Nasr


2 Answers

In the role de-server-setup add a task to change the ansible_port host variable.

- name: Change ssh port to 8888   set_fact:     ansible_port: 8888 
like image 147
chrism Avatar answered Sep 22 '22 01:09

chrism


The only thing I can think of that might work would be to create ssh aliases for your hosts. In your .ssh/config:

Host de.1.before   HostName 192.26.32.32   Port 22  Host de.1.after   HostName 192.26.32.32   Port 8888 

Then use these aliases in your Ansible inventory:

[de-servers-before] de.1.before  [de-servers-after] de.1.after 

And the defined groups then respectively in your plays:

- name: Install de-servers configurations   hosts: de-servers-before   roles:     - de-server-setup  - name: Install uk-servers configurations   hosts: uk-servers     roles:     - uk-server-setup  - name: Do some other job on de-servers (cannot be done until uk-servers is installed)   hosts: de-servers-after   roles:     - de-servers-rest-of-jobs 
like image 35
udondan Avatar answered Sep 22 '22 01:09

udondan