user140547
user140547

Reputation: 8200

Ansible multiple includes "in block"

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

Upvotes: 6

Views: 12343

Answers (2)

nelsonenzo
nelsonenzo

Reputation: 680

"include" is soon being depreciated as well. The latest and greatest (>2.4 I think) is "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'

EDIT: As pointed out below, "include_task" rather than "include" (depreciated) or "import_task" (slightly different use case) is the technically correct answer to the initial question.

- 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'

Upvotes: 0

Konstantin Suvorov
Konstantin Suvorov

Reputation: 68269

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')

Upvotes: 7

Related Questions