How to do Ansible conditional include_vars?

695 views
Skip to first unread message

Roy

unread,
Mar 22, 2016, 1:55:04 PM3/22/16
to Ansible Project
Hi,


I am trying to use include_vars based on when condition as follows:

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

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 

Brian Coca

unread,
Mar 22, 2016, 2:57:44 PM3/22/16
to ansible...@googlegroups.com
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"

----------
Brian Coca

Dick Davies

unread,
Mar 22, 2016, 5:22:10 PM3/22/16
to ansible list
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.
> --
> You received this message because you are subscribed to the Google Groups
> "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ansible-proje...@googlegroups.com.
> To post to this group, send email to ansible...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/e8963782-dc02-46b5-98b2-3833312b64c2%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Roy

unread,
Mar 23, 2016, 10:50:25 AM3/23/16
to Ansible Project
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]: 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 ?

Brian Coca

unread,
Mar 23, 2016, 11:24:28 AM3/23/16
to ansible...@googlegroups.com
error suggests that the 'hadoop-namenode' group does not exist




----------
Brian Coca

Roy

unread,
Mar 23, 2016, 12:20:51 PM3/23/16
to Ansible Project
Thanks Brian, It got working after fixing the host group 
Reply all
Reply to author
Forward
0 new messages