Select ports to connect ssh to Ansible
-
There's, for example, a host:
[api] 10.0.0.1 ansible_connection=ssh ansible_ssh_user=root ansible_ssh_port=22
Here we are pointing
22
Ssh port. But some servers a different port, say,220
♪Maybe someday you'll have an extra port for the ssh connection in case you couldn't join.
22
? There's a list for Ansible to take turns.
-
Partly used an example from Nick Volynkin.
First, the playbook checks the ports, selects what is available and installed
ansible_ssh_port
after which Ansible will be connected to the car on this port. One minus - usewait_for
♪ I mean, in this case, we wait 15 seconds (that is, check 3 ports) while the ports check.- name: just test hosts: server gather_facts: false vars: list_of_ssh_ports: [22, 220, 234] tasks: - name: test ssh on port sudo: no local_action: wait_for port={{item}} timeout=5 host={{inventory_hostname}} register: ssh_checks with_items: "{{list_of_ssh_ports}}" ignore_errors: true - debug: msg = "{{item}}" with_items: "{{ssh_checks.results}}" - name: set available ansible_ssh_port sudo: no set_fact: ansible_ssh_port={{item.item}} when: ssh_checks is defined and {{item.elapsed}} < 5 with_items: "{{ssh_checks.results}}" - hosts: server roles: - остальные таски