我试图收集一个MySQL集群的信息,然后在一些本地逻辑中使用。
我的麻烦是,如果我在远程主机上运行我的命令,我将无法访问这些结果
- 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: 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
然而,MySQL踢了一个警告,说实话,我不想强制安装一个mysql客户端的本地机器的要求。
讨厌的感觉,我将不得不在这里写一些Python …
使用set_fact模块和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
是否有助于在本地主机或主机上写入mysql命令的输出,并继续追加所有服务器的结果。 一旦完成,你可以在你的剧本中parsing这个文件,甚至可以编写一个parsing器脚本,并在剧本中执行。
收集输出将看起来像这样 –
---
- 主持人:生产
任务:
- name:获取现有群集节点的uuids
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','
注册:maria_cluster_uuids
- 名称:写入本地磁盘
lineinfile:dest = / tmp / mysqlcluster create =是line =“{{maria_cluster_uuids.stdout_lines}}”
delegate_to:127.0.0.1
然后你可以parsing/ tmp / mysqlcluster文件。