Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip self._parent on dynamic, defer grandparent for attr lookup #38827

Merged
merged 3 commits into from
Apr 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 0 additions & 2 deletions lib/ansible/playbook/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,6 @@ class Base(with_metaclass(BaseMeta, object)):
'su', 'su_user', 'su_pass', 'su_exe', 'su_flags',
]

_inheritable = True

def __init__(self):

# initialize the data loader and variable manager, which will be provided
Expand Down
17 changes: 12 additions & 5 deletions lib/ansible/playbook/block.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,13 +299,20 @@ def _get_parent_attribute(self, attr, extend=False, prepend=False):
prepend = self._valid_attrs[attr].prepend
try:
value = self._attributes[attr]
if self._parent and (value is None or extend):
# If parent is static, we can grab attrs from the parent
# otherwise, defer to the grandparent
if getattr(self._parent, 'statically_loaded', True):
_parent = self._parent
else:
_parent = self._parent._parent

if _parent and (value is None or extend):
try:
if getattr(self._parent, 'statically_loaded', True):
if hasattr(self._parent, '_get_parent_attribute'):
parent_value = self._parent._get_parent_attribute(attr)
if getattr(_parent, 'statically_loaded', True):
if hasattr(_parent, '_get_parent_attribute'):
parent_value = _parent._get_parent_attribute(attr)
else:
parent_value = self._parent._attributes.get(attr, None)
parent_value = _parent._attributes.get(attr, None)
if extend:
value = self._extend_value(value, parent_value, prepend)
else:
Expand Down
2 changes: 0 additions & 2 deletions lib/ansible/playbook/role_include.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ class IncludeRole(TaskInclude):
OTHER_ARGS = ('private', 'allow_duplicates') # assigned to matching property
VALID_ARGS = tuple(frozenset(BASE + FROM_ARGS + OTHER_ARGS)) # all valid args

_inheritable = False

# =================================================================================
# ATTRIBUTES

Expand Down
17 changes: 12 additions & 5 deletions lib/ansible/playbook/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,13 +423,20 @@ def _get_parent_attribute(self, attr, extend=False, prepend=False):
prepend = self._valid_attrs[attr].prepend
try:
value = self._attributes[attr]
if self._parent and (value is None or extend):
if getattr(self._parent, 'statically_loaded', True):
# If parent is static, we can grab attrs from the parent
# otherwise, defer to the grandparent
if getattr(self._parent, 'statically_loaded', True):
_parent = self._parent
else:
_parent = self._parent._parent

if _parent and (value is None or extend):
if getattr(_parent, 'statically_loaded', True):
# vars are always inheritable, other attributes might not be for the partent but still should be for other ancestors
if attr != 'vars' and getattr(self._parent, '_inheritable', True) and hasattr(self._parent, '_get_parent_attribute'):
parent_value = self._parent._get_parent_attribute(attr)
if attr != 'vars' and hasattr(_parent, '_get_parent_attribute'):
parent_value = _parent._get_parent_attribute(attr)
else:
parent_value = self._parent._attributes.get(attr, None)
parent_value = _parent._attributes.get(attr, None)

if extend:
value = self._extend_value(value, parent_value, prepend)
Expand Down
2 changes: 0 additions & 2 deletions lib/ansible/playbook/task_include.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ def __init__(self, block=None, role=None, task_include=None):
@staticmethod
def load(data, block=None, role=None, task_include=None, variable_manager=None, loader=None):
t = TaskInclude(block=block, role=role, task_include=task_include)
if t.action == 'include_task':
t._inheritable = False
return t.load_data(data, variable_manager=variable_manager, loader=loader)

def copy(self, exclude_parent=False, exclude_tasks=False):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- command: "true"
register: block_include_result
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- include_tasks: include_level_1.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- command: "true"
register: import_include_include_result
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- include_tasks: import_include_include_tasks.yml
4 changes: 4 additions & 0 deletions test/integration/targets/include_import/runme.sh
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,7 @@ gen_task_files
ANSIBLE_STRATEGY='linear' ansible-playbook test_copious_include_tasks.yml -i ../../inventory "$@" --skip-tags never
ANSIBLE_STRATEGY='free' ansible-playbook test_copious_include_tasks.yml -i ../../inventory "$@" --skip-tags never
rm -f tasks/hello/*.yml

# Inlcuded tasks should inherit attrs from non-dynamic blocks in parent chain
# https://github.com/ansible/ansible/pull/38827
ANSIBLE_STRATEGY='linear' ansible-playbook test_grandparent_inheritance.yml -i ../../inventory "$@"
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
- hosts: testhost
gather_facts: false
tasks:
- debug:
var: inventory_hostname

- name: Test included tasks inherit from block
check_mode: true
block:
- include_tasks: grandchild/block_include_tasks.yml

- debug:
var: block_include_result

- assert:
that:
- block_include_result is skipped

- name: Test included tasks inherit deeply from import
import_tasks: grandchild/import.yml
check_mode: true

- debug:
var: import_include_include_result

- assert:
that:
- import_include_include_result is skipped