Reputation: 2998
We are trying to use a nested loop in shell commmand. The counter part (Looping from 0 to 5) is not getting substituted on the line and due to which ansible is throwing error.
Task is as below
- name: debug
debug: var=output.results.{{item}}.stdout.split('|').1
with_items:
- 0
- 1
- 2
- 3
- 4
- 5
- name: Remove Groups
shell: echo neutron port-update {{ output.results.{{item}}.stdout.split("|").1 }} --no-security-groups > /tmp/test.txt
with_items:
- 0
- 1
- 2
- 3
- 4
- 5
Just to show the debug output
changed: [1.1.1.1] => (item=2.2.2.2)
changed: [1.1.1.1] => (item=3.3.3.3)
changed: [1.1.1.1] => (item=4.4.4.4)
changed: [1.1.1.1] => (item=5.5.5.5)
changed: [1.1.1.1] => (item=6.6.6.6)
changed: [1.1.1.1] => (item=7.7.7.7)
when we give the task as
output.results.0.stdout.split('|').1
or
output.results.1.stdout.split('|').1
we are getting the corrspondin IP .
But when we loop it for 5 times, items are not getting substtituted resulting in the following error
fatal: [1.1.1.1]: FAILED! => {"failed": true, "msg": "template error while templating string: expected name or number. String: echo neutron port-update {{ output.results.[item].stdout.split(\"|\").1 }} --no-security-groups > /tmp/test.txt"}
Upvotes: 0
Views: 296
Reputation: 60039
The var
part already is interpreted as a jinja variable/expression. Therefore you can not place a variable with curly braces inside. Also you can not nest expressions in each other. {{ foo {{ bar }} }}
is invalid syntax.
This should work:
debug: msg="{{ output.results[item].stdout.split('|').1 }}"
...
shell: echo neutron port-update {{ output.results[item].stdout.split("|").1 }} --no-security-groups > /tmp/test.txt
Upvotes: 1