1

I am executing shell script with Ansible which returns json output.

 - name: Get mlist
    become: no
    shell: "PYTHONPATH=/home/centos/scripts/users/ python /home/centos/scripts/users/team_members.py {{ parameter }}"
    register: account_list

the output looks like

ok: [localhost] => {
    "msg": {
        "changed": true,
        "cmd": "PYTHONPATH=/home/centos/scripts/users/ python /home/centos/scripts/users/team_members.py parameter",
        "delta": "0:00:00.530377",
        "end": "2019-10-09 08:28:20.222480",
        "failed": false,
        "rc": 0,
        "start": "2019-10-09 08:28:19.692103",
        "stderr": "2019-10-09 08:28:19,915 INFO",
        "stderr_lines": [
            "2019-10-09 08:28:19,915 INFO"
        ],
        "stdout": "[{'id': 'XXX=', 'name': 'XXX', 'login': 'xxx'}, {'id': 'YYY', 'name': 'YYY', 'login': 'yyy'}, {'id': 'ZZZ', 'name': 'zzz', 'login': 'zzz'}]",
        "stdout_lines": [
            "[{'id': 'XXX=', 'name': 'XXX', 'login': 'xxx'}, {'id': 'YYY', 'name': 'YYY', 'login': 'yyy'}, {'id': 'ZZZ', 'name': 'ZZZ', 'login': 'zzz'}]"
        ]
    }
}

What I would like to do is to extract all login items. So far I've tried various ways

  - debug:
      msg: "{{ account_list.stdout | to_json | json_query('[*].login') }}"

But this is not working. While putting the .stdout to JSONPath Online Evaluator the .[*].login does what I want I just can't do that with Ansible json_query. Anybody who know how to do that ?

Thank you very much in advance.

aRagornol
  • 235
  • 3
  • 17

1 Answers1

3

I found the solution. I`ve had to update python script to actually output json as previously it didn't output correct json (see use of ' instead of ") and then

 - debug:
      msg: "{{ item }}"
   with_items: "{{ account_list.stdout | from_json | json_query('[*].login') }}"

worked correctly

aRagornol
  • 235
  • 3
  • 17