Debian Codegraph @

ansible_collections/community/general/plugins/lookup/random_pet.py

raw
1 # Copyright (c) 2021, Abhijeet Kasurde <[email protected]>
2 # Copyright (c) 2018, Ansible Project
3 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
4 # SPDX-License-Identifier: GPL-3.0-or-later
5
6 from __future__ import annotations
7
8 DOCUMENTATION = r"""
9 name: random_pet
10 author:
11 - Abhijeet Kasurde (@Akasurde)
12 short_description: Generates random pet names
13 version_added: '3.1.0'
14 requirements:
15 - petname U(https://github.com/dustinkirkland/python-petname)
16 description:
17 - Generates random pet names that can be used as unique identifiers for the resources.
18 options:
19 words:
20 description:
21 - The number of words in the pet name.
22 default: 2
23 type: int
24 length:
25 description:
26 - The maximal length of every component of the pet name.
27 - Values below V(3) are set to V(3) by petname.
28 default: 6
29 type: int
30 prefix:
31 description: A string to prefix with the name.
32 type: str
33 separator:
34 description: The character to separate words in the pet name.
35 default: "-"
36 type: str
37 """
38
39 EXAMPLES = r"""
40 - name: Generate pet name
41 ansible.builtin.debug:
42 var: lookup('community.general.random_pet')
43 # Example result: 'loving-raptor'
44
45 - name: Generate pet name with 3 words
46 ansible.builtin.debug:
47 var: lookup('community.general.random_pet', words=3)
48 # Example result: 'fully-fresh-macaw'
49
50 - name: Generate pet name with separator
51 ansible.builtin.debug:
52 var: lookup('community.general.random_pet', separator="_")
53 # Example result: 'causal_snipe'
54
55 - name: Generate pet name with length
56 ansible.builtin.debug:
57 var: lookup('community.general.random_pet', length=7)
58 # Example result: 'natural-peacock'
59 """
60
61 RETURN = r"""
62 _raw:
63 description: A one-element list containing a random pet name.
64 type: list
65 elements: str
66 """
67
68 try:
69 import petname
70
71 HAS_PETNAME = True
72 except ImportError:
73 HAS_PETNAME = False
74
75 from ansible.errors import AnsibleError
76 from ansible.plugins.lookup import LookupBase
77
78 from ansible_collections.community.general.plugins.plugin_utils._lookup import check_for_no_terms
79
80
81 class LookupModule(LookupBase):
82 def run(self, terms, variables=None, **kwargs):
83 if not HAS_PETNAME:
84 raise AnsibleError('Python petname library is required. Please install using "pip install petname"')
85
86 self.set_options(var_options=variables, direct=kwargs)
87 check_for_no_terms(self, terms=terms, direct=kwargs)
88 words = self.get_option("words")
89 length = self.get_option("length")
90 prefix = self.get_option("prefix")
91 separator = self.get_option("separator")
92
93 values = petname.Generate(words=words, separator=separator, letters=length)
94 if prefix:
95 values = f"{prefix}{separator}{values}"
96
97 return [values]