过滤易翻译剧本输出



我有一个可解析的剧本的输出,我需要以这种方式应用文件器来继续执行下一个任务。请找到以下输出。

ok: [localhost] => {
"results": [
{
"actions": {
"namespaces": {}, 
"state": "present", 
"xpath": "/Storage/SSL/KeyStorePath"
}, 
"ansible_loop_var": "item", 
"changed": false, 
"pod": 1, 
"failed": false, 
"invocation": {
"module_args": {
"mount": true, 
"input_type": "yaml",
}
}, 
"item": "100.108.22.102", 
"msg": "found 1 nodes"
}, 
{
"actions": {
"namespaces": {}, 
"state": "present", 
"xpath": "/Storage/SSL/KeyStorePath"
}, 
"ansible_loop_var": "item", 
"changed": false, 
"pod": 0, 
"failed": false, 
"invocation": {
"module_args": {
"mount": true, 
"input_type": "yaml",
}
}, 
"item": "100.108.22.103", 
"msg": "found 0 nodes"
}
]

}

在这里,我希望在节点各自的pod值为1时执行下一个任务。如果节点的pod值是0,则下一任务不应在各自的节点ip上运行。

请协助。。

Q:">当节点的相应pod值为1"时执行下一个任务

A: 如何进行下一项任务有很多方法。例如,给定变量结果

  1. 使用delegate_to并循环选择的项目
- hosts: localhost
tasks:
- debug:
msg: 'Task is running on {{ item.item }}'
loop: "{{ results|selectattr('pod', 'eq', 1)|list }}"
loop_control:
label: "{{ item.item }}"
delegate_to: "{{ item.item }}"

给出(节略(

TASK [debug] ****
ok: [localhost -> 100.108.22.102] => (item=100.108.22.102) => 
msg: Task is running on 100.108.22.102

  1. 创建所选主机的列表并测试主机是否在列表中
- hosts: 100.108.22.102,100.108.22.103
tasks:
- debug:
msg: "Task is running on {{ inventory_hostname }}"
when: inventory_hostname in pods
vars:
pods: "{{ results|
selectattr('pod', 'eq', 1)|
map(attribute='item')|
list }}"

给出(节略(

TASK [debug] ****
ok: [100.108.22.102] => 
msg: Task is running on 100.108.22.102
skipping: [100.108.22.103]

  1. 使用add_host在第一次播放中创建库存组,并在第二次播放中使用
- hosts: localhost
tasks:
- add_host:
name: '{{ item }}'
groups: pods_enabled
loop: "{{ results|
selectattr('pod', 'eq', 1)|
map(attribute='item')|
list }}"
- hosts: pods_enabled
tasks:
- debug:
msg: "Task is running on {{ inventory_hostname }}"

给出(节略(

PLAY [localhost] ****
TASK [add_host] ****
changed: [localhost] => (item=100.108.22.102)
PLAY [pods_enabled] ****
TASK [debug] ****
ok: [100.108.22.102] => 
msg: Task is running on 100.108.22.102
PLAY RECAP ****
100.108.22.102: ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0   
localhost: ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

您可以在下一个任务中使用with_itemswhen,如下所示

- name: validate pod value
debug:
msg: "The pod value is 1"
with_items: "{{ your_var.results }}"
when: item.pod == "1"

最新更新