Ansible when statements NOT WORKING when checking the ansible_os_family fact

591 views
Skip to first unread message

vma...@gigware.com

unread,
Apr 15, 2016, 12:57:11 PM4/15/16
to Ansible Project
Ok so I have this code(using version 2.0.1.0 of ansible):
<pre>
---
- hosts: localhost
  become: yes
  gather_facts: yes
  vars:
    version: "6.5.5"

  vars_files:
    - vars/{{ ansible_os_family }}.yml

  roles:
    - role: ubuntu_packages
      tags: ubuntu_packages
      when: ansible_os_family == "Debian"

    - role: redhat_packages
      tags: redhat_packages
      when: ansible_os_family == "RedHat"

    - role: windows_packages
      tags: windows_packages
      when: ansible_os_family == "Windows"
</pre>

But when I run it using ansible-playbook foo.yml, Im getting:
<pre>
PLAY ***************************************************************************

TASK [setup] *******************************************************************
ok: [localhost]

TASK [ubuntu_packages : include_vars] ******************************************
ok: [localhost] => (item=/root/scanner/ubuntu_packages/vars/Ubuntu_14.yml)

TASK [ubuntu_packages : Gather_ec2_facts] **************************************
ok: [localhost]

TASK [ubuntu_packages : debug] *************************************************
ok: [localhost] => {
    "file_url_full": "VARIABLE IS NOT DEFINED!"
}

TASK [redhat_packages : include_vars] ******************************************
fatal: [localhost]: FAILED! => {"failed": true, "msg": "No file was found when using with_first_found. Use the 'skip: true' option to allow this task to be skipped if no files are found"}

NO MORE HOSTS LEFT *************************************************************
        to retry, use: --limit @scanner.retry

PLAY RECAP *********************************************************************
localhost                  : ok=4    changed=0    unreachable=0    failed=1

</pre>

In other words the "when" statement is simply ignored when chosing the role and it executes the RedHat role right after the Ubuntu role although it shouldn't since ansible_os_family is Debian and if the when statement would work then there should be no error here. Seems like a serious bug in ansible.

Note: Tests are being done on ubuntu 14.04

Johannes Kastl

unread,
Apr 15, 2016, 1:10:22 PM4/15/16
to ansible...@googlegroups.com
On 15.04.16 18:54 vma...@gigware.com wrote:

> - role: windows_packages
> tags: windows_packages
> when: ansible_os_family == "Windows"

I guess you just need to use "include" instead of "role:", then the
when-statement should work...

Johannes

signature.asc

Brian Coca

unread,
Apr 15, 2016, 1:46:44 PM4/15/16
to ansible...@googlegroups.com
Conditionals are working as intended, it is not possible to make 'roles' conditional, any when: clause will be added to the tasks of the role, but do NOT prevent the import of the role itself.

----------
Brian Coca
Reply all
Reply to author
Forward
0 new messages