Answers: <p>The <code>community.mysql.mysql_query</code> module <a href="https://docs.ansible.com/ansible/latest/collections/community/mysql/mysql_query_module.html#return-values" rel="nofollow noreferrer">documents its return values</a> as:</p>
  <ul>
  <li><code>executed_queries</code></li>
  <li><code>query_results</code></li>
  <li><code>rowcount</code></li>
  </ul>
  <p>So, if you're registering <code>r_query</code>, and you want to see the result, you can call <code>r_query.query_result</code>.</p>
  <p>A common approach when developing playbooks initially is to send to <code>stdout</code> with a <a href="https://docs.ansible.com/ansible/latest/collections/ansible/builtin/debug_module.html#ansible-collections-ansible-builtin-debug-module" rel="nofollow noreferrer"><code>debug</code></a> task:</p>
  <pre class="lang-yaml prettyprint-override"><code>- name: Get stats of a file
    hosts: localhost
    become: true
  
    tasks:
      - name: run mysql query
        community.mysql.mysql_query:
          login_user: testuser
          login_password: testpass
          login_db: test_db
          query: SELECT * FROM STATION WHERE ID = %(id_val)s AND STATE = %(story_val)s
          named_args:
            id_val: 12
            story_val: AZ
        register: r_query
  
      - name: what happened
        ansible.builtin.debug:
          msg: &quot;{{ r_query.query_result }}&quot;
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2022-02-18T09:16:57.660Z
Answers: Thank you for the detailed explanation. This was not clear to me from the documentation nor could not find examples.
Answers: Ravi Kumar CH
Answers: CC BY-SA 4.0
Answers: 2022-02-18T07:50:02.517Z
Answers: 2022-02-18T07:50:02.517Z
Answers: Bruce Becker
Body: <p>I have used community.mysql collection and was able to run a query against the database.</p>
  <pre><code>---
  - name: Get stats of a file
    hosts: localhost
    become: true
  
    tasks:
      - name: run mysql query
        community.mysql.mysql_query:
          login_user: testuser
          login_password: testpass
          login_db: test_db
          query: SELECT * FROM STATION WHERE ID = %(id_val)s AND STATE = %(story_val)s
          named_args:
            id_val: 12
            story_val: AZ
        register: r_query
  </code></pre>
  <p>The playbook runs fine without errors</p>
  <pre><code>root@ip-172-31-83-231:/home/ubuntu# ansible-playbook sql_query.yml
  [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
  
  PLAY [Get stats of a file] ***************************************************************************************************************************************************************
  
  TASK [Gathering Facts] *******************************************************************************************************************************************************************
  ok: [localhost]
  
  TASK [run mysql query] *******************************************************************************************************************************************************************
  ok: [localhost]
  
  PLAY RECAP *******************************************************************************************************************************************************************************
  localhost                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored
  </code></pre>
  <p>could not find the documentation on how to display the queries results.
  Can you help me in the right direction.</p>
  
Tags: <ansible><mysql><ansible-galaxy>
Title: ansible mysql query not showing results
UserDisplayName: Ravi Kumar CH
ThreadText: # ansible mysql query not showing results
  
  (Asked by: username_0 on 2022-02-18)
  
  I have used community.mysql collection and was able to run a query against the database.
  
  ```
  ---
  - name: Get stats of a file
    hosts: localhost
    become: true
  
    tasks:
      - name: run mysql query
        community.mysql.mysql_query:
          login_user: testuser
          login_password: testpass
          login_db: test_db
          query: SELECT * FROM STATION WHERE ID = %(id_val)s AND STATE = %(story_val)s
          named_args:
            id_val: 12
            story_val: AZ
        register: r_query
  
  ```
  
  The playbook runs fine without errors
  
  ```
  root@ip-172-31-83-231:/home/ubuntu# ansible-playbook sql_query.yml
  [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
  
  PLAY [Get stats of a file] ***************************************************************************************************************************************************************
  
  TASK [Gathering Facts] *******************************************************************************************************************************************************************
  ok: [localhost]
  
  TASK [run mysql query] *******************************************************************************************************************************************************************
  ok: [localhost]
  
  PLAY RECAP *******************************************************************************************************************************************************************************
  localhost                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored
  
  ```
  
  could not find the documentation on how to display the queries results.
  Can you help me in the right direction.
  
  **Tags:** ansible, mysql, ansible-galaxy
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2022-02-18. Score: 1 | Accepted answer
  
  The `community.mysql.mysql_query` module [documents its return values](https://docs.ansible.com/ansible/latest/collections/community/mysql/mysql_query_module.html#return-values) as:
  
  * `executed_queries`
  * `query_results`
  * `rowcount`
  
  So, if you're registering `r_query`, and you want to see the result, you can call `r_query.query_result`.
  
  A common approach when developing playbooks initially is to send to `stdout` with a [`debug`](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/debug_module.html#ansible-collections-ansible-builtin-debug-module) task:
  
  ```yaml
  - name: Get stats of a file
    hosts: localhost
    become: true
  
    tasks:
      - name: run mysql query
        community.mysql.mysql_query:
          login_user: testuser
          login_password: testpass
          login_db: test_db
          query: SELECT * FROM STATION WHERE ID = %(id_val)s AND STATE = %(story_val)s
          named_args:
            id_val: 12
            story_val: AZ
        register: r_query
  
      - name: what happened
        ansible.builtin.debug:
          msg: "{{ r_query.query_result }}"
  
  ```
  
  > **Comments:**
  > **username_0**: Thank you for the detailed explanation. This was not clear to me from the documentation nor could not find examples.  
  
  ---
  
  