Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 # (c) 2021, Brian Scholer (@briantist)
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 __metaclass__ = type
9
10 DOCUMENTATION = """
11 module: vault_read
12 version_added: 1.4.0
13 author:
14 - Brian Scholer (@briantist)
15 short_description: Perform a read operation against HashiCorp Vault
16 requirements:
17 - C(hvac) (L(Python library,https://hvac.readthedocs.io/en/stable/overview.html))
18 - For detailed requirements, see R(the collection requirements page,ansible_collections.community.hashi_vault.docsite.user_guide.requirements).
19 description:
20 - Performs a generic read operation against a given path in HashiCorp Vault.
21 seealso:
22 - ref: community.hashi_vault.vault_read lookup <ansible_collections.community.hashi_vault.vault_read_lookup>
23 description: The official documentation for the C(community.hashi_vault.vault_read) lookup plugin.
24 - ref: community.hashi_vault.hashi_vault lookup <ansible_collections.community.hashi_vault.hashi_vault_lookup>
25 description: The official documentation for the C(community.hashi_vault.hashi_vault) lookup plugin.
26 extends_documentation_fragment:
27 - community.hashi_vault.attributes
28 - community.hashi_vault.attributes.action_group
29 - community.hashi_vault.attributes.check_mode_read_only
30 - community.hashi_vault.connection
31 - community.hashi_vault.auth
32 options:
33 path:
34 description: Vault path to be read.
35 type: str
36 required: True
37 """
38
39 EXAMPLES = """
40 - name: Read a kv2 secret from Vault via the remote host with userpass auth
41 community.hashi_vault.vault_read:
42 url: https://vault:8201
43 path: secret/data/hello
44 auth_method: userpass
45 username: user
46 password: '{{ passwd }}'
47 register: secret
48
49 - name: Display the secret data
50 ansible.builtin.debug:
51 msg: "{{ secret.data.data.data }}"
52
53 - name: Retrieve an approle role ID from Vault via the remote host
54 community.hashi_vault.vault_read:
55 url: https://vault:8201
56 path: auth/approle/role/role-name/role-id
57 register: approle_id
58
59 - name: Display the role ID
60 ansible.builtin.debug:
61 msg: "{{ approle_id.data.data.role_id }}"
62 """
63
64 RETURN = """
65 data:
66 description: The raw result of the read against the given path.
67 returned: success
68 type: dict
69 """
70
71 import traceback
72
73 from ansible.module_utils.common.text.converters import to_text
74
75 from ansible_collections.community.hashi_vault.plugins.module_utils._hashi_vault_module import HashiVaultModule
76 from ansible_collections.community.hashi_vault.plugins.module_utils._hashi_vault_common import HashiVaultValueError
77
78
79 def run_module():
80 argspec = HashiVaultModule.generate_argspec(
81 path=dict(type='str', required=True),
82 )
83
84 module = HashiVaultModule(
85 argument_spec=argspec,
86 supports_check_mode=True
87 )
88
89 path = module.params.get('path')
90
91 module.connection_options.process_connection_options()
92 client_args = module.connection_options.get_hvac_connection_options()
93 client = module.helper.get_vault_client(**client_args)
94 hvac_exceptions = module.helper.get_hvac().exceptions
95
96 try:
97 module.authenticator.validate()
98 module.authenticator.authenticate(client)
99 except (NotImplementedError, HashiVaultValueError) as e:
100 module.fail_json(msg=to_text(e), exception=traceback.format_exc())
101
102 try:
103 data = client.read(path)
104 except hvac_exceptions.Forbidden as e:
105 module.fail_json(msg="Forbidden: Permission Denied to path '%s'." % path, exception=traceback.format_exc())
106
107 if data is None:
108 module.fail_json(msg="The path '%s' doesn't seem to exist." % path)
109
110 module.exit_json(data=data)
111
112
113 def main():
114 run_module()
115
116
117 if __name__ == '__main__':
118 main()