{"action":"created","issue":{"url":"https://api.github.com/repos/ansible/ansible/issues/15591","repository_url":"https://api.github.com/repos/ansible/ansible","labels_url":"https://api.github.com/repos/ansible/ansible/issues/15591/labels{/name}","comments_url":"https://api.github.com/repos/ansible/ansible/issues/15591/comments","events_url":"https://api.github.com/repos/ansible/ansible/issues/15591/events","html_url":"https://github.com/ansible/ansible/issues/15591","id":150989146,"number":15591,"title":"Applying \"when\" to roles","user":{"login":"vasilen","id":4251103,"avatar_url":"https://avatars.githubusercontent.com/u/4251103?v=3","gravatar_id":"","url":"https://api.github.com/users/vasilen","html_url":"https://github.com/vasilen","followers_url":"https://api.github.com/users/vasilen/followers","following_url":"https://api.github.com/users/vasilen/following{/other_user}","gists_url":"https://api.github.com/users/vasilen/gists{/gist_id}","starred_url":"https://api.github.com/users/vasilen/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/vasilen/subscriptions","organizations_url":"https://api.github.com/users/vasilen/orgs","repos_url":"https://api.github.com/users/vasilen/repos","events_url":"https://api.github.com/users/vasilen/events{/privacy}","received_events_url":"https://api.github.com/users/vasilen/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":6,"created_at":"2016-04-25T21:42:59Z","updated_at":"2016-04-26T00:54:18Z","closed_at":"2016-04-25T21:50:18Z","body":"<!--- Verify first that your issue/request is not already reported in GitHub -->\r\n\r\n##### ISSUE TYPE\r\n<!--- Pick one below and delete the rest: -->\r\n - Bug Report\r\n\r\n\r\n\r\n##### ANSIBLE VERSION\r\n```\r\n“ansible 2.0.0.2” \r\n```\r\nbuilt from source with tag v2.0.2.0-1\r\n\r\n##### CONFIGURATION\r\nDefault configuration, ansible.cfg untouched \r\n\r\n##### OS / ENVIRONMENT\r\n\"N/A\"\r\n\r\n##### SUMMARY\r\nUsing role with the \"when\" conditional seems to still evaluate each task in the role when the conditional is not true.  This behavior is to be expected based on the documentation \"http://docs.ansible.com/ansible/playbooks_conditionals.html#applying-when-to-roles-and-includes\"\r\nWhat is not expected is that it fails with \" 'blank' is undefined\" on one of the tasks it evaluates within the role it is supposed to skip.\r\n\r\n\r\n##### STEPS TO REPRODUCE\r\nRun the playbook listed below with this command:\r\n```ansible-playbook -i hosts -c local compute-1 test.yml```\r\n\r\n\r\ntest.yml (TorqueClient is not defined for compute-1)\r\n```\r\n---\r\n# playbook to build an compute node\r\n- hosts: all\r\n  roles:\r\n    - iptables\r\n    - { role: torque-common, when: TorqueClient is defined }\r\n```\r\n\r\nrole/torque-common/tasks/main.yml\r\n```\r\n---\r\n# tasks file for roles/torque-common\r\n- name: Set OS dependent variables\r\n  include_vars: \"{{ item }}\"\r\n  with_first_found:\r\n    -  \"{{ ansible_distribution }}_{{ ansible_distribution_major_version }}.yml\"\r\n    -  main.yml\r\n\r\n- name: Install rpm package dependecies\r\n  yum: state=present name=\"{{ item }}\"\r\n  with_items:\r\n   - hwloc\r\n   - hwloc-devel\r\n   - hwloc-libs\r\n\r\n- name: Install the torque clients pkg\r\n  command: \"{{ torque_pkg_path }}/torque-package-clients-linux-x86_64.sh --install\"\r\n  tags:\r\n    - client\r\n\r\n- name: Install the torque devel pkg\r\n  command: \"{{ torque_pkg_path }}/torque-package-devel-linux-x86_64.sh --install\"\r\n  tags:\r\n    - devel\r\n\r\n- name: Install the torque doc pkg\r\n  command: \"{{ torque_pkg_path }}/torque-package-doc-linux-x86_64.sh --install\"\r\n  tags:\r\n    - doc\r\n\r\n- name: Deploy trqauthd service script, torque.conf ld file, server_name, and torque.sh profile.d\r\n  template:\r\n    src: \"{{ item.name }}\"\r\n    dest: \"{{ item.destName }}\"\r\n    owner: root\r\n    mode: \"{{ item.modeState }}\"\r\n  with_items:\r\n    - { name: 'torque.conf.j2', destName: '/etc/ld.so.conf.d/torque.conf', modeState: '0644' }\r\n    - { name: \"{{ trqauthd_src }}\", destName: '{{ service_path }}/{{ trqauthd_dest }}', modeState: '0755' }\r\n    - { name: 'torque.sh.j2' , destName: '/etc/profile.d/torque.sh', modeState: '0755' }\r\n    - { name: 'torque.csh.j2' , destName: '/etc/profile.d/torque.csh', modeState: '0755' }\r\n    - { name: 'server_name.j2' , destName: \"{{ torque_path }}/server_name\", modeState: '0755' }\r\n```\r\n##### EXPECTED RESULTS\r\nI expect that the torque-common role as listed above would be skipped.  The first task in the torque-common role loads the variable definitions that are used in the rest of the task.  Since the role is supposed to be skipped, it skips the tasks as expected until it reaches the task that uses the \"template\" module and errors out with \"is undefined\" .  If it was going to fail do to \"is undefined\" it should have failed the first time it came across a variable, as in the first task that uses the \"command\" module with a variable.\r\n\r\n##### ACTUAL RESULTS\r\n<!--- What actually happened? If possible run with high verbosity (-vvvv) -->\r\n\r\n```\r\nPLAY ***************************************************************************\r\n\r\nTASK [setup] *******************************************************************\r\nok: [compute-1]\r\n\r\nTASK [iptables : Make sure iptables is installed, and remove firewalld] ********\r\nok: [compute-1] => (item={u'state': u'present', u'name': u'iptables'})\r\nok: [compute-1] => (item={u'state': u'present', u'name': u'iptables-services'})\r\nok: [compute-1] => (item={u'state': u'absent', u'name': u'firewalld'})\r\n\r\nTASK [iptables : Copy iptables configuration] **********************************\r\nok: [compute-1]\r\n\r\nTASK [iptables : Restart iptables service] *************************************\r\nskipping: [compute-1]\r\n\r\nTASK [torque-common : Set OS dependent variables] ******************************\r\nskipping: [compute-1] => (item=/etc/ansible-buildtime/ansible/roles/torque-common/vars/CentOS_7.yml) \r\n\r\nTASK [torque-common : Install rpm package dependecies] *************************\r\nskipping: [compute-1] => (item=[]) \r\n\r\nTASK [torque-common : Install the torque clients pkg] **************************\r\nskipping: [compute-1]\r\n\r\nTASK [torque-common : Install the torque devel pkg] ****************************\r\nskipping: [compute-1]\r\n\r\nTASK [torque-common : Install the torque doc pkg] ******************************\r\nskipping: [compute-1]\r\n\r\nTASK [torque-common : Deploy trqauthd service script, torque.conf ld file, server_name, and torque.sh profile.d] ***\r\nfatal: [compute-1]: FAILED! => {\"failed\": true, \"msg\": \"ERROR! 'service_path' is undefined\"}\r\n\r\nPLAY RECAP *********************************************************************\r\ncompute-1                    : ok=3    changed=0    unreachable=0    failed=1\r\n```\r\n"},"comment":{"url":"https://api.github.com/repos/ansible/ansible/issues/comments/214575322","html_url":"https://github.com/ansible/ansible/issues/15591#issuecomment-214575322","issue_url":"https://api.github.com/repos/ansible/ansible/issues/15591","id":214575322,"user":{"login":"vasilen","id":4251103,"avatar_url":"https://avatars.githubusercontent.com/u/4251103?v=3","gravatar_id":"","url":"https://api.github.com/users/vasilen","html_url":"https://github.com/vasilen","followers_url":"https://api.github.com/users/vasilen/followers","following_url":"https://api.github.com/users/vasilen/following{/other_user}","gists_url":"https://api.github.com/users/vasilen/gists{/gist_id}","starred_url":"https://api.github.com/users/vasilen/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/vasilen/subscriptions","organizations_url":"https://api.github.com/users/vasilen/orgs","repos_url":"https://api.github.com/users/vasilen/repos","events_url":"https://api.github.com/users/vasilen/events{/privacy}","received_events_url":"https://api.github.com/users/vasilen/received_events","type":"User","site_admin":false},"created_at":"2016-04-26T00:54:18Z","updated_at":"2016-04-26T00:54:18Z","body":"> This is also expected, as when: evaluates AFTER with_ as it allows conditional execution per item. You might want to use the |default filter to avoid \r\n\r\nI understand the concept of how things are evaluated, and it makes sense if I have a ```when``` and ```with_``` in  a playbook.  \r\n\r\nCorrect me if I am wrong, so the process of  ```when:``` in a playbook as a conditional to call a role is then passed into the role and used to evaluate each individual task.  I would expect that the role just gets skipped and not evaluated task by task within the role.\r\n\r\nAm I using the role with ```when:``` incorrectly or should I put in a feature request?  Ultimately I would like to have a playbook and roles only run ```when:``` conditional is met or not met."}}