Skip to content

Instantly share code, notes, and snippets.

@halberom
Created May 3, 2019 11:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save halberom/35b6939d92edcad1c6a4f9833bd572d1 to your computer and use it in GitHub Desktop.
Save halberom/35b6939d92edcad1c6a4f9833bd572d1 to your computer and use it in GitHub Desktop.
ansible - example of handling list item default
PLAY [localhost] ***********************************************************************************************************************************************************************************************************************************************************************************************************************************************************
TASK [Gathering Facts] *****************************************************************************************************************************************************************************************************************************************************************************************************************************************************
ok: [localhost]
TASK [file] ****************************************************************************************************************************************************************************************************************************************************************************************************************************************************************
ok: [localhost] => (item={'key': 'default', 'value': '/tmp/foo/'})
ok: [localhost] => (item={'key': 'jane', 'value': '/tmp/bar'})
TASK [copy] ****************************************************************************************************************************************************************************************************************************************************************************************************************************************************************
changed: [localhost] => (item=abc)
changed: [localhost] => (item=efg)
changed: [localhost] => (item=jane)
changed: [localhost] => (item=john)
PLAY RECAP *****************************************************************************************************************************************************************************************************************************************************************************************************************************************************************
localhost : ok=3 changed=1 unreachable=0 failed=0
$ ls -1 /tmp/foo
abc.txt
efg.txt
john.txt
$ ls -1 /tmp/bar
jane.txt
---
- hosts: localhost
gather_facts: True
connection: local
vars:
mylist1:
- abc
- efg
mylist2:
- jane
- john
mylist: "{{ mylist1 + mylist2 }}"
mydests:
default: /tmp/foo/
jane: /tmp/bar
tasks:
- file:
path: "{{ item.value }}"
state: directory
loop: "{{ mydests | dict2items }}"
- copy:
content: this is a test
dest: "{{ mydests[item] | default(mydests['default']) }}/{{ item }}.txt"
loop: "{{ mylist }}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment