我想为Ansible创建一个剧本,它将首先检查主机是否在线。如果主机处于联机状态--它应该中止执行剧本或跳过任务/角色,这些任务/角色在剧本中定义得更低。否则,它应该执行进一步的操作。
我现在拥有的是:
---
- name: Install nameserver from scratch
hosts: ns
gather_facts: no
vars:
role: BIND
type: host
msg: "Successfully installed from scratch"
tasks:
- name: Check if host is accessible
run_once: true
shell: ping -c 1 -w 2 {{ item }} 2>&1 > /dev/null
register: online
delegate_to: localhost
with_items: "{{ ansible_play_batch }}"
failed_when: false
- name: Install the role if no respond from host is available
include_role:
name: "{{ pl_role }}"
when: online.results.item.rc !=0 # 1)
with_items:
- scratch
- default
- bind
- jabber_notifier
loop_control:
loop_var: pl_role( 1)我知道结果在环变量中,但是如何从那里得到结果呢?
谢谢!
发布于 2018-05-17 16:29:33
好的,伙计们,抱歉,噪音太大了,我把它修好了。
在ping阶段,我可以通过向failed_when键分配"online.rc == 0“的值来中断一个剧本的进一步执行。所以我们可以在那个舞台上执行死刑,
- name: Check if host is accessible
run_once: true
shell: ping -c 1 -w 2 {{ item }} 2>&1 > /dev/null
register: online
delegate_to: localhost
with_items: "{{ ansible_play_batch }}"
failed_when: online.rc == 0在此之后,所有其他任务/角色操作都不会被执行。
- name: Install the role if no respond from host is available
include_role:
name: "{{ pl_role }}"
loop:
- scratch
- default
- bind
- jabber_notifier
loop_control:
loop_var: pl_role干杯
https://stackoverflow.com/questions/50395772
复制相似问题