, , , ,

Notes Ansible - test ports de connexion SSH

Voir aussi :

Context :

Le port par défaut pour SSH est le 22.

Nous changeons habituellement ce port en 2222.

Nous écrivons un playbook capable d’adresser les machines avec SSH configuré avec le port 2222 ainsi que celles configurées avec le port 22.

wrapper_ssh_ports.yml

---

- name: wrapper_both_ssh_ports
  hosts: all
  gather_facts: false
  vars:
    port_test_1: 2222
    port_test_2: 22

  tasks:
  
    - name: set_fact port_test_1
      set_fact:
        ansible_port: "{{ port_test_1 }}"

    - name: ping 1
      ignore_unreachable: true
      ping:
      register: ping_1

    - name: set_fact port_test_2
      when: not ( ping_1.ping is defined and ping_1.ping == 'pong' )
      set_fact:
        ansible_port: "{{ port_test_2 }}" 

    - name: ping 2
      when: not ( ping_1.ping is defined and ping_1.ping == 'pong' )
      ignore_unreachable: true
      ping:
      register: ping_2

    - name: fail when all ports NOK
      assert:
        that:
          - ( ping_1.ping is defined and ping_1.ping == 'pong' ) or ( ping_2.ping is defined and ping_2.ping == 'pong' )

        msg: "Fail unreachable target"
 

- import_playbook: play2.yml

play2.yml

- name: playbook to launch always - SSH port 2222 and 22
  hosts: all
  gather_facts: false

  tasks:

    - name: commande uptime
      command: uptime
      changed_when: false
      register: uptime

    - name: echo uptime
      debug:
        var: uptime.stdout