[utoddl@tango ansible]$ cat test-subelements3.yml
---
- name: Demo set_fact over subelements
hosts: localhost
gather_facts: false
vars:
vm_name: NED-DEV601-X
vm_drs_groups:
- group_name: VM2-on-dev
vms:
- deep-test
- NED-DEV601-X
type: vm
- group_name: VM2-on-uat
vms:
- NED-TST601-X
type: vm
- group_name: VM2-on-qa
vms:
- deep-test
- NED-DEV601-X
type: vm
tasks:
- name: Show the list generated by the subelements lookup
ansible.builtin.debug:
msg: "{{ lookup('subelements', vm_drs_groups, 'vms') }}"
- name: Iterate over subelements to gather the group_names for {{ vm_name }}
ansible.builtin.set_fact:
match_vm_group: |
{% set matching_groups = [] %}
{% for vm_by_group_name in lookup('subelements', vm_drs_groups, 'vms') %}
{% if vm_by_group_name[1] == vm_name %}
{% set _ = matching_groups.append(vm_by_group_name[0].group_name) %}
{% endif %}
{% endfor %}{{ matching_groups }}
- name: Same thing without subelements
ansible.builtin.set_fact:
match_vm_group: |
{% set matching_groups = [] %}
{% for vm_drs_group in vm_drs_groups %}
{% if vm_name in vm_drs_group.vms %}
{% set _ = matching_groups.append(vm_drs_group.group_name) %}
{% endif %}
{% endfor %}{{ matching_groups }}
[utoddl@tango ansible]$ ANSIBLE_STDOUT_CALLBACK=yaml ansible-playbook test-subelements3.yml -v
Using /etc/ansible/ansible.cfg as config file
PLAY [Demo set_fact over subelements] ***********************************************************
TASK [Show the list generated by the subelements lookup] ****************************************
ok: [localhost] =>
msg:
- - group_name: VM2-on-dev
type: vm
- deep-test
- - group_name: VM2-on-dev
type: vm
- NED-DEV601-X
- - group_name: VM2-on-uat
type: vm
- NED-TST601-X
- - group_name: VM2-on-qa
type: vm
- deep-test
- - group_name: VM2-on-qa
type: vm
- NED-DEV601-X
TASK [Iterate over subelements to gather the group_names for NED-DEV601-X] **********************
ok: [localhost] => changed=false
ansible_facts:
match_vm_group:
- VM2-on-dev
- VM2-on-qa
TASK [Same thing without subelements] ***********************************************************
ok: [localhost] => changed=false
ansible_facts:
match_vm_group:
- VM2-on-dev
- VM2-on-qa
PLAY RECAP **************************************************************************************
localhost : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Cheers,--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/8645357b-5e61-4918-873d-1f410ea9ea87n%40googlegroups.com.
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/4a5551ca-7d30-f9f9-4538-1270b158b2b0%40gmail.com.