EJEMPLO 1:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | domains: erp: address: erp.mycompany.com crm: address: crm.mycompany.com git: address: git.mycompany.com tasks: - name: Print phone records debug: msg: "{{ item.value.address }}" with_dict: "{{ domains }}" # I can still access a given domain by its name when needed like so: {{ domains.erp.address }} |
EJEMPLO 2:
1 2 3 4 5 6 7 8 9 10 | domains: - erp: erp.mycompany.com - crm: crm.mycompany.com - git: git.mycompany.com - name: Print domains debug: msg: test {{ item }} with_items: - "{{ domains }}" |
EJEMPLO 3:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | - hosts: localhost vars: domains: - name: erp address: erp.mycompany.com - name: crm address: crm.mycompany.com - name: git address: git.mycompany.com tasks: - name: Print phone records debug: msg: "{{ item.address }}" with_items: "{{ domains }}" |
EJEMPLO 4:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | port: abc: - server1 - server2 abc_port: - 22 - 34 cde: - server3 - server cde_port: - 122 - 343 - set_fact: dest_port: [] - set_fact: dest_port: "{{ dest_port + item.0| product(item.1)| map('join', ':')| list }}" loop: "{{ _dest|zip(_port) }}" vars: _d: "{{ dest|d('') }}" _dest_keys: "{{ (_d|length > 0)|ternary(_d.split(','), []) }}" _port_keys: "{{ _dest_keys|product(['_port'])|map('join')|list }}" _dest: "{{ _dest_keys|map('extract', port)|list }}" _port: "{{ _port_keys|map('extract', port)|list }}" gives... dest_port: - server1:22 - server1:34 - server2:22 - server2:34 - server3:122 - server3:343 - server:122 - server:343 |
Mas info:
https://blog.learncodeonline.in/everything-about-ansible-loops