Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible with_subelements

Tags:

ansible

I am having a hard time understanding the logic of ansible with_subelements syntax, what exactly does with_subelements do? i took a look at ansible documentation on with_subelements here https://docs.ansible.com/ansible/latest/user_guide/playbooks_loops.html#with-subelements and was not very helpful. I also saw a playbook with with_subelements example on a blog

--- - hosts: cent   vars:     users:      - name: jagadish        comments:          - 'Jagadish is Good'       - name: srini        comments:          - 'Srini is Bad'     tasks:    - name: User Creation      shell: useradd -c "{{ item.1 }}" "{{ item.0.name }}"      with_subelements:          - users          - comments 

what do item.1 and item.0 refer to?

like image 508
ahmedjaad Avatar asked Jan 28 '17 10:01

ahmedjaad


People also ask

Does Ansible do until loop?

To use this loop in task you essentially need to add 3 arguments to your task arguments: until - condition that must be met for loop to stop. That is Ansible will continue executing the task until expression used here evaluates to true. retry - specifies how many times we want to run the task before Ansible gives up.

What is With_item in Ansible?

What is Ansible with_items? The Ansible with_items is a handy plugin to perform loop operations in a playbook. The plugin accepts items and then passes them to the calling module. For example, you can pass a list of packages to install and then give each item in the list to the install task.

How do you do nested loops in Ansible?

Nested loops in many ways are similar in nature to a set of arrays that would be iterated over using the with_nested operator. Nested loops provide us with a succinct way of iterating over multiple lists within a single task. Now we can create nested loops using with_nested.

Can there be n number of plays inside a playbook?

A Playbook is a list of plays. It can contain a single play, or many.


1 Answers

This is really bad example of how subelements lookup works. (And has old, unsupported, syntax as well).

Look at this one:

--- - hosts: localhost   gather_facts: no   vars:     families:       - surname: Smith         children:           - name: Mike             age: 4           - name: Kate             age: 7       - surname: Sanders         children:           - name: Pete             age: 12           - name: Sara             age: 17    tasks:     - name: List children       debug:         msg: "Family={{ item.0.surname }} Child={{ item.1.name }} Age={{ item.1.age }}"       with_subelements:         - "{{ families }}"         - children 

Task List children is like a nested loop over families list (outer loop) and over children subelement in each family (inner loop).
So you should provide a list of dicts as first argument to subelements and name of subelement you want to iterate inside each outer item.

This way item.0 (family in my example) is an outer item and item.1 (child in my example) is an inner item.

In Ansible docs example subelements is used to loop over users (outer) and add several public keys (inner).

like image 131
Konstantin Suvorov Avatar answered Oct 05 '22 15:10

Konstantin Suvorov