Not sure why this when: is not working.

1,627 views
Skip to first unread message

Steven Smith

unread,
Nov 16, 2016, 12:06:49 PM11/16/16
to Ansible Project
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?

Alexander H. Laughlin

unread,
Nov 16, 2016, 4:53:37 PM11/16/16
to Ansible Project

---

- name: Test when functionality.

 hosts: all

 tasks:

   - name: Output inventory_hostname.

     debug:

       msg: "The inventory hostname is {{ inventory_hostname }}."

   - 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

...

# vim: set ft=ansible:



The syntax for your when statement is incorrect. As noted elsewhere support for bare variables is deprecated, so you should not be using them anywhere.

The playbook included above shows the correct syntax. Hope that helps.

Best of luck!

Alex

Brian Coca

unread,
Nov 16, 2016, 7:44:38 PM11/16/16
to ansible...@googlegroups.com
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.

     when: ("{{ inventory_hostname }}" in groups["web"]) or ("{{ inventory_hostname }}" in groups["web_backend"])

     ignore_errors: true

     notify: restart httpd

...

# vim: set ft=ansible:


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

Kai Stian Olstad

unread,
Nov 17, 2016, 12:45:47 AM11/17/16
to ansible...@googlegroups.com
On 15. nov. 2016 16:53, Steven Smith wrote:
> 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
>
> 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?

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([]))

--
Kai Stian Olstad
Reply all
Reply to author
Forward
0 new messages