I try to execute this ansible command :
ansible localhost -m debug -a "var={{ db_config| to_json |json_query(*)}}" \
-e 'db_config=[{"name":"mydb1","services":["app1","app2"]},{"name":"mydb12","services":["app1"]}]'
To get all json elements using json_query(*) but I'm getting the following error :
fatal: [localhost]: FAILED! =>
msg: 'template error while templating string: expected token ''end of print statement'', got ''name''. String: {{"[{\\"name\\":\\"mydb1\\",\\"services\\":[\\"app1\\",\\"app2\\"]},{\\"name\\":\\"mydb12\\",\\"services\\":[\\"app1\\"]}]"}}'
There are several issues in this single one liner:
msg
(to output the result of a template or static value) and not var
(used to debug a single var without any transformation). See the debug module documentationfrom_json
(to transform your string to a dict/list) and not to_json
(which does the exact opposite).json_query
is invalid for two reason:
The following fixed one liner gives the result (I believe...) you expect:
$ ansible localhost -m debug -a 'msg={{ db_config | from_json | json_query("[]") }}' \
-e 'db_config=[{"name":"mydb1","services":["app1","app2"]},{"name":"mydb12","services":["app1"]}]'
localhost | SUCCESS => {
"msg": [
{
"name": "mydb1",
"services": [
"app1",
"app2"
]
},
{
"name": "mydb12",
"services": [
"app1"
]
}
]
}
Note that you can even drop the json decoding step by passing your arguement as a full inlined json:
$ ansible localhost -m debug -a 'msg={{ db_config | json_query("[]") }}' \
-e '{"db_config":[{"name":"mydb1","services":["app1","app2"]},{"name":"mydb12","services":["app1"]}]}'
localhost | SUCCESS => {
"msg": [
{
"name": "mydb1",
"services": [
"app1",
"app2"
]
},
{
"name": "mydb12",
"services": [
"app1"
]
}
]
}
I suspect you are trying all this to later test json_query
expressions to select your data.
selectattr
, rejectattr
, select
, reject
, map
, dict2items
, items2dict
, first
, last
, unique
, zip
, subelements
,...) that combined together can do most of the job json_query
can do without its overhead (installing a python module and an ansible collection). json_query
is only really needed for very complex queries.$ ansible localhost -m debug -a 'msg={{ db_config | from_json }}' \
-e 'db_config=[{"name":"mydb1","services":["app1","app2"]},{"name":"mydb12","services":["app1"]}]'
# OR (the next one can work with `var` for debug)
$ ansible localhost -m debug -a 'var=db_config' \
-e '{"db_config":[{"name":"mydb1","services":["app1","app2"]},{"name":"mydb12","services":["app1"]}]}'