# Is it possible to read different database Columns entries returned by SQL query in respective variables in Ansible ?

**URL:** <https://forum.ansible.com/t/is-it-possible-to-read-different-database-columns-entries-returned-by-sql-query-in-respective-variables-in-ansible/31172>\
**Category:** Ansible Project\
**Created:** [August 30, 2019, 4:44am UTC](https://forum.ansible.com/t/is-it-possible-to-read-different-database-columns-entries-returned-by-sql-query-in-respective-variables-in-ansible/31172 "2019-08-30T04:44:33Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Easy\_King](https://avatars.discourse-cdn.com/v4/letter/e/c2a13f/32.png) [@Easy\_King](https://forum.ansible.com/u/Easy_King)\
**Post date:** [August 30, 2019, 4:44am UTC](https://forum.ansible.com/t/is-it-possible-to-read-different-database-columns-entries-returned-by-sql-query-in-respective-variables-in-ansible/31172/1 "2019-08-30T04:44:33Z")

</div>

Below is my playbook with a single SQL query that fetches 3 columns from MYSQL database.

I wish to read the values returned if any in three different variables.

`

- name: “Fetch from Database”  
tags: validateA  
command: \>  
mysql --user=root --password=mypass@d deployment  
–host=localhost -Ns -e “SELECT status,layer,environment FROM my\_database WHERE num LIKE ‘{{ Number }}’ AND status LIKE ‘Deploy’”  
failed\_when: (‘{{ Number }}’ not in command\_result.stdout) or command\_result.rc != 0  
`

Thus, variable1 should have the output of status  
variable2 should have the output of layer  
variable3 should have the output of environment

This will help me get the results with one query fired instead of Running three seperate queries one for each column.

Please let me know if and how is it possible ?

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [August 30, 2019, 6:18am UTC](https://forum.ansible.com/t/is-it-possible-to-read-different-database-columns-entries-returned-by-sql-query-in-respective-variables-in-ansible/31172/2 "2019-08-30T06:18:19Z")

</div>

At a high level I would look at adjusting the query so that it returns json:  
[https://dev.mysql.com/doc/refman/5.7/en/json-creation-functions.html](https://dev.mysql.com/doc/refman/5.7/en/json-creation-functions.html)

Register the output, and then set your values in a set\_fact task.

And I would try to change the authorization setup so you don’t have to echo throw root passwords around in shell commands.

Dick
