我正试图通过将cluster_status
命令输出到一个变量来验证rabbitmq-集群状态,然后查看输出是否包含该组的所有主机。如果缺少任何主机,请退出。
如何检查字符串(command_output(是否包含列表(group_hosts(中的所有值?
我的代码:
- name: check cluster status
command: rabbitmqctl cluster_status
register: cluster_status
changed_when: false
输出:
"stdout": "Cluster status of node rabbit@ip-127.0.0.1 ...n[{nodes,[{disc,['rabbit@ip-127.0.0.1','rabbit@ip-127.0.0.2']}]},n {running_nodes,['rabbit@ip-127.0.0.2','rabbit@ip-127.0.0.1']},n {cluster_name,<<"rabbit@ip-127.0.0.2.ec2.internal">>},n {partitions,[]},n {alarms,[{'rabbit@ip-127.0.0.2',[]},{'rabbit@ip-127.0.0.1',[]}]}]",
"stdout_lines": [
"Cluster status of node rabbit@ip-127.0.0.1 ...",
"[{nodes,[{disc,['rabbit@ip-127.0.0.1','rabbit@ip-127.0.0.2']}]},",
" {running_nodes,['rabbit@ip-127.0.0.2','rabbit@ip-127.0.0.1']},",
" {cluster_name,<<"rabbit@ip-127.0.0.2.ec2.internal">>},",
" {partitions,[]},",
" {alarms,[{'rabbit@ip-127.0.0.2',[]},{'rabbit@ip-127.0.0.1',[]}]}]"
]
列表变量:
group_hosts=[ip-127.0.0.1,ip-127.0.0.2]
如何从stdout获取变量的以下行?
running_nodes,['rabbit@ip-127.0.0.2','rabbit@ip-127.0.0.1']
-
您可以使用
in
测试检查字符串是否包含在其他字符串中"{{ 'a_string' in 'an other string containing a_string somewhere' }}"
一行演示:
$ ansible localhost -m debug -a "msg={{ 'a_string' in 'an other string containing a_string somewhere' }}" localhost | SUCCESS => { "msg": true } $ ansible localhost -m debug -a "msg={{ 'a_string' in 'it won''t be find in here' }}" localhost | SUCCESS => { "msg": false }
-
可以使用
select
或reject
筛选器根据测试筛选列表元素。在您的情况下,我们可以使用reject从初始列表中筛选出在给定命令结果字符串中实际找到的所有元素。如果找到了所有元素,则生成的列表应为空。
- name: demo task which fails if all servers are not found vars: unfound: "{{ group_hosts | reject('in', cluster_status.stdout) | list }}" assert: that: unfound | length == 0 success_msg: all servers where found fail_msg: "one ore more servers are missing: {{ unfound | join(', ') }}"
我能够在--formatter json
标志到rabbitmqq命令的帮助下实现这一点。
- name: Get cluster status
shell: rabbitmqctl cluster_status --formatter json
register: cluster_status
failed_when: cluster_status.rc > 0
- name: demo task which fails if all servers are not found
vars:
group_hosts: "{{ groups['rmq_servers'] | map('extract', hostvars, 'ansible_hostname') | list }}"
running_nodes: "{{ cluster_status.stdout| from_json| json_query('running_nodes') }}"
assert:
that: group_hosts|length == running_nodes|length
success_msg: All Nodes joined cluster
fail_msg: "All Nodes are not part of cluster: {{ group_hosts|length - running_nodes|length }} missing"
感谢@Zeitounator和@Vladimir Botka。两个答案都有助于