Debian Codegraph @

ansible_collections/community/hashi_vault/plugins/modules/vault_database_role_delete.py

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 # (c) 2024, Martin Chmielewski (@M4rt1nCh)
4 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
5 # SPDX-License-Identifier: GPL-3.0-or-later
6
7 from __future__ import absolute_import, division, print_function
8
9 __metaclass__ = type
10
11 DOCUMENTATION = r"""
12 module: vault_database_role_delete
13 version_added: 6.2.0
14 author:
15 - Martin Chmielewski (@M4rt1nCh)
16 short_description: Delete a role definition
17 requirements:
18 - C(hvac) (L(Python library,https://hvac.readthedocs.io/en/stable/overview.html))
19 - For detailed requirements, see R(the collection requirements page,ansible_collections.community.hashi_vault.docsite.user_guide.requirements).
20 description:
21 - L(Delete a role definition,https://hvac.readthedocs.io/en/stable/usage/secrets_engines/database.html#delete-a-role).
22 notes:
23 - Applies to both static and dynamic roles.
24 - This module always reports C(changed) status because it cannot guarantee idempotence.
25 - Use C(changed_when) to control that in cases where the operation is known to not change state.
26 attributes:
27 check_mode:
28 support: partial
29 details:
30 - In check mode, a sample response will be returned, but the deletion will not be performed in Hashicorp Vault.
31 extends_documentation_fragment:
32 - community.hashi_vault.attributes
33 - community.hashi_vault.attributes.action_group
34 - community.hashi_vault.connection
35 - community.hashi_vault.auth
36 - community.hashi_vault.engine_mount
37 options:
38 role_name:
39 description: The name of the role to rotate credentials for.
40 type: str
41 required: True
42 """
43
44 EXAMPLES = r"""
45 - name: Delete a Role with the default mount point
46 community.hashi_vault.vault_database_role_delete:
47 url: https://vault:8201
48 auth_method: userpass
49 username: '{{ user }}'
50 password: '{{ passwd }}'
51 role_name: SomeRole
52 register: result
53
54 - name: Display the result of the operation
55 ansible.builtin.debug:
56 msg: "{{ result }}"
57
58 - name: Delete a Role with a custom mount point
59 community.hashi_vault.vault_database_role_delete:
60 url: https://vault:8201
61 auth_method: userpass
62 username: '{{ user }}'
63 password: '{{ passwd }}'
64 engine_mount_path: db1
65 role_name: SomeRole
66 register: result
67
68 - name: Display the result of the operation
69 ansible.builtin.debug:
70 msg: "{{ result }}"
71 """
72
73 RETURN = r""""""
74
75 import traceback
76
77 from ansible.module_utils.common.text.converters import to_text
78
79 from ..module_utils._hashi_vault_module import HashiVaultModule
80 from ..module_utils._hashi_vault_common import HashiVaultValueError
81
82
83 def run_module():
84 argspec = HashiVaultModule.generate_argspec(
85 engine_mount_point=dict(type="str", required=False),
86 role_name=dict(type="str", required=True),
87 )
88
89 module = HashiVaultModule(argument_spec=argspec, supports_check_mode=True)
90
91 if module.check_mode is True:
92 module.exit_json(changed=True)
93
94 parameters = {}
95 engine_mount_point = module.params.get("engine_mount_point", None)
96 if engine_mount_point is not None:
97 parameters["mount_point"] = engine_mount_point
98 parameters["name"] = module.params.get("role_name")
99
100 module.connection_options.process_connection_options()
101 client_args = module.connection_options.get_hvac_connection_options()
102 client = module.helper.get_vault_client(**client_args)
103 hvac_exceptions = module.helper.get_hvac().exceptions
104
105 try:
106 module.authenticator.validate()
107 module.authenticator.authenticate(client)
108 except (NotImplementedError, HashiVaultValueError) as e:
109 module.fail_json(msg=to_text(e), exception=traceback.format_exc())
110
111 try:
112 client.secrets.database.delete_role(**parameters)
113 except hvac_exceptions.Forbidden as e:
114 module.fail_json(
115 msg="Forbidden: Permission Denied to path ['%s']." % engine_mount_point
116 or "database",
117 exception=traceback.format_exc(),
118 )
119 except hvac_exceptions.InvalidPath as e:
120 module.fail_json(
121 msg="Invalid or missing path ['%s/roles/%s']."
122 % (engine_mount_point or "database", parameters["name"]),
123 exception=traceback.format_exc(),
124 )
125 else:
126 module.exit_json(changed=True)
127
128
129 def main():
130 run_module()
131
132
133 if __name__ == "__main__":
134 main()