Debian Codegraph @

lib/ansible/plugins/lookup/vars.py

raw
1 # (c) 2017 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: vars
7 author: Ansible Core Team
8 version_added: "2.5"
9 short_description: Lookup templated value of variables
10 description:
11 - 'Retrieves the value of an Ansible variable. Note: Only returns top level variable names.'
12 positional: _terms
13 options:
14 _terms:
15 description: The variable names to look up.
16 required: True
17 default:
18 description:
19 - What to return if a variable is undefined.
20 - If no default is set, it will result in an error if any of the variables is undefined.
21 seealso:
22 - plugin_type: lookup
23 plugin: ansible.builtin.varnames
24
25 """
26
27 EXAMPLES = """
28 - name: Show value of 'variablename'
29 ansible.builtin.debug: msg="{{ lookup('ansible.builtin.vars', 'variabl' + myvar) }}"
30 vars:
31 variablename: hello
32 myvar: ename
33
34 - name: Show default empty since I don't have 'variablnotename'
35 ansible.builtin.debug: msg="{{ lookup('ansible.builtin.vars', 'variabl' + myvar, default='') }}"
36 vars:
37 variablename: hello
38 myvar: notename
39
40 - name: Produce an error since I don't have 'variablnotename'
41 ansible.builtin.debug: msg="{{ q('vars', 'variabl' + myvar) }}"
42 ignore_errors: True
43 vars:
44 variablename: hello
45 myvar: notename
46
47 - name: Find several related variables
48 ansible.builtin.debug: msg="{{ query('ansible.builtin.vars', 'ansible_play_hosts', 'ansible_play_batch', 'ansible_play_hosts_all') }}"
49
50 - name: Show values from variables found via varnames (note "*" is used to dereference the list to a 'list of arguments')
51 debug: msg="{{ q('vars', *q('varnames', 'ansible_play_.+')) }}"
52
53 - name: Access nested variables
54 ansible.builtin.debug: msg="{{ lookup('ansible.builtin.vars', 'variabl' + myvar).sub_var }}"
55 ignore_errors: True
56 vars:
57 variablename:
58 sub_var: 12
59 myvar: ename
60
61 - name: Alternate way to find some 'prefixed vars' in loop
62 ansible.builtin.debug: msg="{{ lookup('ansible.builtin.vars', 'ansible_play_' + item) }}"
63 loop:
64 - hosts
65 - batch
66 - hosts_all
67 """
68
69 RETURN = """
70 _value:
71 description:
72 - value of the variables requested.
73 type: list
74 elements: raw
75 """
76
77 from ansible.errors import AnsibleTypeError
78 from ansible.plugins.lookup import LookupBase
79 from ansible.module_utils.datatag import native_type_name
80 from ansible._internal._templating import _jinja_bits
81
82
83 class LookupModule(LookupBase):
84 def run(self, terms, variables, **kwargs):
85 self.set_options(var_options=variables, direct=kwargs)
86
87 default = self.get_option('default')
88
89 ret = []
90
91 for term in terms:
92 if not isinstance(term, str):
93 raise AnsibleTypeError(f'Variable name must be {native_type_name(str)!r} not {native_type_name(term)!r}.', obj=term)
94
95 try:
96 value = variables[term]
97 except KeyError:
98 if default is None:
99 value = _jinja_bits._undef(f'No variable named {term!r} was found.')
100 else:
101 value = default
102
103 ret.append(value)
104
105 return self._templar._engine.template(ret)