Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterate over a array in with_items loop

Tags:

ansible

Based on this question

Ansible recursive checks in playbooks

I have another one.

We need to go through this structure

Zone spec https://gist.github.com/git001/9230f041aaa34d22ec82eb17d444550c

Now I can adress the hostnames via the array index but can I also iterate over the array "hosts"?

playbook

--
- hosts: all
  gather_facts: no

  vars_files:
    - "../doc/application-zone-spec.yml"

  roles:
    - { role: ingress_add, customers: "{{ application_zone_spec }}" }

role

 - name: Print ingress hostnames
   debug: msg="{{ item.hosts.0.hostname }} {{ item.hosts.1.hostname }}"
   with_items: "{{ customers.ingress }}"

We use.

ansible-playbook --version
ansible-playbook 2.1.0.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = Default w/o overrides
like image 289
Aleksandar Avatar asked Oct 28 '25 08:10

Aleksandar


1 Answers

Use with_subelements:

 - name: Print ingress hostnames
   debug: msg="{{ item.0.type }} {{ item.1.hostname }}"
   with_subelements:
     - "{{ customers.ingress }}"
     - "hosts"

There is quite a bit of examples for different loops in the Loops section of the documentation.

like image 75
Konstantin Suvorov Avatar answered Oct 30 '25 03:10

Konstantin Suvorov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!