summaryrefslogtreecommitdiffstatshomepage
path: root/plugins
diff options
context:
space:
mode:
authorEgor Tensin <egor@tensin.name>2025-11-23 21:49:27 +0100
committerEgor Tensin <egor@tensin.name>2025-11-23 22:24:05 +0100
commitb3a42e41cd183f1228bd4c535b183a5b0d87a212 (patch)
tree2d30f845e13be520a1414177845f820455c4e4b6 /plugins
parentplugins: factor out read_file (diff)
downloadinfra-ansible-b3a42e41cd183f1228bd4c535b183a5b0d87a212.tar.gz
infra-ansible-b3a42e41cd183f1228bd4c535b183a5b0d87a212.zip
plugins: add parse_json_file
Diffstat (limited to 'plugins')
-rwxr-xr-xplugins/action/parse_json_file.py27
1 files changed, 27 insertions, 0 deletions
diff --git a/plugins/action/parse_json_file.py b/plugins/action/parse_json_file.py
new file mode 100755
index 0000000..d9c864a
--- /dev/null
+++ b/plugins/action/parse_json_file.py
@@ -0,0 +1,27 @@
+#!/usr/bin/python
+
+import json
+
+from ansible.errors import AnsibleActionFail
+from ansible.plugins.action import ActionBase
+from ansible_collections.tensin.infra.plugins.module_utils import file
+
+
+class ActionModule(ActionBase):
+ def run(self, tmp=None, task_vars=None):
+ result = super().run(tmp, task_vars)
+
+ path = self._task.args['path']
+ default = self._task.args.get('default')
+ if default is not None:
+ default = json.dumps(default)
+
+ content = file.read_file(self, path, task_vars, default=default)
+
+ try:
+ content = json.loads(content)
+ except json.JSONDecodeError as e:
+ raise AnsibleActionFail(f'Failed to parse JSON at {path}') from e
+
+ result['content'] = content
+ return result