Skip to content

Instantly share code, notes, and snippets.

@philfry
Created April 21, 2017 09:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save philfry/1ad81f4d9b6560f0d52405edaf9c98c7 to your computer and use it in GitHub Desktop.
Save philfry/1ad81f4d9b6560f0d52405edaf9c98c7 to your computer and use it in GitHub Desktop.
diff --git i/lib/ansible/playbook/helpers.py w/lib/ansible/playbook/helpers.py
index 7ded4c654..3c4dd5617 100644
--- i/lib/ansible/playbook/helpers.py
+++ w/lib/ansible/playbook/helpers.py
@@ -24,6 +24,8 @@ from ansible import constants as C
from ansible.errors import AnsibleParserError, AnsibleUndefinedVariable, AnsibleFileNotFound
from ansible.module_utils.six import string_types
+from ansible.utils.helpers import pk
+
try:
from __main__ import display
except ImportError:
@@ -69,6 +71,23 @@ def load_list_of_blocks(ds, play, parent_block=None, role=None, task_include=Non
t._parent = block_list[-1]
block_list[-1].block.extend(b.block)
else:
+ for pkt in b.block:
+ if isinstance(pkt, Block):
+ try:
+ pk("block %s, when %s" % (pkt, pkt.when))
+ except:
+ pk("block %s (no when)" % (pkt))
+ for spkt in pkt.block:
+ try:
+ pk("task %s, when %s" % (spkt, spkt.when))
+ except:
+ pk("task %s (no when)" % (spkt))
+
+ else:
+ try:
+ pk("task %s, when %s" % (pkt, pkt.when))
+ except:
+ pk("task %s (no when)" % (pkt))
block_list.append(b)
return block_list
diff --git i/lib/ansible/playbook/play.py w/lib/ansible/playbook/play.py
index ce943f09a..ad8e52569 100644
--- i/lib/ansible/playbook/play.py
+++ w/lib/ansible/playbook/play.py
@@ -33,6 +33,8 @@ from ansible.playbook.role import Role
from ansible.playbook.taggable import Taggable
from ansible.vars import preprocess_vars
+from ansible.utils.helpers import pk
+
try:
from __main__ import display
except ImportError:
@@ -233,6 +235,7 @@ class Play(Base, Taggable, Become):
if len(self.roles) > 0:
for r in self.roles:
+ pk(r)
block_list.extend(r.compile(play=self))
return block_list
@@ -251,6 +254,14 @@ class Play(Base, Taggable, Become):
return block_list
+
+ def pkdebug(self, pk_task):
+ if isinstance(pk_task, Block):
+ for pk_sub in pk_task.block:
+ self.pkdebug(pk_sub)
+ else:
+ pk(" %s, when: %s" % (pk_task, pk_task.when))
+
def compile(self):
'''
Compiles and returns the task list for this play, compiled from the
@@ -270,13 +281,26 @@ class Play(Base, Taggable, Become):
block_list = []
+ pk("pre_tasks")
block_list.extend(self.pre_tasks)
+ pk("flush_block 1")
block_list.append(flush_block)
- block_list.extend(self._compile_roles())
+ pk("_compile_roles")
+ scr = self._compile_roles()
+ block_list.extend(scr)
+# block_list.extend(self._compile_roles())
+ pk("tasks")
block_list.extend(self.tasks)
+ pk("flush_block 2")
block_list.append(flush_block)
+ pk("post_tasks")
block_list.extend(self.post_tasks)
+ pk("flush_block 3")
block_list.append(flush_block)
+ pk("done building block_list")
+
+ for pkblock in scr:
+ self.pkdebug(pkblock)
return block_list
diff --git i/lib/ansible/playbook/role/__init__.py w/lib/ansible/playbook/role/__init__.py
index e1e876c54..48a587b6d 100644
--- i/lib/ansible/playbook/role/__init__.py
+++ w/lib/ansible/playbook/role/__init__.py
@@ -34,6 +34,13 @@ from ansible.playbook.taggable import Taggable
from ansible.plugins import get_all_plugin_loaders
from ansible.utils.vars import combine_vars
+try:
+ from __main__ import display
+except ImportError:
+ from ansible.utils.display import Display
+ display = Display()
+
+from ansible.utils.helpers import pk
__all__ = ['Role', 'hash_params']
@@ -159,6 +166,7 @@ class Role(Base, Become, Conditional, Taggable):
play.ROLE_CACHE[role_include.role] = dict()
play.ROLE_CACHE[role_include.role][hashed_params] = r
+ pk("return from load")
return r
except RuntimeError:
@@ -172,6 +180,7 @@ class Role(Base, Become, Conditional, Taggable):
self._variable_manager = role_include.get_variable_manager()
self._loader = role_include.get_loader()
+ pk("begin _load_role_data")
if parent_role:
self.add_parent(parent_role)
@@ -232,6 +241,8 @@ class Role(Base, Become, Conditional, Taggable):
elif not isinstance(self._default_vars, dict):
raise AnsibleParserError("The defaults/main.yml file for role '%s' must contain a dictionary of variables" % self._role_name)
+ pk("end _load_role_data")
+
def _load_role_yaml(self, subdir, main=None):
file_path = os.path.join(self._role_path, subdir)
if self._loader.path_exists(file_path) and self._loader.is_directory(file_path):
@@ -398,6 +409,8 @@ class Role(Base, Become, Conditional, Taggable):
can correctly take their parent's tags/conditionals into account.
'''
+ pk("begin %s" % self)
+
block_list = []
# update the dependency chain here
@@ -407,10 +420,19 @@ class Role(Base, Become, Conditional, Taggable):
deps = self.get_direct_dependencies()
for dep in deps:
+ pk("process dep %s" % dep)
dep_blocks = dep.compile(play=play, dep_chain=new_dep_chain)
block_list.extend(dep_blocks)
for idx, task_block in enumerate(self._task_blocks):
+ try:
+ for pktb in task_block.block:
+ pk("task block %s (%s)" % (pktb.block, pktb.when))
+ except:
+ try:
+ pk("task block %s (%s)" % (task_block.block, task_block.block.when))
+ except:
+ pk("task block %s (no when:)" % (task_block.block))
new_task_block = task_block.copy(exclude_parent=True)
if task_block._parent:
new_task_block._parent = task_block._parent.copy()
@@ -420,6 +442,7 @@ class Role(Base, Become, Conditional, Taggable):
new_task_block._eor = True
block_list.append(new_task_block)
+ pk("end %s" % self)
return block_list
def serialize(self, include_deps=True):
diff --git i/lib/ansible/playbook/role_include.py w/lib/ansible/playbook/role_include.py
index 4ed9369cb..9c87594b6 100644
--- i/lib/ansible/playbook/role_include.py
+++ w/lib/ansible/playbook/role_include.py
@@ -26,6 +26,7 @@ from ansible.playbook.attribute import FieldAttribute
from ansible.playbook.task import Task
from ansible.playbook.role import Role
from ansible.playbook.role.include import RoleInclude
+from ansible.utils.helpers import pk
try:
from __main__ import display
@@ -73,6 +74,7 @@ class IncludeRole(Task):
ri.vars.update(self.vars)
# build role
+ pk(self)
actual_role = Role.load(ri, myplay, parent_role=self._parent_role, from_files=self._from_files)
actual_role._metadata.allow_duplicates = self.allow_duplicates
diff --git i/lib/ansible/utils/helpers.py w/lib/ansible/utils/helpers.py
index 488c3e2d9..264fa9213 100644
--- i/lib/ansible/utils/helpers.py
+++ w/lib/ansible/utils/helpers.py
@@ -33,3 +33,21 @@ def pct_to_int(value, num_items, min_value=1):
else:
return int(value)
+try:
+ from __main__ import display
+except ImportError:
+ from ansible.utils.display import Display
+ display = Display()
+
+import inspect
+def pk(txt):
+ x_func = inspect.stack()[1]
+ x_func_p = x_func[1].split("/")[-2:]
+ x_caller = inspect.stack()[2]
+ x_caller_p = x_caller[1].split("/")[-2:]
+
+ display.debug("[PK] [%s:%s]<-[%s:%s]\n%29s %s\n%29s" % (
+ "/".join(x_func_p), x_func[3],
+ "/".join(x_caller_p), x_caller[3],
+ "[PK]", txt, "--------------- [PK]")
+ )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment