Allow setting ODL NB REST password 91/73391/17
authorTaseer Ahmed <taseer94@gmail.com>
Mon, 25 Jun 2018 09:05:53 +0000 (14:05 +0500)
committerTaseer Ahmed <taseer94@gmail.com>
Wed, 18 Jul 2018 04:32:33 +0000 (09:32 +0500)
Use custom ansible module. When placed under library,
Ansible will detect it automatically. Mirrors the Puppet
logic. Usage is explained in the examples section of the code.

JIRA: INTPAK-177
Change-Id: Ieecbf1dd9c0a923aaf9eff2fd85d391bed800d81
Signed-off-by: Taseer Ahmed <taseer94@gmail.com>
library/odl_usermod.py [new file with mode: 0644]

diff --git a/library/odl_usermod.py b/library/odl_usermod.py
new file mode 100644 (file)
index 0000000..c5a1223
--- /dev/null
@@ -0,0 +1,113 @@
+from ansible.module_utils.basic import AnsibleModule
+
+ANSIBLE_METADATA = {'metadata_version': '1.0', 'status': ['preview'],
+                    'supported_by': 'community'}
+
+DOCUMENTATION = \
+    '''
+---
+module: odl_usermod
+short_description: Manipulate ODL users
+description:
+    - Use this module to add, delete and list ODL users
+version_added: "1.0"
+author: "Taseer Ahmed (@Taseer)"
+options:
+notes:
+requirements:
+'''
+
+RETURN = \
+    '''
+message:
+  description: Add/remove/list OpenDaylight users
+'''
+
+EXAMPLES = \
+    '''
+---
+- hosts: localhost
+  tasks:
+    - name: create odl user
+      odl_usermod:
+        username: admin
+        password: admin
+        state: present
+
+    - name: delete odl user
+      odl_usermod:
+        username: admin
+        state: absent
+
+    - name: list odl users
+      odl_usermod:
+        state: list
+'''
+
+
+def build_cmd(module, *args):
+    cmd = [
+        module.get_bin_path('java', True),
+        '-jar',
+        '/opt/opendaylight/bin/aaa-cli-jar.jar',
+        '--dbd',
+        '/opt/opendaylight/data'
+        ]
+    for arg in args:
+        cmd.append(arg)
+    return cmd
+
+
+def main():
+    module = AnsibleModule(
+        argument_spec=dict(
+            username=dict(type='str'),
+            password=dict(type='str'),
+            state=dict(type='str')
+        )
+    )
+
+    username = module.params['username']
+    password = module.params['password']
+    state = module.params['state']
+
+    if state == 'absent':
+        ls_users_cmd = build_cmd(module, "-l")
+        (rc, out, err) = module.run_command(ls_users_cmd)
+        if username in out:
+            cmd = build_cmd(module, '--deleteUser', username)
+            (rc, out, err) = module.run_command(cmd)
+            if rc is not None and rc != 0:
+                return module.fail_json(msg=err)
+            module.exit_json(changed=True, msg="User deleted")
+        else:
+            module.exit_json(changed=False, msg="No such user exists")
+    elif state == 'present':
+        ls_users_cmd = build_cmd(module, "-l")
+        (rc, out, err) = module.run_command(ls_users_cmd)
+        if rc is not None and rc != 0:
+            return module.fail_json(msg=err)
+
+        if username in out:
+            module.exit_json(changed=False, msg="User already exists")
+        else:
+            cmd = build_cmd(module, '--newUser', username, '-p', password)
+            (rc, out, err) = module.run_command(cmd)
+            if rc is not None and rc != 0:
+                return module.fail_json(msg=err)
+            module.exit_json(changed=True, msg="User added")
+    elif state == 'list':
+        ls_users_cmd = build_cmd(module, "-l")
+        (rc, out, err) = module.run_command(ls_users_cmd)
+        if rc is not None and rc != 0:
+            return module.fail_json(msg=err)
+        users = out.split('\n')
+        if users[0] == 'User names:':
+            users.pop(0)
+        module.exit_json(changed=False, msg=users)
+    else:
+        module.exit_json(changed=False, msg="No state specified")
+
+
+if __name__ == '__main__':
+    main()