6

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

user140547
  • 7,750
  • 3
  • 28
  • 80

2 Answers2

7

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')
Konstantin Suvorov
  • 65,183
  • 9
  • 162
  • 193
  • I have also tried this syntax, but I then get errors like `ERROR! no action detected in task` https://groups.google.com/forum/#!msg/ansible-project/BjycVAVlUpI/qVdiWycuAgAJ I am using 2.1.0, and cannot upgrade to 2.1.1 because of another issue. – user140547 Aug 17 '16 at 21:47
  • @user140547 do you include tasks or plays? – Konstantin Suvorov Aug 18 '16 at 05:55
  • well I am also including plays. Actually, I didn't write that playbook. So, after some searching, am I not supposed to include playbooks? (and then, the structure of the whole playbook would be wrong. but it still seems to work in most cases) – user140547 Aug 18 '16 at 07:09
  • 1
    @user140547 yes, you can't include playbook as a list of tasks - they are different things, see my [other answer](http://stackoverflow.com/a/38526100/2795592) – Konstantin Suvorov Aug 18 '16 at 07:22
  • ok but then I can't use blocks and I have to stick with the originial playbook? – user140547 Aug 18 '16 at 07:24
  • 1
    @user140547 blocks and loops are not supported on playbook level. as a workaround, you can make include_all.yml playbook that includes include1.yml and include2.yml, then include include_all.yml with when statement. – Konstantin Suvorov Aug 18 '16 at 07:34
0

"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'
nelsonenzo
  • 682
  • 5
  • 9
  • In this case you probably want to use `include_tasks` and not `import_tasks`. – Konstantin Suvorov Feb 21 '18 at 09:18
  • Sure, - include_tasks works also. Here is the info people can use to decide: https://docs.ansible.com/ansible/2.4/playbooks_reuse.html – nelsonenzo Mar 25 '18 at 21:01
  • @Konstantin Suvorov is right import_tasks is not the same as include_tasks, be careful https://serverfault.com/questions/875247/whats-the-difference-between-include-tasks-and-import-tasks – krad May 09 '18 at 09:44