Created
May 3, 2019 11:25
-
-
Save halberom/35b6939d92edcad1c6a4f9833bd572d1 to your computer and use it in GitHub Desktop.
ansible - example of handling list item default
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 [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 |
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: | |
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