From 78478e80ea01c2d752af5abdfbf3f744bfd188ac Mon Sep 17 00:00:00 2001 From: James Cammarata Date: Thu, 11 May 2017 14:57:30 -0500 Subject: [PATCH] Avoid default serialization of classes in dump_attrs For playbook base objects, when dumping attributes via dump_attrs() an attribute like loop_control is a class. Using the default serialization for these is slow and consumes a lot of memory. Since LoopControl is also based on the Base class, we can use serialize() instead and save a lot of resources. This also adds a from_attrs() complimentary method to nicely turn the dumped attrs back into proper field attributes. Fixes #23579 --- lib/ansible/playbook/base.py | 22 ++++++++++++++++++++-- lib/ansible/plugins/strategy/__init__.py | 3 +-- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/lib/ansible/playbook/base.py b/lib/ansible/playbook/base.py index bd4bef91b6..eaddab0076 100644 --- a/lib/ansible/playbook/base.py +++ b/lib/ansible/playbook/base.py @@ -506,10 +506,28 @@ class Base(with_metaclass(BaseMeta, object)): Dumps all attributes to a dictionary ''' attrs = dict() - for name in self._valid_attrs.keys(): - attrs[name] = getattr(self, name) + for (name, attribute) in iteritems(self._valid_attrs): + attr = getattr(self, name) + if attribute.isa == 'class' and attr is not None and hasattr(attr, 'serialize'): + attrs[name] = attr.serialize() + else: + attrs[name] = attr return attrs + def from_attrs(self, attrs): + ''' + Loads attributes from a dictionary + ''' + for (attr, value) in iteritems(attrs): + if attr in self._valid_attrs: + attribute = self._valid_attrs[attr] + if attribute.isa == 'class' and isinstance(value, dict): + obj = attribute.class_type() + obj.deserialize(value) + setattr(self, attr, obj) + else: + setattr(self, attr, value) + def serialize(self): ''' Serializes the object derived from the base object into diff --git a/lib/ansible/plugins/strategy/__init__.py b/lib/ansible/plugins/strategy/__init__.py index 630ebfe140..09fa6dbe86 100644 --- a/lib/ansible/plugins/strategy/__init__.py +++ b/lib/ansible/plugins/strategy/__init__.py @@ -332,8 +332,7 @@ class StrategyBase: found_task = iterator.get_original_task(original_host, task_result._task) original_task = found_task.copy(exclude_parent=True, exclude_tasks=True) original_task._parent = found_task._parent - for (attr, val) in iteritems(task_result._task_fields): - setattr(original_task, attr, val) + original_task.from_attrs(task_result._task_fields) task_result._host = original_host task_result._task = original_task