Skip to content

Instantly share code, notes, and snippets.

@halberom
Last active May 3, 2019 10:21
Show Gist options
  • Save halberom/debc44697f05dc3f69173805b1014c33 to your computer and use it in GitHub Desktop.
Save halberom/debc44697f05dc3f69173805b1014c33 to your computer and use it in GitHub Desktop.
ansible - example of getting subset of hash using list
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
---
- 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
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
---
- 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