Allow setting ODL NB REST password
[integration/packaging/ansible-opendaylight.git] / library / odl_usermod.py
1 from ansible.module_utils.basic import AnsibleModule
2
3 ANSIBLE_METADATA = {'metadata_version': '1.0', 'status': ['preview'],
4                     'supported_by': 'community'}
5
6 DOCUMENTATION = \
7     '''
8 ---
9 module: odl_usermod
10 short_description: Manipulate ODL users
11 description:
12     - Use this module to add, delete and list ODL users
13 version_added: "1.0"
14 author: "Taseer Ahmed (@Taseer)"
15 options:
16 notes:
17 requirements:
18 '''
19
20 RETURN = \
21     '''
22 message:
23   description: Add/remove/list OpenDaylight users
24 '''
25
26 EXAMPLES = \
27     '''
28 ---
29 - hosts: localhost
30   tasks:
31     - name: create odl user
32       odl_usermod:
33         username: admin
34         password: admin
35         state: present
36
37     - name: delete odl user
38       odl_usermod:
39         username: admin
40         state: absent
41
42     - name: list odl users
43       odl_usermod:
44         state: list
45 '''
46
47
48 def build_cmd(module, *args):
49     cmd = [
50         module.get_bin_path('java', True),
51         '-jar',
52         '/opt/opendaylight/bin/aaa-cli-jar.jar',
53         '--dbd',
54         '/opt/opendaylight/data'
55         ]
56     for arg in args:
57         cmd.append(arg)
58     return cmd
59
60
61 def main():
62     module = AnsibleModule(
63         argument_spec=dict(
64             username=dict(type='str'),
65             password=dict(type='str'),
66             state=dict(type='str')
67         )
68     )
69
70     username = module.params['username']
71     password = module.params['password']
72     state = module.params['state']
73
74     if state == 'absent':
75         ls_users_cmd = build_cmd(module, "-l")
76         (rc, out, err) = module.run_command(ls_users_cmd)
77         if username in out:
78             cmd = build_cmd(module, '--deleteUser', username)
79             (rc, out, err) = module.run_command(cmd)
80             if rc is not None and rc != 0:
81                 return module.fail_json(msg=err)
82             module.exit_json(changed=True, msg="User deleted")
83         else:
84             module.exit_json(changed=False, msg="No such user exists")
85     elif state == 'present':
86         ls_users_cmd = build_cmd(module, "-l")
87         (rc, out, err) = module.run_command(ls_users_cmd)
88         if rc is not None and rc != 0:
89             return module.fail_json(msg=err)
90
91         if username in out:
92             module.exit_json(changed=False, msg="User already exists")
93         else:
94             cmd = build_cmd(module, '--newUser', username, '-p', password)
95             (rc, out, err) = module.run_command(cmd)
96             if rc is not None and rc != 0:
97                 return module.fail_json(msg=err)
98             module.exit_json(changed=True, msg="User added")
99     elif state == 'list':
100         ls_users_cmd = build_cmd(module, "-l")
101         (rc, out, err) = module.run_command(ls_users_cmd)
102         if rc is not None and rc != 0:
103             return module.fail_json(msg=err)
104         users = out.split('\n')
105         if users[0] == 'User names:':
106             users.pop(0)
107         module.exit_json(changed=False, msg=users)
108     else:
109         module.exit_json(changed=False, msg="No state specified")
110
111
112 if __name__ == '__main__':
113     main()