Johnathan Kupferer
2020-03-01 24f6de3abec95545584a6f8944266b85f0233d2c
Add agnosticd_user_info per-user data (#1212)

3 files modified
76 ■■■■■ changed files
ansible/action_plugins/agnosticd_user_info.py 37 ●●●● patch | view | raw | blame | history
ansible/configs/test-empty-config/post_software.yml 26 ●●●●● patch | view | raw | blame | history
ansible/library/agnosticd_user_info.py 13 ●●●● patch | view | raw | blame | history
ansible/action_plugins/agnosticd_user_info.py
@@ -20,7 +20,15 @@
__metaclass__ = type
import json
import yaml
import os
# Force yaml string representation for safe dump
yaml.SafeDumper.yaml_representers[None] = lambda self, data: \
    yaml.representer.SafeRepresenter.represent_str(
        self,
        str(data),
    )
from ansible.errors import AnsibleError, AnsibleUndefinedVariable
from ansible.module_utils.six import string_types
@@ -31,7 +39,7 @@
    '''Print statements during execution and save user info to file'''
    TRANSFERS_FILES = False
    _VALID_ARGS = frozenset(('msg','data'))
    _VALID_ARGS = frozenset(('msg','data','user'))
    def run(self, tmp=None, task_vars=None):
        self._supports_check_mode = True
@@ -44,6 +52,7 @@
        msg = self._task.args.get('msg')
        data = self._task.args.get('data')
        user = self._task.args.get('user')
        if msg:
            result['msg'] = 'user.info: ' + msg
@@ -70,9 +79,29 @@
                fh.write('- ' + json.dumps(msg) + "\n")
                fh.close()
            if data:
                fh = open(os.path.join(output_dir, 'user-data.yaml'), 'a')
                for k, v in data.items():
                    fh.write("{0}: {1}\n".format(k, json.dumps(v)))
                user_data = None
                try:
                    fh = open(os.path.join(output_dir, 'user-data.yaml'), 'r')
                    user_data = yaml.safe_load(fh)
                    fh.close()
                except FileNotFoundError:
                    pass
                if user_data == None:
                    user_data = {}
                if user:
                    if 'users' not in user_data:
                        user_data['users'] = {}
                    if user in user_data['users']:
                        user_data['users'][user].update(data)
                    else:
                        user_data['users'][user] = data
                else:
                    user_data.update(data)
                fh = open(os.path.join(output_dir, 'user-data.yaml'), 'w')
                yaml.safe_dump(user_data, stream=fh, explicit_start=True)
                fh.close()
            result['failed'] = False
        except Exception as e:
ansible/configs/test-empty-config/post_software.yml
@@ -31,6 +31,32 @@
        random_string: >-
          {{ lookup('password', '/dev/null length=15 chars=ascii_letters') }}
    - name: Test set agnosticd_user_info for users
      agnosticd_user_info:
        msg: student{{ n }} password is {{ random_string }}
        data:
          password: "{{ random_string }}"
        user: student{{ n }}
      loop: "{{ range(1, 10) | list }}"
      loop_control:
        loop_var: n
      vars:
        random_string: >-
          {{ lookup('password', '/dev/null length=15 chars=ascii_letters') }}
    - name: Test add agnosticd_user_info for users
      agnosticd_user_info:
        msg: student{{ n }} password is {{ random_string }}
        data:
          dns_domain: student{{ n }}.example.com
        user: student{{ n }}
      loop: "{{ range(1, 10) | list }}"
      loop_control:
        loop_var: n
      vars:
        random_string: >-
          {{ lookup('password', '/dev/null length=15 chars=ascii_letters') }}
    - name: Print string expected by Cloudforms
      debug:
        msg: "Post-Software checks completed successfully"
ansible/library/agnosticd_user_info.py
@@ -37,9 +37,16 @@
options:
  msg:
    description:
    - This is the message or data to display.
    - It may be of any datatype.
  required: true
      - This is the message or data to display.
      - It should be a string.
  data:
    description:
      - Dictionary of data to store.
      - Values may be of any type, deep data structures are discouraged.
      - Subsequent calls to agnosticd_user_info will add new keys and update existing keys.
  user:
    description:
      - User name if data should be set for a particular user.
author:
- Johnathan Kupferer