Passing MariaDB query result

100 views
Skip to first unread message

Alex Wanderley

unread,
Jul 20, 2023, 11:43:09 AM7/20/23
to ansible...@googlegroups.com
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:

ok: [xxxxxxxxx] => {
    "msg": [
        [
            {
                "hostname": "host_A"
            },
            {
                "hostname": "host_B"
            },
            {
                "hostname": "host_C"
            },
            {
                "hostname": "host_D"
            }
        ]
    ]
}


How could I create a list with just the actual host names "host_A  host_ host_C  host_D" to be used/referenced in a loop further down my playbook?

Thanks a lot,

Alex


Edmonton_sig_RGB_S.jpg

Alex Wanderley

Application and Infrastructure Analyst II
Acting Team Lead Server Solutions & Automation

Financial and Corporate Services | Open City and Technology  

 

780-496-4156  Office

780-819-0273  Mobile

 

City of Edmonton

Century Place, 19th Floor

9803 102A Avenue NW

Edmonton AB, T5J 3A3

 

All information contained in this email post is proprietary to the City of Edmonton, confidential and intended only for the addressed recipient. If you have received this post 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 email, if misdirected, is strictly prohibited.


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.

Todd Lewis

unread,
Jul 20, 2023, 11:52:20 AM7/20/23
to ansible...@googlegroups.com, uto...@gmail.com
{{ cacti_query.query_result | map(attribute='hostname') }}

Alex Wanderley

unread,
Jul 20, 2023, 12:43:50 PM7/20/23
to ansible...@googlegroups.com
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

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/7cbbf374-1d36-1fa6-c7ed-15b133d656f6%40gmail.com.


--

Todd Lewis

unread,
Jul 20, 2023, 1:17:34 PM7/20/23
to Ansible Project
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,
--
Todd

Alex Wanderley

unread,
Jul 20, 2023, 2:10:11 PM7/20/23
to ansible...@googlegroups.com
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

Todd Lewis

unread,
Jul 20, 2023, 2:48:24 PM7/20/23
to Ansible Project
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.

Abhijeet Kasurde

unread,
Jul 20, 2023, 2:58:49 PM7/20/23
to ansible...@googlegroups.com
Hi Alex,

Todd's solution works for me -

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

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



--
Thanks,
Abhijeet Kasurde

Alex Wanderley

unread,
Jul 20, 2023, 3:28:26 PM7/20/23
to ansible...@googlegroups.com
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
        ]
    }
}



Alex Wanderley

unread,
Jul 20, 2023, 3:44:38 PM7/20/23
to ansible...@googlegroups.com
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



--

 

Edmonton_sig_RGB_S.jpg

Alex Wanderley

Application and Infrastructure Analyst II
Acting Team Lead Server Solutions & Automation

Financial and Corporate Services | Open City and Technology  

 

780-496-4156  Office

780-819-0273  Mobile

 

City of Edmonton

Century Place, 19th Floor

9803 102A Avenue NW

Edmonton AB, T5J 3A3

 

All information contained in this email post is proprietary to the City of Edmonton, confidential and intended only for the addressed recipient. If you have received this post 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 email, if misdirected, is strictly prohibited.

Reply all
Reply to author
Forward
0 new messages