Not sure why this when: is not working.

This is the play I am trying to get to run:

  • name: Install httpd
    yum: name=httpd state=latest disable_gpg_check=yes
    when: (inventory_hostname in groups[“web”]) or (inventory_hostname in groups[“web_backend”])
    ignore_errors: true
    notify: restart httpd

I get this error:

fatal: [rhel7]: FAILED! => {“failed”: true, “msg”: “The conditional check ‘(inventory_hostname in groups["web"]) or (inventory_hostname in groups["web_backend"])’ failed. The error was: error while evaluating conditional ((inventory_hostname in groups["web"]) or (inventory_hostname in groups["web_backend"])): Unable to look up a name or access an attribute in template string ({% if (inventory_hostname in groups["web"]) or (inventory_hostname in groups["web_backend"]) %} True {% else %} False {% endif %}).\nMake sure your variable name does not contain invalid characters like ‘-’: argument of type ‘StrictUndefined’ is not iterable\n\nThe error appears to have been in ‘/etc/ansible/roles/httpd/tasks/enabled.yml’: line 14, 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- name: Install httpd\n ^ here\n”}

If my hosts file has nothing at all for “web” or “web_backend” I am hoping it would just skip over this entire play instead of throwing this big red error.

If either one of those are defined, then I want this play to run… can anyone please help?

`

This is wrong, when: are always templated and you should not use {{ }}, if what you say were true groups wouldn’t work either as they are also a variable. You are actually doing double interpolation, that is why you need the quotes.

All variable that is not defined you'll need to filter through the default filter, try this.

when: (inventory_hostname in groups["web"] | default()) or (inventory_hostname in groups["web_backend"] | default())