0

I have the following includes in a old role by a colleague.

---
- name: deploy
  include: deploy.yml
  when: deploy is defined and  deploy == 'True'

- name: undeploy
  include: undeploy.yml
  when: undeploy is defined and undeploy == 'True'

- name: database-migrate
  include: database-migrate.yml
  when: db is defined and db == 'True'

However, irrespective of how I include the role Ansible is automatically processing each include. I can verify this by using the --list-tasks option.

For example in my playbook I have the following

  roles:
    - { role: vip-notification-services-app, deploy: 'True', tags: ['deploy']}

I running with version 2.1.1.0 (upgraded a few weeks back). This role was executing fine before the upgrade.

Therefore I'm wondering if this style of includes in a role no longer allowed or there is some different syntax I need to use.

techraf
  • 64,883
  • 27
  • 193
  • 198
Carl Wainwright
  • 328
  • 5
  • 18
  • 1
    See my [answer](http://stackoverflow.com/a/39856625/2795592) to another question (about how "conditional" includes work). – Konstantin Suvorov Oct 07 '16 at 17:30
  • @KonstantinSuvorov That answer does not apply to this situation. – MillerGeek Oct 07 '16 at 18:14
  • I notice you are saying that the values need to evaluate to the string `'True'` instead of the boolean `True`. Is this intentional? Are you perhaps defining any of these in a vars or a defaults file, or defining them somewhere before these tasks are run? – MillerGeek Oct 07 '16 at 18:14
  • @smiller171 It does apply to this situation. – techraf Oct 09 '16 at 01:32
  • So I wrote a small example containing a site.yml and two playbooks. Each playbook uses the the same role, but with different variable definitions. The results show that the correct includes are included based on which variable is defined. Therefore, I need to investigate how these variables are getting defined elsewhere in my project to produce the results I am getting. – Carl Wainwright Oct 09 '16 at 21:28

1 Answers1

1

The --list-tasks option it does not evaluate the when conditional for its results.

That is, if you have a playbook.yml:

---
- hosts: localhost
  connection: local
  tasks:
    - debug:
      when: false

ansible-playbook playbook.yml --list-tasks will display:

playbook: test.yml

  play #1 (localhost): localhost    TAGS: []
    tasks:
      debug TAGS: []

although the debug task would never run (the result will not change if you change the condition to with: true).

As Konstantin Suvorov noticed, include always includes all tasks and then applies when conditional to each task. Thus in --list-tasks result you will always see all tasks. They won't however be executed in a real run.

techraf
  • 64,883
  • 27
  • 193
  • 198
  • Thanks for the explanation of the --list-tasks option. I wrote a small test playbook/role and confirmed that the when clause is ignored. – Carl Wainwright Oct 09 '16 at 21:22