I wish to collect the results of SQL query so that every database column is
registered with a variable / array.
Thus, I should be able to get the values returned by three database columns
viz destip,filedets,mynum in three variables or an array / list in the same
or a different play.
Below is how my playbooks & its database output looks like.
tasks:
- name: "Search in the database"
command: >
mysql --user=root --password=mypassword deployment
--host=localhost -Ns -e "SELECT destip,filedets,mynum FROM MYTABLE
WHERE mynum LIKE '{{ Number }}'"
register: command_result
- set_fact:
sql_output: "{{ command_result.stdout_lines }}"
failed_when: command_result.stdout == "" or command_result.rc != 0
- name: "Print output for Number Search"
debug:
msg: "{{ command_result.stdout }}"
Output:
> TASK [set_fact]
> *********************************************************************************************************************************************
> task path: /app/rollback.yml:43
> ok: [localhost] => {
> "ansible_facts": {
> "sql_output": [
> "10.17.54.26,10.17.34.46\t10.17.54.26\\n-rwxrwxrwx user1
> 2019-08-31 17:36 /opt/logs/scripts/filename6.src 4294967295\\n-rw-rw-r--
> user1 2019-08-30 17:31 /opt/logs/sql/33211.sql
> 4294967295\\n10.17.34.46\\n-rwxrwxrwx user1 2019-08-31 17:36
> /opt/logs/scripts/filename6.src 4294967295\\n-rw-rw-r-- user1 2019-08-30
> 17:31 /opt/logs/sql/33211.sql 4294967295\tCR8745"
> ]
> },
> "changed": false,
> "failed_when_result": false
> }
> TASK [Print output for Number Search]
> ********************************************************************************************************************
> task path: /app/rollback.yml:54
> ok: [localhost] => {
> "msg": "10.17.54.26,10.17.34.46\t10.17.54.26\\n-rwxrwxrwx user1
> 2019-08-31 17:36 /opt/logs/scripts/filename6.src 4294967295\\n-rw-rw-r--
> user1 2019-08-30 17:31 /opt/logs/sql/33211.sql
> 4294967295\\n10.17.34.46\\n-rwxrwxrwx user1 2019-08-31 17:36
> /opt/logs/scripts/filename6.src 4294967295\\n-rw-rw-r-- user1 2019-08-30
> 17:31 /opt/logs/sql/33211.sql 4294967295\tCR8745"
> }
As you can see the output of each column is seperated by a tab "\t"
Below is what I tried to assign and reference each column to an array:
- name: "LIST SELECT SQL QUERY output in order"
debug:
msg: "at array position {{ item.0 }} there is a value {{ item.1 }}
and hassss {{ item.0 }}"
with_items:
- "{{ sql_output.split('\t') }}"
I also tried with_indexed_items but could not get this to work.
Output Error:
TASK [LIST SELECT SQL QUERY output in order]
> ****************************************************************************************************************
> task path: /app/rollback.yml:64
> fatal: [localhost]: FAILED! => {
> "msg": "'list object' has no attribute 'split'"
> }
Can you please suggest ?
--
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 [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/de87fca6-8ece-4dce-aa87-64e454a43fd8%40googlegroups.com.