Ansible multiple包括" in block"

时间:2016-08-17 19:58:21

标签: ansible ansible-playbook ansible-2.x

我有一个包含以下内容的剧本:

- include: include1.yml
  when: doinclude | default('true')
- include: include2.yml
  when: doinclude | default('true')

有没有可能不重复这个条件?我尝试了块,但似乎块不能在这种情况下使用:

- block:
  - include: include1.yml
  - include: include2.yml
  when: doinclude  | default('true')

有没有办法做到这一点?我也试过像

这样的东西
- name: test
  hosts: all
  tasks:
    - block:
      - include: include1.yml
      - include: include2.yml
    when: doinclude  | default('true')

也不起作用

2 个答案:

答案 0 :(得分:7)

此语法在ansible 2.1.1中正常工作(缩进时准确无误):

---
- hosts: localhost
  tasks:
    - block:
        - include: include1.yml
        - include: include2.yml
      when: doinclude | default('true')

答案 1 :(得分:0)

“include”也很快被折旧。最新最好的(我认为> 2.4)是“import_task”

- name: create/update security group, ec2, and elb
  block:
    - import_tasks: security_group.yaml
    - import_tasks: ec2.yaml
    - import_tasks: elb.yaml
  when: STATE == 'present'

编辑:正如下面所指出的,“include_task”而不是“include”(折旧)或“import_task”(略有不同的用例)是对初始问题的技术上正确的答案。

- name: create/update security group, ec2, and elb
  block:
    - include_tasks: security_group.yaml
    - include_tasks: ec2.yaml
    - include_tasks: elb.yaml
  when: STATE == 'present'