0

I have a role in a playbook which gets a value from a query. That value should then be available in other roles.

I tried: root-play.yml:

roles:
   - update
   - role: query
     tags: query
   - process

The query role:

   - name: Query data
     shell: query.sh
     environment:
         VAR: "some var"
     register: query_out

   - debug: msg="{{ query_out.stdout }}"

   - set_fact:
        id: "{{ query_out.stdout | from_json | json_query(id) }}"

   - debug: msg="{{ id }}"

This fails with a message of like

"the field 'args' has an invalid value ([u'query']), and could not be converted to an dict.The error was: Expecting property name enclosed in double quotes

I am assuming that the set_fact statement above in the roles doesn't work as expected, in fact I always read something that it'd be with a task, but not with a role.

How can I pass that variable on to the next role?

transient_loop
  • 5,984
  • 15
  • 58
  • 117
  • Please show what `query_out.stdout` looks like exactly. I suspect the error is fired by the `from_json` filter which cannot recognise the content as json. Moreover your json_query looks suspicious as the jmespath expression should be quoted. My 2 cent: debug iteratively `query_out.stdout` then `query_out.stdout | from_json` and finally add your json_query until you get your result. Once you're happy, stick it into the set_fact. – Zeitounator Dec 17 '20 at 01:04
  • You are right @Zeitounator, `query_out.stdout` was actually capturing a debug output from `query.sh`, not its actual return value. Therefore it wasn't even correct json (it was having the expected value in it as well, which tricked me). Cleaning up that debug output fixed it. Not sure if you want to provide that as an answer so we can close this question in a clean way. Otherwise, I am fine. Thank you. – transient_loop Dec 17 '20 at 01:18
  • You can probably self delete it as it falls in "non reproducible / caused by a typo" category. Glad I could help. – Zeitounator Dec 17 '20 at 01:22

0 Answers0