| 1 |
# Copyright (c) Ansible project |
| 2 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 3 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 4 |
|
| 5 |
"""The community.general.random_words Ansible lookup plugin.""" |
| 6 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
DOCUMENTATION = r""" |
| 10 |
name: random_words |
| 11 |
author: |
| 12 |
- Thomas Sjögren (@konstruktoid) |
| 13 |
short_description: Return a number of random words |
| 14 |
version_added: "4.0.0" |
| 15 |
requirements: |
| 16 |
- xkcdpass U(https://github.com/redacted/XKCD-password-generator) |
| 17 |
description: |
| 18 |
- Returns a number of random words. The output can for example be used for passwords. |
| 19 |
- See U(https://xkcd.com/936/) for background. |
| 20 |
options: |
| 21 |
numwords: |
| 22 |
description: |
| 23 |
- The number of words. |
| 24 |
default: 6 |
| 25 |
type: int |
| 26 |
min_length: |
| 27 |
description: |
| 28 |
- Minimum length of words to make password. |
| 29 |
default: 5 |
| 30 |
type: int |
| 31 |
max_length: |
| 32 |
description: |
| 33 |
- Maximum length of words to make password. |
| 34 |
default: 9 |
| 35 |
type: int |
| 36 |
delimiter: |
| 37 |
description: |
| 38 |
- The delimiter character between words. |
| 39 |
default: " " |
| 40 |
type: str |
| 41 |
case: |
| 42 |
description: |
| 43 |
- The method for setting the case of each word in the passphrase. |
| 44 |
choices: ["alternating", "upper", "lower", "random", "capitalize"] |
| 45 |
default: "lower" |
| 46 |
type: str |
| 47 |
""" |
| 48 |
|
| 49 |
EXAMPLES = r""" |
| 50 |
- name: Generate password with default settings |
| 51 |
ansible.builtin.debug: |
| 52 |
var: lookup('community.general.random_words') |
| 53 |
# Example result: 'traitor gigabyte cesarean unless aspect clear' |
| 54 |
|
| 55 |
- name: Generate password with six, five character, words |
| 56 |
ansible.builtin.debug: |
| 57 |
var: lookup('community.general.random_words', min_length=5, max_length=5) |
| 58 |
# Example result: 'brink banjo getup staff trump comfy' |
| 59 |
|
| 60 |
- name: Generate password with three capitalized words and the '-' delimiter |
| 61 |
ansible.builtin.debug: |
| 62 |
var: lookup('community.general.random_words', numwords=3, delimiter='-', case='capitalize') |
| 63 |
# Example result: 'Overlabor-Faucet-Coastline' |
| 64 |
|
| 65 |
- name: Generate password with three words without any delimiter |
| 66 |
ansible.builtin.debug: |
| 67 |
var: lookup('community.general.random_words', numwords=3, delimiter='') |
| 68 |
# Example result: 'deskworkmonopolystriking' |
| 69 |
# https://www.ncsc.gov.uk/blog-post/the-logic-behind-three-random-words |
| 70 |
""" |
| 71 |
|
| 72 |
RETURN = r""" |
| 73 |
_raw: |
| 74 |
description: A single-element list containing random words. |
| 75 |
type: list |
| 76 |
elements: str |
| 77 |
""" |
| 78 |
|
| 79 |
from ansible.errors import AnsibleLookupError |
| 80 |
from ansible.plugins.lookup import LookupBase |
| 81 |
|
| 82 |
from ansible_collections.community.general.plugins.plugin_utils._lookup import check_for_no_terms |
| 83 |
|
| 84 |
try: |
| 85 |
from xkcdpass import xkcd_password as xp |
| 86 |
|
| 87 |
HAS_XKCDPASS = True |
| 88 |
except ImportError: |
| 89 |
HAS_XKCDPASS = False |
| 90 |
|
| 91 |
|
| 92 |
class LookupModule(LookupBase): |
| 93 |
"""The random_words Ansible lookup class.""" |
| 94 |
|
| 95 |
def run(self, terms, variables=None, **kwargs): |
| 96 |
if not HAS_XKCDPASS: |
| 97 |
raise AnsibleLookupError('Python xkcdpass library is required. Please install using "pip install xkcdpass"') |
| 98 |
|
| 99 |
self.set_options(var_options=variables, direct=kwargs) |
| 100 |
check_for_no_terms(self, terms=terms, direct=kwargs) |
| 101 |
method = self.get_option("case") |
| 102 |
delimiter = self.get_option("delimiter") |
| 103 |
max_length = self.get_option("max_length") |
| 104 |
min_length = self.get_option("min_length") |
| 105 |
numwords = self.get_option("numwords") |
| 106 |
|
| 107 |
words = xp.locate_wordfile() |
| 108 |
wordlist = xp.generate_wordlist(max_length=max_length, min_length=min_length, wordfile=words) |
| 109 |
|
| 110 |
values = xp.generate_xkcdpassword(wordlist, case=method, delimiter=delimiter, numwords=numwords) |
| 111 |
|
| 112 |
return [values] |