Trouble with using an include based on conditional

This is being copied from the bug that just got closed:

ANSIBLE VERSION
ansible 2.2.1.0
 
CONFIGURATION

Nothing weird here

OS / ENVIRONMENT

MAC Sierra 10.12.3
Destination: CentOS 7

SUMMARY

Using a conditional with the include for a task include is ignored.
I have run it using static: no and without - the results are the same.

I am running this play for a web server which is defined as modef-prod-web.
There are alert servers which run as mozdef-prod-alert.

Running this for mozdef-prod-web should essentially only include the mozdef-web.yml - and no other includes. But it doesn’t seem to check the conditional.

Inventory List

Inventory list:
[mozdef-prod-web]
192.168.250.134

[mozdef-prod-alert]

192.168.250.135

mozdef-update.yml:

  • hosts: mozdef-prod-web
    remote_user: phrozyn
    become: yes
    become_user: someuser
    roles:
    • mozdef-update
- include: mozdef-alert.yml
  static: no
  when: ("'mozdef-prod-alert' in group_names") or ("'mozdef-dev' in group_names") or ("'mozdef-gen2-qa' in group_names")

- include: mozdef-consumer.yml
  static: no
  when: ("'mozdef-prod-ingest' in group_names") or ("'mozdef-dev' in group_names") or ("'mozdef-gen2-qa' in group_names")

- include: mozdef-web.yml
  static: no
  when: ("'mozdef-prod-web' in group_names") or ("'mozdef-dev' in group_names") or ("'mozdef-gen2-qa' in group_names")

- include: mozdef-qa.yml
  static: no
  when: ("'mozdef-gen2-qa' in group_names") or ("'mozdef-dev' in group_names")

 
EXPECTED RESULTS

TASK [mozdef-update : include] *************************************************
task path: /Users/Phrozyn/Projects/mozdef-dev/QA/ansible-eis/roles/mozdef-update/tasks/main.yml:45
included: /Users/Phrozyn/Projects/mozdef-dev/QA/ansible-eis/roles/mozdef-update/tasks/mozdef-web.yml for 192.168.250.134

ACTUAL RESULTS

TASK [mozdef-update : include] *************************************************
task path: /Users/Phrozyn/Projects/mozdef-dev/QA/ansible-eis/roles/mozdef-update/tasks/main.yml:45
included: /Users/Phrozyn/Projects/mozdef-dev/QA/ansible-eis/roles/mozdef-update/tasks/mozdef-alert.yml for 192.168.250.134

It of course dies when it runs because the webserver’s group_vars don’t include variables for the alert tasks.

So any help would be appreciated!

Keep in mind, this fails whether or not static: no is designated

Since you are not showing a play, I'm going to guess that you are
doing play includes, which cannot be made dynamic. ONLY task includes
can, as the docs state:
http://docs.ansible.com/ansible/include_module.html