cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
228
Views
0
Helpful
1
Replies

Ansible Builtin Ping

Netmart
Level 3
Level 3

Hello,

I was running the following playbook; the purpose is to run ping checks of virtual IPs on a Cisco 7K switch.

So far so good. However, when I wanted to expand the playbook by also counting the successful ping.

Running the runbook again I receive the following error:

 ansible-builtin-ping-count-pong2.yml

---

- name: Ansible Ping Check on Cisco IOS Virtual IPs on Cisco 7k

  hosts: 7KcoreVLANSios

  vars_files:

         - /home/cisco/Ansible/vault_password5.yml  




  gather_facts: false # No need to gather facts for a simple ping

  tasks:

    - name: Ping all hosts

      ansible.builtin.ping:

      register: ping_results


    - name: Count successful pings

      ansible.builtin.set_fact:

        successful_pings_count: "{{ ping_results.results | selectattr('ping', 'equalto', 'pong') | list | length }}"


    - name: Display the count of successful pings

      ansible.builtin.debug:

        msg: "Successfully pinged {{ successful_pings_count }} hosts."

---------------------

inventory

[7KcoreVLANSios]

10.0.0.1

10.0.0.2

10.0.0.3

[7KcoreVLANSios:vars]

ansible_network_os=ios

ansible_connection=network_cli

-------------------

Error:

fatal: [10.254.11.1]: FAILED! => {"msg": "The task includes an option with an undefined variable.

The error was: 'dict object' has no attribute 'results'. 'dict object' has no attribute 'results'\n\n

The error appears to be in '/home/cisco/Ansible/roles/ios-xe-role/tasks/ios-xe-checks/ansible-builtin-ping-count-pong2.yml': line 15, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n    - name: Count successful pings\n      ^ here\n"}

 

Thank you for any advise.

 

 

 

1 Reply 1

@Netmart you need to update your playbook to iterate through the hosts and count the successful pings.

Hope this helps.

Please mark this as helpful or solution accepted to help others
Connect with me https://bigevilbeard.github.io