|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# |
| 3 | +# Ansible action plugin merge_ansible_facts. |
| 4 | +# Merges saved facts with current Ansible facts on the controller and returns |
| 5 | +# ansible_facts (saved_ansible_facts with current_ansible_facts overlaid). |
| 6 | +# |
| 7 | +# Copyright: (c) 2025, Linux System Roles |
| 8 | +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 9 | + |
| 10 | +from __future__ import absolute_import, division, print_function |
| 11 | + |
| 12 | +__metaclass__ = type |
| 13 | + |
| 14 | +from ansible.plugins.action import ActionBase |
| 15 | + |
| 16 | + |
| 17 | +class ActionModule(ActionBase): |
| 18 | + |
| 19 | + TRANSFERS_FILES = False |
| 20 | + |
| 21 | + def run(self, tmp=None, task_vars=None): |
| 22 | + if task_vars is None: |
| 23 | + task_vars = {} |
| 24 | + |
| 25 | + result = super(ActionModule, self).run(tmp, task_vars) |
| 26 | + result["changed"] = False |
| 27 | + |
| 28 | + current = self._task.args.get("current_ansible_facts") |
| 29 | + saved = self._task.args.get("saved_ansible_facts") |
| 30 | + |
| 31 | + if current is None: |
| 32 | + result["failed"] = True |
| 33 | + result["msg"] = "current_ansible_facts is required" |
| 34 | + return result |
| 35 | + if saved is None: |
| 36 | + result["failed"] = True |
| 37 | + result["msg"] = "saved_ansible_facts is required" |
| 38 | + return result |
| 39 | + |
| 40 | + # Template in case args were passed as raw Jinja |
| 41 | + current = self._templar.template(current) |
| 42 | + saved = self._templar.template(saved) |
| 43 | + |
| 44 | + if not isinstance(current, dict): |
| 45 | + result["failed"] = True |
| 46 | + result["msg"] = "current_ansible_facts must be a dict" |
| 47 | + return result |
| 48 | + if not isinstance(saved, dict): |
| 49 | + result["failed"] = True |
| 50 | + result["msg"] = "saved_ansible_facts must be a dict" |
| 51 | + return result |
| 52 | + |
| 53 | + # Merge: start with a copy of saved, overlay current (same behavior as module) |
| 54 | + merged = dict(saved) |
| 55 | + merged.update(current) |
| 56 | + result["ansible_facts"] = merged |
| 57 | + |
| 58 | + return result |
0 commit comments