Ansible 101 : SSH test, play ping pong 4 (Tools, 2_ssh_connection.yml )

2_ssh_connection.yml

  • Here, I am using ansible_user and ansible_password hardcoded under inventory file.
---
- name:
  hosts: routers
  connection: network_cli
  gather_facts: false

  tasks:
   # Option 1
   # This will SSH into each device and run ping to itself
#  - name: Test SSH Login to each device
#    net_ping:
#      dest: "{{ hostvars[inventory_hostname].ansible_host }}"
##      dest: "{{ ansible_host }}"
#    ignore_errors: true

  # Option 2
  # This will test SSH connection as it will login and run a simple command on IOS/IOS-XE nodes
  # Run show clock
  - name: Run show clock (tests SSH connection and privilege)
    ios_command:
      commands:
        - show clock
    register: clock01

  - debug: var=clock01.stdout

Result:

[jdoe@centos8 csr1000v_dev]$ ansible-playbook 2_ssh_connection.yml

PLAY [routers] **********************************************************************************************************************************************************************

TASK [Run show clock (tests SSH connection and privilege)] **************************************************************************************************************************
ok: [cisco871]
fatal: [csr1000v-1]: FAILED! => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python"}, "changed": false, "msg": "[Errno None] Unable to connect to port 22 on 192.168.30.11"}

TASK [debug] ************************************************************************************************************************************************************************
ok: [cisco871] => {
    "clock01.stdout": [
        "11:43:44.160 UTC Wed Aug 18 2021"
    ]
}

PLAY RECAP **************************************************************************************************************************************************************************
cisco871                   : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
csr1000v-1                 : ok=0    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0

Collect user ID and password from the user

  • Here I have removed the ansible_user and ansible_password from inventory file. Getting the information from the user.

2_ssh_connection_v0.1.yml


---
- name:
  hosts: routers
  connection: network_cli
  gather_facts: false

  vars_prompt:
    - name: "ansible_user"
      prompt: "Network Admin username"
      private: no
    - name: "ansible_password"
      prompt: "Network Admin Password"
      private: yes

  tasks:
   # Option 1
   # This will SSH into each device and run ping to itself
#  - name: Test SSH Login to each device
#    net_ping:
#      dest: "{{ hostvars[inventory_hostname].ansible_host }}"
##      dest: "{{ ansible_host }}"
#    ignore_errors: true

  # Option 2
  # This will test SSH connection as it will login and run a simple command on IOS/IOS-XE nodes
  # Run show clock
  - name: Run show clock (tests SSH connection and privilege)
    ios_command:
      commands:
        - show clock
    register: clock01

  - debug: var=clock01.stdout

Run the new playbook

[jdoe@centos8 csr1000v_dev]$ ansible-playbook 2_ssh_connection_v0.1.yml
Network Admin username: jdoe
Network Admin Password: *********

Result:

  • Added another inactive node csr1000v0-3.
[jdoe@centos8 csr1000v_dev]$ ansible-playbook 2_ssh_connection_v0.1.yml
Network Admin username: jdoe
Network Admin Password:

PLAY [routers] **********************************************************************************************************************************************************************

TASK [Run show clock (tests SSH connection and privilege)] **************************************************************************************************************************
ok: [cisco871]
fatal: [csr1000v-1]: FAILED! => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python"}, "changed": false, "msg": "[Errno None] Unable to connect to port 22 on 192.168.30.11"}
fatal: [csr1000v-3]: FAILED! => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python"}, "changed": false, "msg": "[Errno None] Unable to connect to port 22 on 192.168.30.33"}

TASK [debug] ************************************************************************************************************************************************************************
ok: [cisco871] => {
    "clock01.stdout": [
        "11:49:24.329 UTC Wed Aug 18 2021"
    ]
}

PLAY RECAP **************************************************************************************************************************************************************************
cisco871                   : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
csr1000v-1                 : ok=0    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0
csr1000v-3                 : ok=0    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0

Leave a comment