Is it possible combine remote results to local a register in Ansible?
I'm trying to gather information on a mysql cluster in order to then use in some local logic.
My trouble is if I run my command on the remote hosts I won't have access to those results
- name: get uuids for existing cluster nodes
shell: mysql -N -B -u {{ db_user }} -p {{ db_user_password }} -e "SHOW GLOBAL STATUS LIKE 'wsrep_cluster_state_uuid';" | sed 's/\t/,/g' | cut -f2 -d','
register: maria_cluster_uuids
This gives me the data I need but what I'd really like is a combined list/dict of the results.
I could try:
- name: get uuids for existing cluster nodes
run_once: true
shell: mysql -N -B -u {{ db_user }} -h {{ item }} -p {{ db_user_password }} -e "SHOW GLOBAL STATUS LIKE 'wsrep_cluster_state_uuid';" | sed 's/\t/,/g' | cut -f2 -d','
register: maria_cluster_uuids
with_items: play_hosts
delegate_to: 127.0.0.1
however, mysql kicks up a warning and to be honest, I don't want to impose a requirement of the local machine having a mysql client installed.
Nasty feeling I'm going to have to write some python here...
Use set_fact
module and hostvars
:
---
- hosts: all
vars:
uuids: |
{%- set o=[] %}
{%- for i in play_hosts %}
{%- if o.append(hostvars[i].uuid) %}
{%- endif %}
{%- endfor %}
{{ o }}
tasks:
- name: get uuids for existing cluster nodes
shell: mysql -N -B -u {{ db_user }} -p {{ db_user_password }} -e "SHOW GLOBAL STATUS LIKE 'wsrep_cluster_state_uuid';" | sed 's/\t/,/g' | cut -f2 -d','
register: maria_cluster_uuids
- set_fact:
uuid: "{{ maria_cluster_uuids.stdout }}"
- debug:
var: uuids
run_once: true
delegate_to: 127.0.0.1
Would it help to write the output of the mysql command on the local host or ansible host, and keep on appending the results from all the servers. Once done, you can parse that file within your playbook, or even write a parser script and execute if from the playbook.
Collecting the output would look something like this -
--- - hosts: production tasks: - name: get uuids for existing cluster nodes shell: mysql -N -B -u {{ db_user }} -p {{ db_user_password }} -e "SHOW GLOBAL STATUS LIKE 'wsrep_cluster_state_uuid';" | sed 's/\t/,/g' | cut -f2 -d',' register: maria_cluster_uuids - name: Write to local disk lineinfile: dest=/tmp/mysqlcluster create=yes line="{{ maria_cluster_uuids.stdout_lines }}" delegate_to: 127.0.0.1
Then you can parse /tmp/mysqlcluster file.