How to do Ansible conditional include_vars?

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

Answers

Changing when condition following worked

when: inventory_hostname in groups['hadoop-clouderamanager']

Posted on by roy