My Playbook:
- name: get dataError:
TASK [utils : debug] *************************************************************************************PS: I had tried using > YAML construct specified in How to use jq in ansible shell tasks but got same error
- name: get key value pairsMy data variable set using set_fact module:
"data": { "key1": "keyval1", "key2": "keyval2" }Working jq command and expected output: jqPlay
How can I pass the fact variable set using set_fact to jq pipeline in ansible and transform my json?
Thank you
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/42a94ddc-39b6-4e70-ac1b-e289cf546719n%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/1276ef70-ed88-4bba-bfbc-703c5f2d1559n%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/ACE3464F-8DD5-4F7F-8331-18EE8FB4D704%40nist.gov.
There's good advice further up thread about doing data manipulations within Ansible using the tools it provides, and I heartily agree. Still, the original post raises some interesting questions, and it's instructive to understand why the playbook snippet there doesn't work.
First, we aren't
shown what value.stdout
looks like, but since it made it through the from_json
filter let's assume it looks something like
{"app": "myApp", "env": "myEnv"}
In the "get key value
pairs" step, jq
is expecting json, but data
isn't a json string, so it needs to be filtered through to_json.
And since that json is going to contain double-quotes, you'll
need to single-quote the whole thing for the shell to handle it
properly. (And that's assuming none of your json values contains
single quotes!)
Here's the closest I could come up with that does what the original post was attempting to do. Not that is is a good idea; this is just an exercise in fixing stuff as asked. There are other, better ways to do this.
---
- name: json to jq
hosts: localhost
gather_facts: no
vars:
value:
stdout: '{"app": "myApp", "env": "myEnv"}'
tasks:
#
- name: get data
set_fact:
data: "{{ value.stdout | from_json }}"
#
- name: get key value pairs
shell: |
echo '{{ data | to_json }}' | jq 'to_entries | map((.key) + "=" + .value)|join(",")'
register: key_value
Cheers,Thanks Todd! That worked!
Error seems to be jq complaining about the JSON passed to it. As mentioned in the question the json printed in error message isn't valid JSON. Somehow double quotes were replaced by single quote although debug prints valid JSON. Invalid JSON was passed to jq.
Replacing the single quote with double quote also solved the issue.
shell: echo "{{ data }}" | sed "s/'/\"/g" | jq 'to_entries | map((.key) + "=" + .value)|join(",")'--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CACVha7drenovhrAbDRDShHGS8x8hY38a%2Bh3_3aPyjTfVKqW2Ug%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CAB7JCouco4oFzDfnOWgOaBPjMvZ%3DW6ziV5q7_EAdvCioVP%2Bw1Q%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/0049F6D8-E024-474E-A3F3-7C256B56A37E%40nist.gov.