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"

On Monday, November 21, 2016 at 10:02:28 PM UTC-8, Thiago Martins wrote:
>
> 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:
>
>  http://docs.ansible.com/ansible/include_role_module.html
>
>  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
>

-- 
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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/8a9c2d7d-6045-4ee0-aa9f-6e2db59cafd5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to