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:
I need to change the ssh ports on the hosts I manage and I want to use Ansible to do it. Essentially, Ansible uses the following logic to manage it's SSH connections:
if self.port is not None:
ssh -p {{ self.port }} ...
else:
ssh ...
where "self.port" is the port specification from the host inventory, or an override via the "-e" parameter, or an explicit declaration of the variables "ansible_port" and/or "ansible_ssh_port". The recommended solution to changing ports is to employ the "wait_for" and "when" modules in "pre_tasks", but there are many inadequacies to this approach, particularly when many hosts are involved and especially when you want to use different ports on different hosts.
I cloned and patched the ssh plugin (versions 1 and 2) to change the logic as follows:
if self.port is not None and self.port is OPEN:
ssh -p {{ self.port }} ...
else:
ssh ...
The patch, by itself, makes no changes on the target nodes but allows connections to succeed even if the ports on the nodes haven't changed yet. With the patch, it is now very easy to write roles/tasks to change ssh ports to whatever is in the host inventory.
If you're interested, you can find the patch and samples of how use it at https://github.com/crlb/ansible; the README.md contains additional information.