# How to do Ansible conditional include\_vars?

**URL:** <https://forum.ansible.com/t/how-to-do-ansible-conditional-include-vars/20958>\
**Category:** Ansible Project\
**Created:** [March 22, 2016, 5:55pm UTC](https://forum.ansible.com/t/how-to-do-ansible-conditional-include-vars/20958 "2016-03-22T17:55:04Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Roy](https://avatars.discourse-cdn.com/v4/letter/r/4da419/32.png) [@Roy](https://forum.ansible.com/u/Roy)\
**Post date:** [March 22, 2016, 5:55pm UTC](https://forum.ansible.com/t/how-to-do-ansible-conditional-include-vars/20958/1 "2016-03-22T17:55:04Z")

</div>

Hi,

> I am trying to use `include_vars` based on `when` condition as follows:
> 
> ```auto
> - include_vars: clouderamanager.yml
> when: "'{{ inventory_hostname }}' in groups['hadoop-clouderamanager']"
> 
> - include_vars: hadoop-namenode.yml
> when: "'{{ inventory_hostname }}' in groups['hadoop-namenode']"
> 
> - include_vars: resourcemanager.yml
> when: "'{{ inventory_hostname }}' in groups['hadoop-resourcemanager']"
> 
> - include_vars: spark-history-server.yml
> when: "'{{ inventory_hostname }}' in groups['spark-history-server']"
> 
> ```
> 
> I am getting `ERROR! Syntax Error while loading YAML.`
> 
> ```auto
> The error appears to have been in '~/ansible/roles/hadoop-master/tasks/main.yml': line 4, column 36, but may be elsewhere in the file depending on the exact syntax problem.
> 
> The offending line appears to be:
> 
> - include_vars: clouderamanager.yml
> when: '{{ inventory_hostname }}' in groups['hadoop-clouderamanager']
> ^ here
> 
> ```

What I am missing here ?

Thanks

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [March 22, 2016, 6:57pm UTC](https://forum.ansible.com/t/how-to-do-ansible-conditional-include-vars/20958/2 "2016-03-22T18:57:33Z")

</div>

a) you are using {{}} in when clause  
b) you start a YAML value by a quote but do not end by a quote  
c) you want this:

when: inventory\_hostname in groups[‘hadoop-clouderamanager’]

or

when: “‘hadoop-clouderamanager’ in group\_names”

---

<div class="post-metadata">

**Author:** ![Dick\_Davies](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@Dick\_Davies](https://forum.ansible.com/u/Dick_Davies)\
**Post date:** [March 22, 2016, 9:22pm UTC](https://forum.ansible.com/t/how-to-do-ansible-conditional-include-vars/20958/3 "2016-03-22T21:22:04Z")

</div>

Sorry if I have this wrong - the nested YAML quote thing is hurting my  
eyes - but I think you can do what you want with group\_vars - have a  
look at the docs for that.

---

<div class="post-metadata">

**Author:** ![Roy](https://avatars.discourse-cdn.com/v4/letter/r/4da419/32.png) [@Roy](https://forum.ansible.com/u/Roy)\
**Post date:** [March 23, 2016, 2:50pm UTC](https://forum.ansible.com/t/how-to-do-ansible-conditional-include-vars/20958/4 "2016-03-23T14:50:25Z")

</div>

Hi Brian, thanks for your reply.

I tried your both suggestion now getting another error

for **when: “‘hadoop-namenode’ in group\_names”**

> `
> 
> TASK [hadoop-master : include\_vars] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
> fatal: [[hnn001.dev.abc.com](http://hnn001.dev.abc.com)]: FAILED! =\> {“failed”: true, “msg”: “The conditional check ‘inventory\_hostname in groups[‘hadoop-namenode’]’ failed. The error was: error while evaluating conditional (inventory\_hostname in groups[‘hadoop-namenode’]): Unable to look up a name or access an attribute in template string. Make sure your variable name does not contain invalid characters like ‘-’.\n\nThe error appears to have been in ‘~/ansible/roles/hadoop-master/tasks/main.yml’: line 6, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- include\_vars: namenode.yml\n ^ here\n”}
> 
> `

This is what I have in my namenode.yml

> `
> 
> * * *
> 
> role : hadoop-namenode
> 
> monitoring\_tags : “”
> 
> datadog\_http\_checks:
> 
> - name: Magnetic {{ env }} NameNode  
> url : http://{{ inventory\_hostname }}:50070/dfshealth.jsp  
> threshold : 5  
> window : 5  
> timeout : 10
> 
> `

If I remove the **when: inventory\_hostname in groups[‘hadoop-namenode’]** then it works as expected.

any idea why I am missing here ?

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [March 23, 2016, 3:24pm UTC](https://forum.ansible.com/t/how-to-do-ansible-conditional-include-vars/20958/5 "2016-03-23T15:24:20Z")

</div>

error suggests that the ‘hadoop-namenode’ group does not exist

---

<div class="post-metadata">

**Author:** ![Roy](https://avatars.discourse-cdn.com/v4/letter/r/4da419/32.png) [@Roy](https://forum.ansible.com/u/Roy)\
**Post date:** [March 23, 2016, 4:20pm UTC](https://forum.ansible.com/t/how-to-do-ansible-conditional-include-vars/20958/6 "2016-03-23T16:20:51Z")

</div>

Thanks Brian, It got working after fixing the host group
