I'm trying to write a play that allows for me to only build certain directories conditionally while using the with_nested.
routers:
router1:
hostname: adam
router2:
hostname: steve
Here's my play:
- name: create dirs for swiftmq
file:
state: directory
path: "{{app_path}}/{{item[1]}}/{{item[0]}}"
follow: yes
owner: inpho
group: inpho
mode: 0755
when:
- item[0].hostname == ansible_hostname
with_nested:
- "{{routers}}"
- [ 'config', 'deploy', 'jmsapp', 'log', 'store', 'trace' ]
So, my desired behavior is that it would only create the directories when absible_hostname=adam or ansible_hostname=steve.
I get the following error:
fatal: [xxx]: FAILED! => {"failed": true, "msg": "The conditional check 'item[0].hostname == ansible_hostname' failed. The error was: error while evaluating conditional (item[0].hostname == ansible_hostname): 'unicode object' has no attribute 'hostname'\n\nThe error appears to have been in 'xxx': line 33, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: create dirs for xxx\n ^ here\n"}
Is it possible to use a when condition in conjunction with with_nested? If so, how do I reference children of the items in the conditional? I've tried it a bunch of ways & can't seem to figure it out.
Thanks in advance!
Adam