I want to get the result of a query (the value of log_hostname
paramter) returned back from a playbook like the below
---
- name: get postgres settings
hosts: "{{ targets | default('postgres') }}"
vars:
setting: "{{ pg_setting }}"
tasks:
- name: get a setting
become: true
become_user: postgres
community.postgresql.postgresql_query:
login_unix_socket: "/tmp"
db: postgres
query: "{{ query }}"
loop:
- "SHOW {{ setting }}"
loop_control:
loop_var: query
register: test_result
- name: debug test_result
debug:
msg: "{{ test_result }}"
what I am getting back (no surprise) is
TASK [debug test_result] *********************************************************************************************************************************************************
ok: [dvzsn-rd5108.efa-bauen.portal.cn-mv.de] => {
"msg": {
"changed": true,
"msg": "All items completed",
"results": [
{
"ansible_loop_var": "query",
"changed": true,
"failed": false,
"invocation": {
"module_args": {
"as_single_query": true,
"autocommit": false,
"ca_cert": null,
"connect_params": {},
"db": "postgres",
"encoding": null,
"login_host": "",
"login_password": "",
"login_unix_socket": "/tmp",
"login_user": "postgres",
"named_args": null,
"path_to_script": null,
"port": 5432,
"positional_args": null,
"query": "SHOW log_hostname",
"search_path": null,
"session_role": null,
"ssl_mode": "prefer",
"trust_input": true
}
},
"query": "SHOW log_hostname",
"query_all_results": [
[
{
"log_hostname": "on"
}
]
],
"query_list": [
"SHOW log_hostname"
],
"query_result": [
{
"log_hostname": "on"
}
],
"rowcount": 1,
"statusmessage": "SHOW"
}
],
"skipped": false
}
}
to get deeper into the deatils the first step I am taking is to replace
msg: "{{ test_result }}"
with
msg: "{{ test_result.results }}"
this peels off the outer layer of the output, but I canât mange to get into more detail then that.
I tried adding more .
like msg: "{{ test_result.results.query_result }}"
, but that fails with some
âlist objectâ has no attribute âquery_resultâ
error
I guess knowing the logic of parsing json
with jq
would be the way to go, but I can not get that going quite yet