# Passing MariaDB query result

**URL:** <https://forum.ansible.com/t/passing-mariadb-query-result/38249>\
**Category:** Ansible Project\
**Created:** [July 20, 2023, 3:42pm UTC](https://forum.ansible.com/t/passing-mariadb-query-result/38249 "2023-07-20T15:42:37Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alex\_Wanderley](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/alex_wanderley/32/3232_2.png) [@Alex\_Wanderley](https://forum.ansible.com/u/Alex_Wanderley)\
**Post date:** [July 20, 2023, 3:42pm UTC](https://forum.ansible.com/t/passing-mariadb-query-result/38249/1 "2023-07-20T15:42:37Z")

</div>

Hello,

I need to collect some hostnames from a MariaDB database but don’t know exactly how to properly parse the result of my query… Could somebody help?

This how the tasks look like:

- name: Collect devices in down state  
community.mysql.mysql\_query:  
login\_db: cacti  
query: SELECT hostname FROM host WHERE status=1  
login\_unix\_socket: /run/mysqld/mysqld.sock  
register: cacti\_query

- name: print query result  
debug:  
msg: “{{ cacti\_query.query\_result }}”

And this is the output:

_The contents of this message and any attachment(s) are confidential, proprietary to the City of Edmonton, and are intended only for the addressed recipient. If you have received this in error, please disregard the contents, inform the sender of the misdirection, and remove it from your system. The copying, dissemination, or distribution of this message, if misdirected, is strictly prohibited._

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [July 20, 2023, 3:52pm UTC](https://forum.ansible.com/t/passing-mariadb-query-result/38249/2 "2023-07-20T15:52:03Z")

</div>

```auto
{{ cacti_query.query_result | map(attribute='hostname') }}

```

---

<div class="post-metadata">

**Author:** ![Alex\_Wanderley](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/alex_wanderley/32/3232_2.png) [@Alex\_Wanderley](https://forum.ansible.com/u/Alex_Wanderley)\
**Post date:** [July 20, 2023, 4:43pm UTC](https://forum.ansible.com/t/passing-mariadb-query-result/38249/3 "2023-07-20T16:43:20Z")

</div>

Hi Todd,

Thanks for helping…

{{ cacti\_query.query\_result | map(attribute=‘hostname’) }}

Gave me the output:

“msg”: “\<generator object do\_map at 0x7fe0c4e9f2b0\>”

Looking around I found that {{ cacti\_query.query\_result | map(attribute=‘hostname’) | list }} would be the solution for the message above, but instead I got:  
“msg”: “[AnsibleUndefined]”

Would that possibly be caused by Ansible/Python versions?  
We have Ansible 2.9.7 and Python 3.6.8.

Regards,

Alex

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [July 20, 2023, 5:17pm UTC](https://forum.ansible.com/t/passing-mariadb-query-result/38249/4 "2023-07-20T17:17:33Z")

</div>

That’s twice this morning a working solution was inapplicable b/c someone was still running Ansible 2.9. I know people don’t always get to pick the version of whatever software they have to use because of factors beyond their control. We were stuck on 2.9 far longer than we would have liked.

However, if it’s at all possible, IMO, any time spent trying to make stuff work under 2.9 _rather than moving to a later version_ is time seriously wasted. There have been so very many improvements.

Adding “| list” as you did should have been enough, though. It makes me wonder what was in your registered result on that particular run.

Sorry I can’t offer any actionable suggestions. Good luck,

---

<div class="post-metadata">

**Author:** ![Alex\_Wanderley](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/alex_wanderley/32/3232_2.png) [@Alex\_Wanderley](https://forum.ansible.com/u/Alex_Wanderley)\
**Post date:** [July 20, 2023, 6:09pm UTC](https://forum.ansible.com/t/passing-mariadb-query-result/38249/5 "2023-07-20T18:09:29Z")

</div>

Yes, I’m afraid we indeed are still in a transition process…

But you did help a lot: I didn’t think straight and did not try to run the same playbook on one of our future servers, running 2.14.3.

However, it did not like the map solution.

msg: “{{ cacti\_query.query\_result | map(attribute=‘hostname’) }}”

Is giving me:  
"The task includes an option with an undefined variable. The error was: list object has no element AnsibleUndefined(hint=None, obj=missing, name=‘hostname’).

Could you give one last insight about that?  
(I have no intention to drag this out for longer and keep bothering our list with something that I understand is simple but I don’t have yet the knowledge to solve.)

Regards,

Alex

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [July 20, 2023, 6:48pm UTC](https://forum.ansible.com/t/passing-mariadb-query-result/38249/6 "2023-07-20T18:48:24Z")

</div>

You’d need to show what’s registered to cacti\_query. It appears to be saying it doesn’t contain anything named “hostname”. It may not contain “query\_result” either. Like I said before, it makes me question what’s registered by your query _in that particular run_. Stick another debug task before that showing what’s being registered by the db query task.

---

<div class="post-metadata">

**Author:** ![Akasurde](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/akasurde/32/230_2.png) [@Akasurde](https://forum.ansible.com/u/Akasurde)\
**Post date:** [July 20, 2023, 6:58pm UTC](https://forum.ansible.com/t/passing-mariadb-query-result/38249/7 "2023-07-20T18:58:22Z")

</div>

Hi Alex,

Todd’s solution works for me -

```auto
- name: print query result
debug:
msg: "{{ cacti_query.query_result[0] | map(attribute='hostname') }}"

```

```auto

TASK [print query result] ****************************************************************
task path: /root/mysql.yml:13
ok: [localhost] => {
"msg": [
"host_A",
"host_B",
"host_C",
"host_D"
]
}

```

---

<div class="post-metadata">

**Author:** ![Alex\_Wanderley](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/alex_wanderley/32/3232_2.png) [@Alex\_Wanderley](https://forum.ansible.com/u/Alex_Wanderley)\
**Post date:** [July 20, 2023, 7:27pm UTC](https://forum.ansible.com/t/passing-mariadb-query-result/38249/8 "2023-07-20T19:27:57Z")

</div>

OK, thanks again for taking the time to help…

- name: print query result 01  
debug:  
msg: “{{ cacti\_query }}”

Gives:

ok: [xxxxxxxx] =\> {  
“msg”: {  
“changed”: false,  
“executed\_queries”: [  
“SELECT hostname FROM host WHERE status=1”  
],  
“failed”: false,  
“query\_result”: [  
[  
{  
“hostname”: “host\_A”  
},  
{  
“hostname”: " host\_B "  
},  
{  
“hostname”: “host\_C”  
},  
{  
“hostname”: “host\_D”  
}  
]  
],  
“rowcount”: [  
4  
]  
}  
}

---

<div class="post-metadata">

**Author:** ![Alex\_Wanderley](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/alex_wanderley/32/3232_2.png) [@Alex\_Wanderley](https://forum.ansible.com/u/Alex_Wanderley)\
**Post date:** [July 20, 2023, 7:44pm UTC](https://forum.ansible.com/t/passing-mariadb-query-result/38249/9 "2023-07-20T19:44:03Z")

</div>

Hello Abhijeet,

And Todd’s solution just worked for me as well…! 🙂

The issue was the oversight of “[0]” on cacti\_query.query\_result.

Just in case…  
For a few of those still having to deal with 2.9, going with “{{ cacti\_query.query\_result[0] | map(attribute=‘hostname’) | list }}” also produced the same result.

Thank you Todd and Abhijeet…!

Alex
