Ansible with_subelements

后端 未结 1 1861
长情又很酷
长情又很酷 2020-12-05 06:31

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_sub

相关标签:
1条回答
  • 2020-12-05 07:24

    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).

    0 讨论(0)
提交回复
热议问题