09-09-2025 04:22 PM - edited 09-09-2025 04:23 PM
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.
09-10-2025 02:16 AM
@Netmart you need to update your playbook to iterate through the hosts and count the successful pings.
Hope this helps.
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide