forked from TerryHowe/ansible-modules-hashivault
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashivault_delete.py
executable file
·100 lines (91 loc) · 3.17 KB
/
hashivault_delete.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/python
from hvac.exceptions import InvalidPath
from ansible.module_utils.hashivault import hashivault_argspec
from ansible.module_utils.hashivault import hashivault_auth_client
from ansible.module_utils.hashivault import hashivault_init
from ansible.module_utils.hashivault import hashiwrapper
ANSIBLE_METADATA = {'status': ['stableinterface'], 'supported_by': 'community', 'version': '1.1'}
DOCUMENTATION = '''
---
module: hashivault_delete
version_added: "3.4.0"
short_description: Hashicorp Vault delete module
description:
- Module to delete from Hashicorp Vault. Consider using `hashivault_secret` instead.
options:
version:
description:
- version of the kv engine (int)
default: 1
mount_point:
description:
- secret mount point
default: secret
secret:
description:
- secret to delete.
permanent:
description:
- delete all versions and metadata for a given secret (only effective for kv engine version 2).
default: false
extends_documentation_fragment: hashivault
'''
EXAMPLES = '''
---
- hosts: localhost
tasks:
- hashivault_delete:
secret: giant
'''
def main():
argspec = hashivault_argspec()
argspec['version'] = dict(required=False, type='int', default=1)
argspec['mount_point'] = dict(required=False, type='str', default='secret')
argspec['secret'] = dict(required=True, type='str')
argspec['permanent'] = dict(required=False, type='bool', default=False)
module = hashivault_init(argspec)
result = hashivault_delete(module.params)
if result.get('failed'):
module.fail_json(**result)
else:
module.exit_json(**result)
@hashiwrapper
def hashivault_delete(params):
result = {"changed": False, "rc": 0}
client = hashivault_auth_client(params)
version = params.get('version')
mount_point = params.get('mount_point')
secret = params.get('secret')
permanent = params.get('permanent')
if secret.startswith('/'):
secret = secret.lstrip('/')
mount_point = ''
if mount_point:
secret_path = '%s/%s' % (mount_point, secret)
else:
secret_path = secret
try:
if version == 2:
if permanent:
returned_data = client.secrets.kv.v2.delete_metadata_and_all_versions(secret, mount_point=mount_point)
else:
returned_data = client.secrets.kv.v2.delete_latest_version_of_secret(secret, mount_point=mount_point)
else:
returned_data = client.secrets.kv.v1.delete_secret(secret, mount_point=mount_point)
except InvalidPath:
result['msg'] = u"Secret %s nonexistent" % secret_path
result['changed'] = False
return result
except Exception as e:
result['rc'] = 1
result['failed'] = True
error_string = "%s(%s)" % (e.__class__.__name__, e)
result['msg'] = u"Error %s deleting %s" % (error_string, secret_path)
return result
if returned_data:
result['data'] = returned_data.text
result['msg'] = u"Secret %s deleted" % secret_path
result['changed'] = True
return result
if __name__ == '__main__':
main()