Issue 1:
ISSUE TYPE

Bug Report

COMPONENT NAME

include_role module

ANSIBLE VERSION

ansible 2.4.0.0
config file = /home/esio/work/ansible/ansible.cfg
configured module search path = [u'/home/esio/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python2.7/site-packages/ansible
executable location = /usr/bin/ansible
python version = 2.7.13 (default, Sep 5 2017, 08:53:59) [GCC 7.1.1 20170622 (Red Hat 7.1.1-3)]

CONFIGURATION

ANSIBLE_NOCOWS(/home/esio/work/ansible/ansible.cfg) = True
DEFAULT_BECOME_EXE(/home/esio/work/ansible/ansible.cfg) = sudo su -
DEFAULT_BECOME_METHOD(/home/esio/work/ansible/ansible.cfg) = su
DEFAULT_HASH_BEHAVIOUR(/home/esio/work/ansible/ansible.cfg) = merge
DEFAULT_HOST_LIST(/home/esio/work/ansible/ansible.cfg) = [u'/home/esio/work/ansible/inventories/test/hosts']
DEFAULT_REMOTE_TMP(/home/esio/work/ansible/ansible.cfg) = /tmp/.ansible-${USER}/tmp # workaround become permission issues
DEFAULT_ROLES_PATH(/home/esio/work/ansible/ansible.cfg) = [u'/home/esio/work/ansible/roles', u'/home/esio/work/ansible/vendor']
HOST_KEY_CHECKING(/home/esio/work/ansible/ansible.cfg) = False
RETRY_FILES_SAVE_PATH(/home/esio/work/ansible/ansible.cfg) = /home/esio/work/ansible/.retryfiles

OS / ENVIRONMENT

Fedora 26 x86_64

SUMMARY

When I use include_role ansible doesn't apply default variables.

STEPS TO REPRODUCE

I have directories and files in my ansible config:

└── roles
├── r1
│ └── tasks
│ └── main.yml
└── r2
├── defaults
│ └── main.yml
└── tasks
└── main.yml

In roles/r1/tasks/main.yml

- name: Run role r2
include_role:
name: gpdw.deploy-hdfs-component
vars:
artifact_version: "{{ x[2] }}"
artifact_id: "{{ x[1] }}"
with_list: "{{ list }}"
loop_control:
loop_var: x

In roles/r2/tasks/main.yml

- name: debug
debug:
msg: "{{ artifact_extension }}

In roles/r2/defaults/main.yml

artifact_extension: "tar.gz"

EXPECTED RESULTS

It should work and print tar.gz as debug.

ACTUAL RESULTS

Ansible fails with error, that variable artifact_extension is undefined. In my opinion ansible should use variables from defaults/main.yml file in included role.

fatal: [ap-hdpen1t]: FAILED! => {
"msg": "The task includes an option with an undefined variable. The error was: 'artifact_extension' is undefined\n\nThe error appears to have been in '/home/esio/work/ansible/roles/gpdw.deploy-dq-all/tasks/main.yml': line 29, 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- name: debug\n ^ here\n\nexception type: <class 'ansible.errors.AnsibleUndefinedVariable'>\nexception: 'artifact_extension' is undefined"
}

Issue 2:
ISSUE TYPE

Feature Idea

COMPONENT NAME

include_role

ANSIBLE VERSION

ansible 2.2.1.0
config file = /etc/ansible/ansible.cfg
configured module search path = Default w/o overrides

CONFIGURATION

None.

OS / ENVIRONMENT

OS: CentOS7, but shouldn't matter.

SUMMARY

When including a role using include_role its vars do not become available to the rest of the playbook. This does work when using the regular roles definition.

STEPS TO REPRODUCE

A minimal test-case lives here: https://github.com/wouterhund/demo-ansible-include_role-bug/blob/master/bad-playbook.yml https://github.com/wouterhund/demo-ansible-include_role-bug/blob/master/roles/testrole/vars/main.yml

The actual code that triggers the issue:

---
- hosts: localhost

tasks:
- include_role:
name: testrole
- debug: msg="{{some_var}}"

roles/testrole/vars/main.yml

---
some_var: "Hello world!"

EXPECTED RESULTS

I would expect good-playbook.yml and bad-playbook.yml from the linked github to work identically, however bad-playbook.yml fails. I expect variables defined within an included role to be made available to the rest of the playbook.

For the example above I'd expect it to pass and display "Hello world!"

ACTUAL RESULTS

» ansible-playbook -vvvv bad-playbook.yml 1 ↵
Using /etc/ansible/ansible.cfg as config file
[WARNING]: provided hosts list is empty, only localhost is available

Loading callback plugin default of type stdout, v2.0 from /usr/lib/python2.7/site-packages/ansible/plugins/callback/__init__.pyc

PLAYBOOK: bad-playbook.yml *****************************************************
1 plays in bad-playbook.yml

PLAY [localhost] ***************************************************************

TASK [setup] *******************************************************************
Using module file /usr/lib/python2.7/site-packages/ansible/modules/core/system/setup.py
<127.0.0.1> ESTABLISH LOCAL CONNECTION FOR USER: vagrant
<127.0.0.1> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo ~/.ansible/tmp/ansible-tmp-1487936470.27-110880382737009 `" && echo ansible-tmp-1487936470.27-110880382737009="` echo ~/.ansible/tmp/ansible-tmp-1487936470.27-110880382737009 `" ) && sleep 0'
<127.0.0.1> PUT /tmp/tmp017eF_ TO /home/vagrant/.ansible/tmp/ansible-tmp-1487936470.27-110880382737009/setup.py
<127.0.0.1> EXEC /bin/sh -c 'chmod u+x /home/vagrant/.ansible/tmp/ansible-tmp-1487936470.27-110880382737009/ /home/vagrant/.ansible/tmp/ansible-tmp-1487936470.27-110880382737009/setup.py && sleep 0'
<127.0.0.1> EXEC /bin/sh -c '/usr/bin/python2 /home/vagrant/.ansible/tmp/ansible-tmp-1487936470.27-110880382737009/setup.py; rm -rf "/home/vagrant/.ansible/tmp/ansible-tmp-1487936470.27-110880382737009/" > /dev/null 2>&1 && sleep 0'
ok: [localhost]

TASK [testrole : debug] ********************************************************
task path: /home/vagrant/demo-ansible-include_role-bug/roles/testrole/tasks/main.yml:2
ok: [localhost] => {
"msg": "Hello world!"
}

TASK [debug] *******************************************************************
task path: /home/vagrant/demo-ansible-include_role-bug/bad-playbook.yml:7
fatal: [localhost]: FAILED! => {
"failed": true,
"msg": "the field 'args' has an invalid value, which appears to include a variable that is undefined. The error was: 'some_var' is undefined\n\nThe error appears to have been in '/home/vagrant/demo-ansible-include_role-bug/bad-playbook.yml': line 7, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n name: testrole\n - debug: msg=\"{{some_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"
}
to retry, use: --limit @/home/vagrant/demo-ansible-include_role-bug/bad-playbook.retry

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