Last active
May 3, 2019 10:21
-
-
Save halberom/debc44697f05dc3f69173805b1014c33 to your computer and use it in GitHub Desktop.
ansible - example of getting subset of hash using list
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PLAY [localhost] *********************************************************************************************************************************************************************************************************************************************************************************************************************************************************** | |
TASK [Gathering Facts] ***************************************************************************************************************************************************************************************************************************************************************************************************************************************************** | |
ok: [localhost] | |
TASK [set_fact] ************************************************************************************************************************************************************************************************************************************************************************************************************************************************************ | |
ok: [localhost] | |
TASK [debug] *************************************************************************************************************************************************************************************************************************************************************************************************************************************************************** | |
ok: [localhost] => { | |
"foo": [ | |
{ | |
"foo2": "foo2", | |
"name": "john" | |
}, | |
{ | |
"foo3": "foo3", | |
"name": "jane" | |
} | |
] | |
} | |
PLAY RECAP ***************************************************************************************************************************************************************************************************************************************************************************************************************************************************************** | |
localhost : ok=3 changed=0 unreachable=0 failed=0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
- hosts: localhost | |
gather_facts: True | |
connection: local | |
vars: | |
subset: jane,john | |
persons: | |
- name: tom | |
foo1: foo1 | |
- name: john | |
foo2: foo2 | |
- name: jane | |
foo3: foo3 | |
tasks: | |
- set_fact: | |
foo: "{{ persons | selectattr('name', 'in', subset) | list }}" | |
- debug: | |
var: foo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PLAY [localhost] *********************************************************************************************************************************************************************************************************************************************************************************************************************************************************** | |
TASK [Gathering Facts] ***************************************************************************************************************************************************************************************************************************************************************************************************************************************************** | |
ok: [localhost] | |
TASK [set_fact] ************************************************************************************************************************************************************************************************************************************************************************************************************************************************************ | |
ok: [localhost] => (item=jane) | |
ok: [localhost] => (item=john) | |
TASK [debug] *************************************************************************************************************************************************************************************************************************************************************************************************************************************************************** | |
ok: [localhost] => { | |
"foo": { | |
"jane": { | |
"foo3": "foo3" | |
}, | |
"john": { | |
"foo2": "foo2" | |
} | |
} | |
} | |
PLAY RECAP ***************************************************************************************************************************************************************************************************************************************************************************************************************************************************************** | |
localhost : ok=3 changed=0 unreachable=0 failed=0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
- hosts: localhost | |
gather_facts: False | |
connection: local | |
vars: | |
subset: jane,john | |
persons: | |
tom: | |
foo1: foo1 | |
john: | |
foo2: foo2 | |
jane: | |
foo3: foo3 | |
tasks: | |
- set_fact: | |
foo: "{{ foo | default({}) | combine({item: persons[item]}) }}" | |
when: item in subset | |
loop: "{{ persons.keys() | list }}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment