From 531e544b30e69f436d14c4ce18c67998c1a0774b Mon Sep 17 00:00:00 2001 From: Rafael Guterres Jeffman <rjeffman@redhat.com> Date: Wed, 5 Aug 2020 15:13:46 -0300 Subject: [PATCH] Added support for client defined result data in FReeIPABaseModule Modified support for processing result of IPA API commands so that client code can define its own processing and add return values to self.exit_args based on command result. If a subclass need to process the result of IPA API commands it should override the method `process_command_result`. The default implementation will simply evaluate if `changed` should be true. --- .../module_utils/ansible_freeipa_module.py | 22 +++++++++++++------ plugins/modules/ipadnszone.py | 8 +++++++ 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/plugins/module_utils/ansible_freeipa_module.py b/plugins/module_utils/ansible_freeipa_module.py index 4799e5a4..30302b4f 100644 --- a/plugins/module_utils/ansible_freeipa_module.py +++ b/plugins/module_utils/ansible_freeipa_module.py @@ -619,7 +619,7 @@ class FreeIPABaseModule(AnsibleModule): if exc_val: self.fail_json(msg=str(exc_val)) - self.exit_json(changed=self.changed, user=self.exit_args) + self.exit_json(changed=self.changed, **self.exit_args) def get_command_errors(self, command, result): """Look for erros into command results.""" @@ -658,14 +658,22 @@ class FreeIPABaseModule(AnsibleModule): except Exception as excpt: self.fail_json(msg="%s: %s: %s" % (command, name, str(excpt))) else: - if "completed" in result: - if result["completed"] > 0: - self.changed = True - else: - self.changed = True - + self.process_command_result(name, command, args, result) self.get_command_errors(command, result) + def process_command_result(self, name, command, args, result): + """ + Process an API command result. + + This method can be overriden in subclasses, and change self.exit_values + to return data in the result for the controller. + """ + if "completed" in result: + if result["completed"] > 0: + self.changed = True + else: + self.changed = True + def require_ipa_attrs_change(self, command_args, ipa_attrs): """ Compare given args with current object attributes. diff --git a/plugins/modules/ipadnszone.py b/plugins/modules/ipadnszone.py index 901bfefd..6a90fa2b 100644 --- a/plugins/modules/ipadnszone.py +++ b/plugins/modules/ipadnszone.py @@ -472,6 +472,14 @@ class DNSZoneModule(FreeIPABaseModule): } self.add_ipa_command("dnszone_mod", zone_name, args) + def process_command_result(self, name, command, args, result): + super(DNSZoneModule, self).process_command_result( + name, command, args, result + ) + if command == "dnszone_add" and self.ipa_params.name_from_ip: + dnszone_exit_args = self.exit_args.setdefault('dnszone', {}) + dnszone_exit_args['name'] = name + def get_argument_spec(): forwarder_spec = dict( -- GitLab