Issue 1:
ISSUE TYPE

Bug Report

COMPONENT NAME

module include_tasks

ANSIBLE VERSION

ansible 2.4.1.0
config file = /home/xxxx/.ansible.cfg
configured module search path = [u'/home/xxxx/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /opt/xxxx/venv/ansible2.4-python2.7/lib/python2.7/site-packages/ansible
executable location = /opt/xxxx/venv/ansible2.4-python2.7/bin/ansible
python version = 2.7.13 (default, Apr 12 2017, 06:53:51) [GCC 4.4.7 20120313 (Red Hat 4.4.7-18)]

CONFIGURATION

ANSIBLE_PIPELINING(/home/xxxx/.ansible.cfg) = True
DEFAULT_HOST_LIST(/home/xxxx/.ansible.cfg) = [u'/home/xxxx/checkouts/ansible-tree/inventory']
DEFAULT_LOG_PATH(/home/xxxx/.ansible.cfg) = /home/xxxx/ansible.log
HOST_KEY_CHECKING(/home/xxxx/.ansible.cfg) = False
INVENTORY_IGNORE_EXTS(/home/xxxx/.ansible.cfg) = ['~', '.orig', '.bak', '.ini', '.cfg', '.retry', '.pyc', '.pyo', '.txt', '.back', 'script']
MAX_FILE_SIZE_FOR_DIFF(/home/xxxx/.ansible.cfg) = 4194304
RETRY_FILES_SAVE_PATH(/home/xxxx/.ansible.cfg) = /home/xxxx

OS / ENVIRONMENT

Centos 6

SUMMARY

When you add a when condition to include_tasks which includes a undefined variable, it processes it as True instead of trowing an undefined error.

STEPS TO REPRODUCE

Do not define or set VAR

- include_tasks: task.yml
when: VAR == "yes"

- debug: msg="test"
when: VAR == "yes"

EXPECTED RESULTS

include_tasks should fail with "'VAR' is undefined"

ACTUAL RESULTS

Include_tasks just gets executed and debug message fails with "'VAR' is undefined"

Issue 2:
ISSUE TYPE

Bug Report

COMPONENT NAME

include_tasks

ANSIBLE VERSION

devel version on 2018/03/24
2.6.0-100.git201803241800.d502745a4e.devel

CONFIGURATION

standart

OS / ENVIRONMENT

centos 7.4

SUMMARY

include_tasks: i.yml

when: undefinded_variable == ''

when condition with an undefined variable not raise error - it is always true !

STEPS TO REPRODUCE

role with two files:

main.yml

- debug: msg="{{test_undef_var}}"
ignore_errors: yes

- debug: msg="{{test_undef_var}}"
when:
- test_undef_var == ''
ignore_errors: yes

- debug: msg="{{ansible_user}}"

- name: "test include_task {{test_undef_var}}"
include_tasks: test_include.yml
when:
- test_undef_var != ''
- ansible_user == "bond"

test_include.yml

- debug: msg="{{ansible_user}}"

- debug: msg="{{test_undef_var}}"
ignore_errors: yes

playbook.yml

- hosts:
- all
vars:
- ansible_ssh_user: bond
roles:
- test

and run:

ansible-playbook -i 192.168.100.71, -k --diff test.yml

EXPECTED RESULTS

playbook should fail on include_task with message test_undef_var is undefined

ACTUAL RESULTS

task included without error

SSH password:

PLAY [all] *************************************************************************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************************************************************
ok: [192.168.100.71]

TASK [test : debug] ****************************************************************************************************************************************************
fatal: [192.168.100.71]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'test_undef_var' is undefined\n\nThe error appears to have been in '/home/robot/test1/roles/test/tasks/main.yml': line 1, 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- debug: msg=\"{{test_undef_var}}\"\n ^ here\nWe could be wrong, but this one looks like it might be an issue with\nmissing quotes. Always quote template expression brackets when they\nstart a value. For instance:\n\n with_items:\n - {{ foo }}\n\nShould be written as:\n\n with_items:\n - \"{{ foo }}\"\n"}
...ignoring

TASK [test : debug] ****************************************************************************************************************************************************
fatal: [192.168.100.71]: FAILED! => {"msg": "The conditional check 'test_undef_var == ''' failed. The error was: error while evaluating conditional (test_undef_var == ''): 'test_undef_var' is undefined\n\nThe error appears to have been in '/home/robot/test1/roles/test/tasks/main.yml': line 4, 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- debug: msg=\"{{test_undef_var}}\"\n ^ here\nWe could be wrong, but this one looks like it might be an issue with\nmissing quotes. Always quote template expression brackets when they\nstart a value. For instance:\n\n with_items:\n - {{ foo }}\n\nShould be written as:\n\n with_items:\n - \"{{ foo }}\"\n"}
...ignoring

TASK [test : debug] ****************************************************************************************************************************************************
ok: [192.168.100.71] => {
"msg": "bond"
}

TASK [test : test include_task {{test_undef_var}}] *********************************************************************************************************************
included: /home/robot/test1/roles/test/tasks/test_include.yml for 192.168.100.71

TASK [test : debug] ****************************************************************************************************************************************************
ok: [192.168.100.71] => {
"msg": "bond"
}

TASK [test : debug] ****************************************************************************************************************************************************
fatal: [192.168.100.71]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'test_undef_var' is undefined\n\nThe error appears to have been in '/home/robot/test1/roles/test/tasks/test_include.yml': line 3, 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- debug: msg=\"{{test_undef_var}}\"\n ^ here\nWe could be wrong, but this one looks like it might be an issue with\nmissing quotes. Always quote template expression brackets when they\nstart a value. For instance:\n\n with_items:\n - {{ foo }}\n\nShould be written as:\n\n with_items:\n - \"{{ foo }}\"\n"}
...ignoring

PLAY RECAP *************************************************************************************************************************************************************
192.168.100.71 : ok=7 changed=0 unreachable=0 failed=0

i try with == task included

- name: "test include_task {{test_undef_var}}"
include_tasks: test_include.yml
when:
- test_undef_var == ''
- ansible_user == "bond"

include task skipped only when trye anisble_user == 'nobond'

- name: "test include_task {{test_undef_var}}"
include_tasks: test_include.yml
when:
- test_undef_var == ''
- ansible_user == "nobond"