I have a playbook that is retrieving data from a test database, but I am unable to figure out how to extract the rows returned from the database.
The whole purpose of the exercise is to pull the value from the KVP and add it to a report, so eventual the output would look similar to:
datetime | hostname | server_status | server_type | associated_controller |
---|---|---|---|---|
null | ATT_DELL | UP | NODE | 123.456.78.901 |
null | steve_ubuntu | UP | CONTROLLER | NOT APPLICABLE |
- name: Sample playbook
gather_facts: True
hosts: sql_server
tasks:
- name: Retrieve data from database
tags: always
community.mysql.mysql_query:
login_host: "{{ login_host }}"
login_db: "{{ login_db }}"
login_user: "{{ login_user }}"
login_password: "{{ login_password }}"
query: select * from test_db
register: mysql_result
- name: output query results
tags: always
debug:
msg: "{{ item }}"
loop: "{{ mysql_result.query_result }}"
PLAY [Sample playbook] **************************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************************
ok: [server_name]
TASK [Pull data from database] *************************************************************************************************************************
ok: [server_name]
TASK [output query results] **************************************************************************************************************************
ok: [servername] => (item=[{u'datetime': None, u'hostname': u'steve_ubuntu', u'associated_controller': u'NOT APPLICABLE', u'server_type': u'controller', u'server_status': u'Up'}, {u'datetime': None, u'hostname': u'ATT_DELL', u'associated_controller': u'NOT APPLICABLE', u'server_type': u'controller', u'server_status': u'Up'}]) =>
msg:
- associated_controller: NOT APPLICABLE
datetime: null
hostname: steve_ubuntu
server_status: Up
server_type: controller
- associated_controller: NOT APPLICABLE
datetime: null
hostname: ATT_DELL
server_status: Up
server_type: controller
I tried the following:
- name: output query results
tags: never
debug:
msg: "{{ item[0] }}"
loop: "{{ mysql_result.query_result | items2dict }}"
Which resulted in the following, but I need to loop through each list items, extracting the individual values from each kvp to be used in the report.
ok: [servername] => (item=[{u'datetime': None, u'hostname': u'steve_ubuntu', u'associated_controller': u'NOT APPLICABLE', u'server_type': u'controller', u'server_status': u'Up'}, {u'datetime': None, u'hostname': u'ATT_DELL', u'associated_controller': u'NOT APPLICABLE', u'server_type': u'controller', u'server_status': u'Up'}]) =>
msg:
associated_controller: NOT APPLICABLE
datetime: null
hostname: steve_ubuntu
server_status: Up
server_type: controller
I have tried so many other things like converting the list to a dictionary, but I received the error below. I need to keep the input dynamic, but I don't know how many items will be retrieved from the database.
fatal: [servername]: FAILED! => msg: 'Unexpected templating type error occurred on ({{ mysql_result.query_result | items2dict}}): list indices must be integers, not str'
I'm not just looking for an answer to my problem, but what fundamentals about ansible do I need to read through to better understand the fundamentals about the values returned and how to extract them? I've read through Ansible's online documentation, but that's not the best documentation for understanding their product.