如果以前的任务都没有运行,则中止

时间:2016-07-18 14:05:48

标签: ansible ansible-playbook

我有tasks.yml,其中包含以下内容:

- name: something on Debian wheezy
  when: ansible_distribution == 'Debian' and ansible_distribution_version == '7'

- name: something on Debian jessie
  when: ansible_distribution == 'Debian' and ansible_distribution_version == '8'

- name: something on Ubuntu
  when: ansible_distribution == 'Ubuntu'

如果没有运行这些任务,我想添加一个中止该剧本的任务(或其他东西)。

也许"某事"正在安装一个软件包,之后的一些任务是配置新安装的软件包。如果我在RedHat系统上运行playbook,那么该软件包将无法安装,我想在执行配置它的任务之前中止。

我想我可以这样做:

- fail: msg="abort!"
  when: not (ansible_distribution == 'Debian' and ansible_distribution_version == '7') and not (ansible_distribution == 'Debian' and ansible_distribution_version == '8') and not ansible_distribution == 'Ubuntu'

......但那令人讨厌。有没有更好的解决方案?

2 个答案:

答案 0 :(得分:1)

---
- name: test
  hosts: localhost

  tasks:
  - name: Install pkg
    debug: msg="Installed pkg"
    when: "{{ ansible_distribution == 'Debian' }}"
    register: debian_pkg

  - name: Install pkg
    debug: msg="Installed pkg"
    when: "{{ ansible_distribution == 'Ubuntu' }}"
    register: ubuntu_pkg

  - fail: msg="Unsupported OS"
    when: "debian_pkg|skipped and ubuntu_pkg|skipped"

答案 1 :(得分:0)

我终于采用了更简单,更“商业”的方式。我想安装something,然后我希望安装something。事实上,在我的基础设施中,并非一切都是自动化的(我不是无处不在的根)我有时无法安装something,但我仍然想检查它是否已安装(它是在带外安装的) )。

因此,在根据操作系统安装something的任务之后,我添加了一个检查是否已安装的任务,只需尝试运行它即可。这样的事情:

- include: install_something.yml # contains the install tasks for each OS
  when: can_become_root

- name: Check that something is installed
  command: something --version