Reputation: 9
The next task reads the contents of a file and for each line read (JSON format), calls a script. This part works fine
- name: create users
script:
cmd: myscript.sh "{{item}}"
with_lines: "cat users-list"
I'm now trying to add a condition to the script call that depends on the content of the row being read (a field from the JSON being read), but I can't seem to find the correct syntax.
- name: create users
script:
cmd: myscript.sh "{{item}}"
with_lines: "cat users-list"
when: "{{(('{' + item + '}') | from_json).userLogin}}"
and I get this error:
Expecting property name enclosed in double quotes
I've tried various syntax, but no luck. I'm a bit out of ideas Any suggestions will be much appreciated.
Upvotes: 0
Views: 411
Reputation: 311606
The immediate cause of the error is that you need to pass valid JSON to the from_json
filter, and JSON only supports double quoted strings ("this is a valid JSON string"
, 'this is not'
).
Additionally, a when
expression is evaluated in an implicit Jinja template context, which means you never use {{...}}
template markers in the condition.
Unfortunately, without knowing the format of your users-list
file, it's hard to suggest the correct syntax. If each line in that file is a valid JSON document, like this:
{"username": "bob", "userLogin": true}
{"username": "alice", "userLogin": false}
Then you could write your task like this:
- name: create users
script:
cmd: myscript.sh "{{item}}"
with_lines: "cat users-list"
when: "(item|from_json).userLogin"
Upvotes: 1