commit | author | age
|
736ddb
|
1 |
#!/usr/bin/python |
JK |
2 |
# |
|
3 |
# Copyright: (c) 2020, Johnathan Kupferer <jkupfere@redhat.com> |
|
4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
|
5 |
# |
|
6 |
# This plugin is free software: you can redistribute it and/or modify |
|
7 |
# it under the terms of the GNU General Public License as published by |
|
8 |
# the Free Software Foundation, either version 3 of the License, or |
|
9 |
# (at your option) any later version. |
|
10 |
# |
|
11 |
# Ansible is distributed in the hope that it will be useful, |
|
12 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 |
# GNU General Public License for more details. |
|
15 |
# |
|
16 |
# You should have received a copy of the GNU General Public License |
|
17 |
# along with Ansible. If not, see <http://www.gnu.org/licenses/>. |
|
18 |
|
|
19 |
from __future__ import (absolute_import, division, print_function) |
|
20 |
__metaclass__ = type |
|
21 |
|
|
22 |
import json |
24f6de
|
23 |
import yaml |
736ddb
|
24 |
import os |
24f6de
|
25 |
|
JK |
26 |
# Force yaml string representation for safe dump |
|
27 |
yaml.SafeDumper.yaml_representers[None] = lambda self, data: \ |
|
28 |
yaml.representer.SafeRepresenter.represent_str( |
|
29 |
self, |
|
30 |
str(data), |
|
31 |
) |
736ddb
|
32 |
|
JK |
33 |
from ansible.errors import AnsibleError, AnsibleUndefinedVariable |
|
34 |
from ansible.module_utils.six import string_types |
|
35 |
from ansible.module_utils._text import to_text |
|
36 |
from ansible.plugins.action import ActionBase |
|
37 |
|
|
38 |
class ActionModule(ActionBase): |
|
39 |
'''Print statements during execution and save user info to file''' |
|
40 |
|
|
41 |
TRANSFERS_FILES = False |
24f6de
|
42 |
_VALID_ARGS = frozenset(('msg','data','user')) |
736ddb
|
43 |
|
JK |
44 |
def run(self, tmp=None, task_vars=None): |
|
45 |
self._supports_check_mode = True |
|
46 |
|
|
47 |
if task_vars is None: |
|
48 |
task_vars = dict() |
|
49 |
|
|
50 |
result = super(ActionModule, self).run(tmp, task_vars) |
|
51 |
del tmp # tmp no longer has any effect |
|
52 |
|
984cda
|
53 |
msg = self._task.args.get('msg') |
JK |
54 |
data = self._task.args.get('data') |
24f6de
|
55 |
user = self._task.args.get('user') |
984cda
|
56 |
|
JK |
57 |
if msg: |
|
58 |
result['msg'] = 'user.info: ' + msg |
|
59 |
if data: |
|
60 |
result['data'] = data |
|
61 |
if not isinstance(data, dict): |
|
62 |
result['failed'] = True |
|
63 |
result['error'] = 'data must be a dictionary of name/value pairs' |
|
64 |
return result |
|
65 |
|
|
66 |
# Force display of result like debug |
736ddb
|
67 |
result['_ansible_verbose_always'] = True |
JK |
68 |
|
|
69 |
try: |
b43276
|
70 |
output_dir = self._templar.template( |
JK |
71 |
task_vars.get('output_dir', |
|
72 |
task_vars['hostvars'].get('localhost',{}).get('output_dir', |
|
73 |
task_vars.get('playbook_dir', '.') |
|
74 |
) |
736ddb
|
75 |
) |
JK |
76 |
) |
984cda
|
77 |
if msg: |
JK |
78 |
fh = open(os.path.join(output_dir, 'user-info.yaml'), 'a') |
|
79 |
fh.write('- ' + json.dumps(msg) + "\n") |
|
80 |
fh.close() |
|
81 |
if data: |
24f6de
|
82 |
user_data = None |
JK |
83 |
try: |
|
84 |
fh = open(os.path.join(output_dir, 'user-data.yaml'), 'r') |
|
85 |
user_data = yaml.safe_load(fh) |
|
86 |
fh.close() |
|
87 |
except FileNotFoundError: |
|
88 |
pass |
|
89 |
|
|
90 |
if user_data == None: |
|
91 |
user_data = {} |
|
92 |
|
|
93 |
if user: |
|
94 |
if 'users' not in user_data: |
|
95 |
user_data['users'] = {} |
|
96 |
if user in user_data['users']: |
|
97 |
user_data['users'][user].update(data) |
|
98 |
else: |
|
99 |
user_data['users'][user] = data |
|
100 |
else: |
|
101 |
user_data.update(data) |
|
102 |
|
|
103 |
fh = open(os.path.join(output_dir, 'user-data.yaml'), 'w') |
|
104 |
yaml.safe_dump(user_data, stream=fh, explicit_start=True) |
984cda
|
105 |
fh.close() |
736ddb
|
106 |
result['failed'] = False |
JK |
107 |
except Exception as e: |
|
108 |
result['failed'] = True |
|
109 |
result['error'] = str(e) |
|
110 |
|
|
111 |
return result |