Debian Codegraph @

lib/ansible/plugins/lookup/unvault.py

raw
1 # (c) 2020 Ansible Project
2 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
3 from __future__ import annotations
4
5 DOCUMENTATION = """
6 name: unvault
7 author: Ansible Core Team
8 version_added: "2.10"
9 short_description: read vaulted file(s) contents
10 description:
11 - This lookup returns the contents from vaulted (or not) file(s) on the Ansible controller's file system.
12 positional: _terms
13 options:
14 _terms:
15 description: path(s) of files to read
16 required: True
17 notes:
18 - This lookup does not understand 'globbing' nor shell environment variables.
19 seealso:
20 - ref: playbook_task_paths
21 description: Search paths used for relative files.
22 """
23
24 EXAMPLES = """
25 - ansible.builtin.debug: msg="the value of foo.txt is {{ lookup('ansible.builtin.unvault', '/etc/foo.txt') | string | trim }}"
26 """
27
28 RETURN = """
29 _raw:
30 description:
31 - content of file(s) as bytes
32 type: list
33 elements: raw
34 """
35
36 from ansible.errors import AnsibleParserError
37 from ansible.module_utils.secrets import register_secrets
38 from ansible.plugins.lookup import LookupBase
39 from ansible.utils.display import Display
40
41 display = Display()
42
43
44 class LookupModule(LookupBase):
45
46 def run(self, terms, variables=None, **kwargs):
47
48 ret = []
49
50 self.set_options(var_options=variables, direct=kwargs)
51
52 for term in terms:
53 display.debug("Unvault lookup term: %s" % term)
54
55 # Find the file in the expected search path
56 lookupfile = self.find_file_in_search_path(variables, 'files', term)
57 display.vvvv(u"Unvault lookup found %s" % lookupfile)
58 if lookupfile:
59 ret.append(self._loader.get_text_file_contents(lookupfile))
60 else:
61 raise AnsibleParserError('Unable to find file matching "%s" ' % term)
62
63 register_secrets(ret)
64
65 return ret