Browse Source

add update_password parameter

main
Markus Bergholz 1 month ago
parent
commit
dd77315091
Signed by: m GPG Key ID: B45724801354B174
  1. 2
      README.md
  2. 37
      plugins/modules/password.py
  3. 19
      tests/passwords.yml

2
README.md

@ -138,6 +138,8 @@ CAUTION ⚠ removes files and folders - recursive!
| `notes` | notes to the password |
| `favorite` | whether the password should be marked as favourite or not |
| `state` | `present` or `absent` |
| `update_password` | When to update a password. `on_create` (default) will write the password only if the password record is created. `always` will also update the password if it's exist and the requested password differs from existing. |
```yml

37
plugins/modules/password.py

@ -30,6 +30,7 @@ def main():
name = dict(required=True, type='str'),
state = dict(type='str', choices=['present', 'absent'], default='present'),
password = dict(required=False, type='str'),
update_password = dict(type='str', choices=['always', 'on_create'], default='on_create'),
username = dict(required=False, type='str'),
url = dict(required=False, type='str'),
notes = dict(required=False, type='str'),
@ -47,6 +48,7 @@ def main():
notes = module.params.get('notes')
state = module.params.get('state')
favorite = module.params.get('favorite')
update_password = module.params.get('update_password')
retval = nc.get_password(name)
@ -54,32 +56,35 @@ def main():
if state == 'present':
if len(retval) == 1:
# update password
if password == retval[0].get('password'):
module.exit_json(changed = False, password=retval)
if password == retval[0].get('password'):
module.exit_json(changed = False, password=retval)
else:
obj = {
elif update_password == 'always':
obj = {
'id': retval[0].get('id'),
'password': password,
'label': name
}
}
if notes:
obj['notes'] = notes
if notes:
obj['notes'] = notes
if username:
obj['username'] = username
if username:
obj['username'] = username
if url:
obj['url'] = url
if url:
obj['url'] = url
if favorite:
obj['favorite'] = favorite
if favorite:
obj['favorite'] = favorite
if not module.check_mode:
retval = nc.update_password(obj)
if not module.check_mode:
retval = nc.update_password(obj)
module.exit_json(changed = True, password=retval)
module.exit_json(changed = True, password=retval)
else:
module.exit_json(changed = False, password=retval)
elif len(retval) == 0:
# create password

19
tests/passwords.yml

@ -83,6 +83,7 @@
markuman.nextcloud.password:
name: ansible-test-02
password: "{{ lookup('markuman.nextcloud.generate_password') }}"
update_password: always
check_mode: yes
register: out
@ -101,13 +102,27 @@
that:
- 'out.password[0].statusCode == "BREACHED"'
- name: update test password
markuman.nextcloud.password:
name: ansible-test-02
password: "{{ lookup('markuman.nextcloud.generate_password') }}"
update_password: on_create
register: out
- name: pseudo updated
- name: don't updated
assert:
that:
- out is not changed
- name: update test password
markuman.nextcloud.password:
name: ansible-test-02
password: "{{ lookup('markuman.nextcloud.generate_password') }}"
update_password: always
register: out
- name: updated password
assert:
that:
- out is changed
@ -117,7 +132,7 @@
name: ansible-test-02
register: out
- name: verify bad password
- name: verify updated good password
assert:
that:
- 'out.password[0].statusCode == "GOOD"'

Loading…
Cancel
Save