Ansible multiple includes "in block"

user140547 picture user140547 · Aug 17, 2016 · Viewed 8k times · Source

I have a playbook with includes:

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

Is there any possibility not to repeat the condition? I tried blocks but it seems blocks cannot be used in that context:

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

Is there any way to do that? I also tried something like

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

which also does not work

Answer

Konstantin Suvorov picture Konstantin Suvorov · Aug 17, 2016

This syntax works fine in ansible 2.1.1 (be accurate with indentation):

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