Conditional role "include_role:" doesn't work (with "when:"), neither with "with_items".

1,916 views
Skip to first unread message

Thiago Martins

unread,
Nov 22, 2016, 1:02:28 AM11/22/16
to Ansible Project
Guys,

 I'm trying to create a top-level playbook, based on "include_role:", instead of "roles:", however, the "when:" condition isn't working properly, I think.

 Easy to reproduce (the content of "roles/hello-world-X/tasks/main.yml" is in the end):

 This works ("include_role:" without the "when:" condition):

$ ansible --version
ansible 2.2.0.0

$ source ~/my-openstack-project-openrc.sh

$ ansible-playbook -i openstack.py hello.yml

---
- hosts: all
  remote_user: ubuntu
  become: yes
  tasks:
    - include_role: name=hello-world-1
---

 Awesome!

 However, if I add the "when:" condition:

---
- hosts: all
  remote_user: ubuntu
  become: yes
  tasks:
    - include_role: name=hello-world-1
      when: ansible_hostname == 'stack-1-web-1'
---

 Then, it tails! With the following error:

---
fatal: [1872cd92-84cf-4ad5-9f1a-52d3b79a5940]: FAILED! => {"failed": true, "msg": "The conditional check 'a' failed. The error was: error while evaluating conditional (a): 'a' is undefined\n\nThe error appears to have been in '/home/tmartins/project/ansible/hello.yml': line 5, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n  tasks:\n    - include_role: name=hello-world-1\n      ^ here\n"}
---

 What's wrong? Ansible bug?

 I tried a "more simple" condition (when: ansible_os_family == "Debian"), same error.

 My "when:" condition is right, I can test it, by replacing the "include_role:", by "ping:", like this:

---
- hosts: all
  remote_user: ubuntu
  become: yes
  tasks:
    - ping:
      when: ansible_hostname == 'stack-1-web-1'
---

 It works...

 If I also apply the when condition to a "-role:", then, it works as well, like this:

---
- hosts: all
  remote_user: ubuntu
  become: yes
  roles:
    - { role: hello-world-1, when: ansible_hostname == 'stack-1-web-1' }
    - { role: hello-world-2, when: ansible_hostname == 'stack-1-sql-1' }
---

 Works, good but, as I said before, I want to use "include_role:", instead of "roles:", mostly because of its (broken?) support for "when:" condition.

 Aaaand, I can't use "when:" under "roles:", something about: "ERROR! 'when' is not a valid attribute for a Play", if I try "when:" under "roles:".

 I also noted that "include_roles:" doesn't work with "with_items", this:

---
- hosts: all
  remote_user: ubuntu
  become: yes
  tasks:
    - include_role: name={{item}}
      with_items:
         - hello-world-1
         - hello-world-2
---

 Returns:

 ERROR! 'item' is undefined

 :-(

 My goal is something like this:

---
- hosts: all
  remote_user: ubuntu
  become: yes
  tasks:

    - include_role: name={{item}}
      with_items:
        - hello-world-1
        - hello-world-2
      when: ansible_hostname | search("stack-1-web.*")

    - include_role: name={{item}}
      with_items:
        - hello-world-3
        - hello-world-4
      when: ansible_hostname | search("stack-1-sql.*")
---

 Is it even possible? Apparently, only the "with_items" with "include_role" isn't really documented here:


 I manage to use "include_role:" only without "when:" or "items", for example:

---
- hosts: all
  remote_user: ubuntu
  become: yes
  tasks:
    - include_role: name=hello-world-1
    - include_role: name=hello-world-2
    - include_role: name=hello-world-3
    - include_role: name=hello-world-4
---

 NOTE: Here is the contents of "roles/hello-world-X/tasks/main.yml":

---
- name: Test connection
  ping:

- debug: msg={{ ansible_hostname }}
---

 BTW, the "when: ansible_hostname | search("stack-1-web.*")" works like a charm, quite awesome! I can easily detect all my "stack-1-web|sql-X" instances without using [web|sql-groups], all my instances are under the same group "[all]", I also have an inventory file with "[all:vars]" and that's it. At least, this is what I'm trying to do...     =P

 Ansible FTW, cheers!

Thanks,
Thiago

Lorrin Nelson

unread,
Nov 28, 2016, 7:05:56 PM11/28/16
to Ansible Project
I am also seeing when: not work with include_role. The "a" reported above is just the first letter of Thiago's condition (ansible_hostname | search("stack-1-sql.*"). I've got a boolean, etcd_enabled, so my when: etcd_enabled produces "The conditional check 'e' failed"

Mario Wolff

unread,
Jan 12, 2017, 3:05:30 PM1/12/17
to Ansible Project
Is there something new on this topic?

I'm using some Galaxy roles and i would like to avoid any modification. Some of these roles do much more than i need and i would like to do something like:

    - name: selective tasks from galaxy role
      include_role:
        name: someones.role
        tasks_from: "{{ item }}"
        static: no
      with_items:
          - prereq.yml
          - installa.yml
          - starta.yml
          - testa.yml

Where the role has also install[b-d].yml included from the main.yml

Here i get the already known 

ERROR! Could not find specified file in role: {{ item }}

Regards,
Mario 

BTW: $ ansible --version
ansible 2.2.0.0


Hany Fahim

unread,
Mar 19, 2017, 6:29:48 PM3/19/17
to Ansible Project
I'm getting the exact same issue. When using "when" with "include_role", I get:

The conditional check 'g' failed. The error was: error while evaluating conditional (g): 'g' is undefined
Reply all
Reply to author
Forward
0 new messages