File: casting.py

package info (click to toggle)
ansible-core 2.19.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 32,752 kB
  • sloc: python: 181,000; cs: 4,929; sh: 4,611; xml: 34; makefile: 21
file content (59 lines) | stat: -rw-r--r-- 1,638 bytes parent folder | download | duplicates (3)
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
# (c) 2021 Ansible Project
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

from __future__ import annotations


DOCUMENTATION = """
    name: casting
    author: Ansible Core Team
    version_added: histerical
    short_description: returns what you gave it
    description:
      - this is mostly a noop
    options:
        _terms:
            description: stuff to pass through
        test_list:
            description: does nothihng, just for testing values
            type: list
        test_int:
            description: does nothihng, just to test casting
            type: int
        test_bool:
            description: does nothihng, just to test casting
            type: bool
        test_str:
            description: does nothihng, just to test casting
            type: str
"""

EXAMPLES = """
- name: like some other plugins, this is mostly useless
  debug: msg={{ q('casting', [1,2,3])}}
"""

RETURN = """
  _list:
    description: basically the same as you fed in
    type: list
    elements: raw
"""

from ansible.plugins.lookup import LookupBase


class LookupModule(LookupBase):

    def run(self, terms, variables=None, **kwargs):

        self.set_options(var_options=variables, direct=kwargs)

        for cast in (list, int, bool, str):
            option = 'test_%s' % str(cast).replace("<class '", '').replace("'>", '')
            value = self.get_option(option)
            if value is None or isinstance(value, cast):
                continue
            raise Exception('%s is not a %s: got %s/%s' % (option, cast, type(value), value))

        return terms