Ping Role for clients to ping all IP from HOST_VAR files?

Dear Community,

I want to write a simple Ping Role, which takes IP Addresses from all YAML files in HOST_VARS and connected to Linux devices and ping all the collected IP

Example:

HOST_VARS:
-server1.yml (contains an IP = 1.1.1.1
-server2.yml (contains an IP = 2.2.2.

The role should connect to a Linux Client and ping all IP (in this example 1.1.1.1 and 2.2.2.2)

It should be simple but I’m struggling to solve it:

Here my try:
https://github.com/fdervisi/cisco-config-generator/blob/master/list-test.yml

  • name: “Ping all Linux VMs”
    hosts: servers

vars:
ip_test:

  • 8.8.8.8
  • 10.116.207.5

tasks:

  • name: Search for all Server IP Address in HOST_VAR folder and create a list
    debug:
    msg: “{{ lookup(‘template’, ‘templates/test.j2’) }}”
    register: ip

  • debug: var=ip

  • name: Check all port numbers are accessible from current host
    wait_for:
    host: “{{ item }}”
    port: 80
    state: started # Port should be open
    delay: 0 # No wait before first check (sec)
    timeout: 3 # Stop checking after timeout (sec)
    ignore_errors: yes
    #with_items: “{{ hostvars[‘{{ hostname }}’][‘vm_interfaces’][1][‘ip’] }}”
    with_items: “{{ ip_test }}”

Can somebody help me to solve this “simple” task?

This should be the solution but it does not work, because I dont gather facts, I want to scan thru my inventroy:

  • name: “Ping all Linux VMs”
    hosts: servers
    gather_facts: no

tasks:

  • debug:
    msg: “{{ hostvars[‘{{ item }}’].ansible_host }}”
    with_items: “{{ groups[‘servers’] }}”
    delegate_to: localhost

What I want to access it this variable within host_vars/linux1.yml:

hostname: “linux1”
ansible_host: 192.168.99.101
server_template: CentOS7_Template
host_groups:

  • servers

Does anybody have an idea how I can solve it?

Thanks
Cheers
Fatos