1
votes

I have an inventory in which I define some vars like the server ip:

all:
  children:
    my_servers:
      children:
        data:
          hosts:
            data01.mydomain:
              ansible_host: 10.0.0.1
              es_name: "data01"
            data02.mydomain:
              ansible_host: 10.0.0.2
              es_name: "data02"

I know that to access the hostnames I can use something like "{{ groups['data'] }}" and this will iterate over the hostnames.

What I would like to do is to iterate over the var "ansible_host" in each hostname (to open rules on a firewall). I can have the value of one with "{{ hostvars['data01']['ansible_host'] }}" but I can't find the way to iterate over all hosts on the inventory.

1

1 Answers

1
votes

Here you go. Remember to run the play with the -i hosts

play.yml

---
- name: Play
  hosts: localhost
  tasks:
    - name: iterate
      debug:
         msg: "{{ item }}:{{ hostvars[item].ansible_ssh_host }}"
      with_items:
        - "{{ groups['all'] }}"

Run playbook like below

ansible-playbook -i hosts play.yml