Ansible检查主机在线 - >如果不在线 - >启动安装程序

时间:2018-05-17 15:54:10

标签: ansible

我想为Ansible创建一个剧本,它将首先检查主持人是否在线。如果主机在线 - 它应该中止playbook的执行或跳过任务/角色,这些是在剧本中进一步定义的。否则它应该执行进一步的操作。

我现在拥有的:

---
- 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)我知道结果是在循环var中,但是我如何从那里得到它们?

谢谢!

1 个答案:

答案 0 :(得分:1)

好的家伙,对不起噪音,我已经开始工作了。

已经在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

干杯