# json\_query with authorized\_key-module output

**URL:** <https://forum.ansible.com/t/json-query-with-authorized-key-module-output/25727>\
**Category:** Ansible Project\
**Created:** [October 12, 2017, 6:23am UTC](https://forum.ansible.com/t/json-query-with-authorized-key-module-output/25727 "2017-10-12T06:23:33Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Goetz\_Leupold](https://avatars.discourse-cdn.com/v4/letter/g/a4c791/32.png) [@Goetz\_Leupold](https://forum.ansible.com/u/Goetz_Leupold)\
**Post date:** [October 12, 2017, 6:23am UTC](https://forum.ansible.com/t/json-query-with-authorized-key-module-output/25727/1 "2017-10-12T06:23:33Z")

</div>

Hi,

i have a problem with the response of the authorized\_key-module in combination with the Jinja2 Filter “json\_query”. This filter works already fine for me with the prompt-Module where i extract all the inputs with “y”(query: “results[?user\_input==‘y’]”).

ansible --version  
ansible 2.4.0.0  
python version = 2.7.10 (default, Feb 7 2017, 00:08:15) [GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)]

This is a testplay to prey my problem(you’ll need 2 testssh keys in /tmp with “.pub” ending, example content “ssh-rsa 123== Comment 1” ):

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [October 12, 2017, 9:14am UTC](https://forum.ansible.com/t/json-query-with-authorized-key-module-output/25727/2 "2017-10-12T09:14:22Z")

</div>

Instead of we recreate this why don't you run it for us and post the output?  
Preferably with  
"ANSIBLE\_STDOUT\_CALLBACK=debug ansible-playbook -v playbook.yaml"

---

<div class="post-metadata">

**Author:** ![Goetz\_Leupold](https://avatars.discourse-cdn.com/v4/letter/g/a4c791/32.png) [@Goetz\_Leupold](https://forum.ansible.com/u/Goetz_Leupold)\
**Post date:** [October 12, 2017, 9:42am UTC](https://forum.ansible.com/t/json-query-with-authorized-key-module-output/25727/3 "2017-10-12T09:42:08Z")

</div>

There is no output of the second debug-task. Dont know why…  
Output of the command:

Using /Users/blackended\_user/git/project/ansible/ansible.cfg as config file

PLAY [localhost] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

TASK [Gathering Facts] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost]

TASK [test] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> (item=/tmp/1.pub) =\> {  
“changed”: false,  
“comment”: null,  
“exclusive”: false,  
“failed”: false,  
“gid”: 31346032,  
“group”: “blackended\_group”,  
“item”: “/tmp/1.pub”,  
“key”: “ssh-rsa 123== Comment 1”,  
“key\_options”: null,  
“keyfile”: “/tmp/test\_authorized\_keys”,  
“manage\_dir”: false,  
“mode”: “0600”,  
“owner”: “blackended\_user”,  
“path”: “/tmp/test\_authorized\_keys”,  
“size”: 24,  
“state”: “file”,  
“uid”: 218476800,  
“unique”: false,  
“user”: “blackended\_user”,  
“validate\_certs”: true  
}  
changed: [localhost] =\> (item=/tmp/2.pub) =\> {  
“changed”: true,  
“comment”: null,  
“exclusive”: false,  
“failed”: false,  
“gid”: 31346032,  
“group”: “blackended\_group”,  
“item”: “/tmp/2.pub”,  
“key”: “ssh-rsa 456== Comment 2”,  
“key\_options”: null,  
“keyfile”: “/tmp/test\_authorized\_keys”,  
“manage\_dir”: false,  
“mode”: “0600”,  
“owner”: “blackended\_user”,  
“path”: “/tmp/test\_authorized\_keys”,  
“size”: 48,  
“state”: “file”,  
“uid”: 218476800,  
“unique”: false,  
“user”: “blackended\_user”,  
“validate\_certs”: true  
}

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> {}

MSG:

{‘msg’: u’All items completed’, ‘changed’: True, ‘results’: [{u’comment’: None, u’exclusive’: False, u’uid’: 218476800, u’owner’: u’blackended\_user’, u’manage\_dir’: False, u’group’: u’ADS\Dom\xe4nen-Benutzer’, ‘failed’: False, u’state’: u’file’, u’gid’: 31346032, u’invocation’: {u’module\_args’: {u’comment’: None, u’exclusive’: False, u’validate\_certs’: True, u’key\_options’: None, u’state’: u’present’, u’user’: u’blackended\_user’, u’key’: u’ssh-rsa 123== Comment 1’, u’path’: u’/tmp/test\_authorized\_keys’, u’unique’: False, u’keyfile’: u’/tmp/test\_authorized\_keys’, u’manage\_dir’: False}}, u’size’: 24, ‘\_ansible\_parsed’: True, ‘\_ansible\_item\_result’: True, u’key\_options’: None, u’user’: u’blackended\_user’, u’key’: u’ssh-rsa 123== Comment 1’, u’path’: u’/tmp/test\_authorized\_keys’, u’unique’: False, ‘\_ansible\_no\_log’: False, u’keyfile’: u’/tmp/test\_authorized\_keys’, ‘changed’: False, ‘item’: u’/tmp/1.pub’, u’mode’: u’0600’, u’validate\_certs’: True}, {u’comment’: None, u’exclusive’: False, u’uid’: 218476800, u’owner’: u’blackended\_user’, u’manage\_dir’: False, u’group’: u’ADS\Dom\xe4nen-Benutzer’, ‘failed’: False, u’state’: u’file’, u’gid’: 31346032, u’invocation’: {u’module\_args’: {u’comment’: None, u’exclusive’: False, u’validate\_certs’: True, u’changed’: True, u’key\_options’: None, u’state’: u’present’, u’user’: u’blackended\_user’, u’key’: u’ssh-rsa 456== Comment 2’, u’path’: u’/tmp/test\_authorized\_keys’, u’unique’: False, u’keyfile’: u’/tmp/test\_authorized\_keys’, u’manage\_dir’: False}}, u’size’: 48, ‘\_ansible\_parsed’: True, ‘\_ansible\_item\_result’: True, u’key\_options’: None, u’user’: u’blackended\_user’, u’key’: u’ssh-rsa 456== Comment 2’, u’path’: u’/tmp/test\_authorized\_keys’, u’unique’: False, ‘\_ansible\_no\_log’: False, u’keyfile’: u’/tmp/test\_authorized\_keys’, u’changed’: True, ‘item’: u’/tmp/2.pub’, u’mode’: u’0600’, u’validate\_certs’: True}]}

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
localhost : ok=3 changed=1 unreachable=0 failed=0

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [October 12, 2017, 10:23am UTC](https://forum.ansible.com/t/json-query-with-authorized-key-module-output/25727/4 "2017-10-12T10:23:35Z")

</div>

> There is no output of the second debug-task. Dont know why...  
> Output of the command:

\<snip /\>

> TASK [debug]  
> \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
> ok: [localhost] =\> {}
> 
> MSG:
> 
> {'msg': u'All items completed', 'changed': True, 'results': [{u'comment':  
> None, u'exclusive': False, u'uid': 218476800, u'owner': u'blackended\_user',  
> u'manage\_dir': False, u'group': u'ADS\\Dom\xe4nen-Benutzer', 'failed':  
> False, u'state': u'file', u'gid': 31346032, u'invocation': {u'module\_args':  
> {u'comment': None, u'exclusive': False, u'validate\_certs': True,  
> u'key\_options': None, u'state': u'present', u'user': u'blackended\_user',  
> u'key': u'ssh-rsa 123== Comment 1', u'path': u'/tmp/test\_authorized\_keys',  
> u'unique': False, u'keyfile': u'/tmp/test\_authorized\_keys', u'manage\_dir':  
> False}}, u'size': 24, '\_ansible\_parsed': True, '\_ansible\_item\_result':  
> True, u'key\_options': None, u'user': u'blackended\_user', u'key': u'ssh-rsa  
> 123== Comment 1', u'path': u'/tmp/test\_authorized\_keys', u'unique': False,  
> '\_ansible\_no\_log': False, u'keyfile': u'/tmp/test\_authorized\_keys',  
> 'changed': False, 'item': u'/tmp/1.pub', u'mode': u'0600',  
> u'validate\_certs': True}, {u'comment': None, u'exclusive': False, u'uid':  
> 218476800, u'owner': u'blackended\_user', u'manage\_dir': False, u'group':  
> u'ADS\\Dom\xe4nen-Benutzer', 'failed': False, u'state': u'file', u'gid':  
> 31346032, u'invocation': {u'module\_args': {u'comment': None, u'exclusive':  
> False, u'validate\_certs': True, u'changed': True, u'key\_options': None,  
> u'state': u'present', u'user': u'blackended\_user', u'key': u'ssh-rsa 456==  
> Comment 2', u'path': u'/tmp/test\_authorized\_keys', u'unique': False,  
> u'keyfile': u'/tmp/test\_authorized\_keys', u'manage\_dir': False}}, u'size':  
> 48, '\_ansible\_parsed': True, '\_ansible\_item\_result': True, u'key\_options':  
> None, u'user': u'blackended\_user', u'key': u'ssh-rsa 456== Comment 2',  
> u'path': u'/tmp/test\_authorized\_keys', u'unique': False, '\_ansible\_no\_log':  
> False, u'keyfile': u'/tmp/test\_authorized\_keys', u'changed': True, 'item':  
> u'/tmp/2.pub', u'mode': u'0600', u'validate\_certs': True}]}

Instead of  
&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "{{ item.item }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with\_items: "{{ sshKey | json\_query(query) }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;query: "results[?changed==true]"

You can use this

&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "{{ item.item }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with\_items: '{{ sshKey.results }}'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: item.changed

---

<div class="post-metadata">

**Author:** ![Goetz\_Leupold](https://avatars.discourse-cdn.com/v4/letter/g/a4c791/32.png) [@Goetz\_Leupold](https://forum.ansible.com/u/Goetz_Leupold)\
**Post date:** [October 12, 2017, 10:36am UTC](https://forum.ansible.com/t/json-query-with-authorized-key-module-output/25727/5 "2017-10-12T10:36:52Z")

</div>

> Instead of
> 
> - debug:  
> msg: “{{ item.item }}”  
> with\_items: “{{ sshKey | json\_query(query) }}”  
> vars:  
> query: “results[?changed==true]”
> 
> You can use this
> 
> - debug:  
> msg: “{{ item.item }}”  
> with\_items: ‘{{ sshKey.results }}’  
> when: item.changed
> 
> –  
> Kai Stian Olstad

Thank you a lot. Sometimes i dont see the forest for the trees 😉 But nevertheless i am unhappy i cant use the json\_query filter on that. I neither know if it is a bug or my bad.

---

<div class="post-metadata">

**Author:** ![Goetz\_Leupold](https://avatars.discourse-cdn.com/v4/letter/g/a4c791/32.png) [@Goetz\_Leupold](https://forum.ansible.com/u/Goetz_Leupold)\
**Post date:** [October 12, 2017, 11:35am UTC](https://forum.ansible.com/t/json-query-with-authorized-key-module-output/25727/6 "2017-10-12T11:35:11Z")

</div>

Sorry, but your suggested solution doesnt cover my request full:

- debug:  
msg: “{{ item.item }}”  
with\_items: “{{ sshKey.results }}”  
when:

- sshKey.changed

- debug:  
msg: “{{ item.item }}”  
with\_items: “{{ sshKey.results }}”  
when:

- not sshKey.changed

The conditional isnt based on the results.(item).changed. It is based on “sshKey.changed” and if one of the keys has changed, it is for every item true.

Is it possible to take the actual item in the when clause?

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [October 12, 2017, 1:18pm UTC](https://forum.ansible.com/t/json-query-with-authorized-key-module-output/25727/7 "2017-10-12T13:18:34Z")

</div>

Well, my example did, if you check it you'll see "when: item.changed".

---

<div class="post-metadata">

**Author:** ![Goetz\_Leupold](https://avatars.discourse-cdn.com/v4/letter/g/a4c791/32.png) [@Goetz\_Leupold](https://forum.ansible.com/u/Goetz_Leupold)\
**Post date:** [October 12, 2017, 1:25pm UTC](https://forum.ansible.com/t/json-query-with-authorized-key-module-output/25727/8 "2017-10-12T13:25:21Z")

</div>

I am sorry, my bad! Should had have copy your snipet 😉 Thank you so much!
