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,
--
Todd

On 9/27/22 8:09 AM, TIYA JOSE wrote:

Hello,
I have a ansible playbook that has set a json variable using set_fact module. I'm trying to use jq command in ansible playbook, to transform that json to a particular format, but got parse error: Invalid numeric literal at line , column

My Playbook:

- name: get data
  set_fact: data: "{{ value.stdout | from_json }}"

- name: get key value pairs
shell: echo "{{ data }}" | jq 'to_entries | map((.key) + "=" + .value)|join(",")'
register: key_value

Error:

TASK [utils : debug] *************************************************************************************
ok: [localhost] => {
 "data": {
"key1": "keyval1",
 "key2": "keyval2"
      }
 }

TASK [utils : get key value pairs] *************************************************************** FAILED! => {"changed": true, "cmd": "echo \"{'key1': 'keyval1', 'key2': 'keyval2'}\" | jq 'to_entries | map((.key) + \"=\" + .value)|join(\",\")'", "stderr": "parse error: Invalid numeric literal at line 1 , column 22", "stderr_lines": ["parse error: Invalid numeric literal at line 1, column 22"], "stdout": "", "stdout_lines": []}

PS: I had tried using > YAML construct specified in How to use jq in ansible shell tasks <https://stackoverflow.com/questions/64045520/how-to-use-jq-in-ansible-shell-tasks/64057402#64057402> but got same error

- name: get key value pairs
  shell: >
       echo "{{ data }}"
       | jq 'to_entries | map((.key) + "=" + .value)|join(",")' register: key_value

My data variable set using set_fact module:

"data": { "key1": "keyval1", "key2": "keyval2" }

Working jq command and expected output: jqPlay <https://jqplay.org/s/x6hnhPBQUmf>

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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/94134720-84f7-c9b4-b703-5607d05fcb36%40gmail.com.

Reply via email to