Search code examples
ansibleansible-factsansible-templatejson-query

how to get specific attribute value from stdout json in ansible


instance_tags.stdout output would look like

enter image description here

Expected output of fallowing snippet is App Server-Development

- name: get instance tags
  win_shell: aws ec2 --region us-east-1 describe-tags \ --filters "Name=resource-id,Values={{instance_id}}"
  register: instance_tags


- name: echo instance tags
  debug:
    msg: "{{instance_tags.stdout | json_query('Tags[?Key==''AMSPatchGroup''].Value')}}"

But Actual Output is

enter image description here


Solution

  • You at least want instance_tags.stdout | from_json | ... since .stdout is a string, not an actual structure

    One can see this behavior with a simple experiment showing {{ '{"a":"b"}' | json_query("a") }} returns "" just like your experience, but {{ '{"a":"b"}' | from_json | json_query("a") }} produces "b" as one would expect