
    Vh|                     ~    d dl mZmZmZ eZdZdZdZd dl	Z	d dl
Z
d dlZd dlmZ d dlmZ d dlmZmZ  G d	 d
e      Zy)    )absolute_importdivisionprint_functionaj
  
name: random_string
author:
  - Abhijeet Kasurde (@Akasurde)
short_description: Generates random string
version_added: '3.2.0'
description:
  - Generates random string based upon the given constraints.
  - Uses L(random.SystemRandom,https://docs.python.org/3/library/random.html#random.SystemRandom), so should be strong enough
    for cryptographic purposes.
options:
  length:
    description: The length of the string.
    default: 8
    type: int
  upper:
    description:
      - Include uppercase letters in the string.
    default: true
    type: bool
  lower:
    description:
      - Include lowercase letters in the string.
    default: true
    type: bool
  numbers:
    description:
      - Include numbers in the string.
    default: true
    type: bool
  special:
    description:
      - Include special characters in the string.
      - Special characters are taken from Python standard library C(string).
        See L(the documentation of string.punctuation,https://docs.python.org/3/library/string.html#string.punctuation)
        for which characters will be used.
      - The choice of special characters can be changed to setting O(override_special).
    default: true
    type: bool
  min_numeric:
    description:
      - Minimum number of numeric characters in the string.
      - If set, overrides O(numbers=false).
    default: 0
    type: int
  min_upper:
    description:
      - Minimum number of uppercase alphabets in the string.
      - If set, overrides O(upper=false).
    default: 0
    type: int
  min_lower:
    description:
      - Minimum number of lowercase alphabets in the string.
      - If set, overrides O(lower=false).
    default: 0
    type: int
  min_special:
    description:
      - Minimum number of special character in the string.
    default: 0
    type: int
  override_special:
    description:
      - Override a list of special characters to use in the string.
      - If set O(min_special) should be set to a non-default value.
    type: str
  override_all:
    description:
      - Override all values of O(numbers), O(upper), O(lower), and O(special) with the given list of characters.
    type: str
  ignore_similar_chars:
    description:
      - Ignore similar characters, such as V(l) and V(1), or V(O) and V(0).
      - These characters can be configured in O(similar_chars).
    default: false
    type: bool
    version_added: 7.5.0
  similar_chars:
    description:
      - Override a list of characters not to be use in the string.
    default: "il1LoO0"
    type: str
    version_added: 7.5.0
  base64:
    description:
      - Returns base64 encoded string.
    type: bool
    default: false
a  
- name: Generate random string
  ansible.builtin.debug:
    var: lookup('community.general.random_string')
  # Example result: 'DeadBeeF'

- name: Generate random string with length 12
  ansible.builtin.debug:
    var: lookup('community.general.random_string', length=12)
  # Example result: 'Uan0hUiX5kVG'

- name: Generate base64 encoded random string
  ansible.builtin.debug:
    var: lookup('community.general.random_string', base64=True)
  # Example result: 'NHZ6eWN5Qk0='

- name: Generate a random string with 1 lower, 1 upper, 1 number and 1 special char (at least)
  ansible.builtin.debug:
    var: lookup('community.general.random_string', min_lower=1, min_upper=1, min_special=1, min_numeric=1)
  # Example result: '&Qw2|E[-'

- name: Generate a random string with all lower case characters
  ansible.builtin.debug:
    var: query('community.general.random_string', upper=false, numbers=false, special=false)
  # Example result: ['exolxzyz']

- name: Generate random hexadecimal string
  ansible.builtin.debug:
    var: query('community.general.random_string', upper=false, lower=false, override_special=hex_chars, numbers=false)
  vars:
    hex_chars: '0123456789ABCDEF'
  # Example result: ['D2A40737']

- name: Generate random hexadecimal string with override_all
  ansible.builtin.debug:
    var: query('community.general.random_string', override_all=hex_chars)
  vars:
    hex_chars: '0123456789ABCDEF'
  # Example result: ['D2A40737']
zb
_raw:
  description: A one-element list containing a random string.
  type: list
  elements: str
N)AnsibleLookupError)
LookupBase)to_bytesto_textc                   6    e Zd Zed        Zedd       ZddZy)LookupModulec                 h     st        d      dj                   fdt        |      D              S )Nz>Available characters cannot be None, please change constraints c              3   @   K   | ]  }j                          y wN)choice).0dummycharsrandom_generators     r/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/lookup/random_string.py	<genexpr>z*LookupModule.get_random.<locals>.<genexpr>   s     Q%'..u5Qs   )r   joinrange)r   r   lengths   `` r   
get_randomzLookupModule.get_random   s1    $P  wwQ5=QQQ    c           	      V    t        t        j                  t        | |d                  S )Nsurrogate_or_strict)encodingerrors)r	   base64	b64encoder   )string_valuer   s     r   r!   zLookupModule.b64encode   s*    AVW
 	
r   Nc           	      D   t         j                  }t         j                  }t         j                  }t         j                  }t        j                         }| j                  ||       | j                  d      }	| j                  d      }
| j                  d      }| j                  d      }| j                  d      }d}d}|rdj                  |D cg c]	  }||vs| c}      }dj                  |D cg c]	  }||vs| c}      }dj                  |D cg c]	  }||vs| c}      }dj                  |D cg c]	  }||vs| c}      }|r|}n| j                  d      }| j                  d	      }| j                  d
      }| j                  d      }| j                  d      }|r|}|r||z  }|r||z  }|r||z  }|r||z  }||||d}|D ]<  }| j                  |      s|| j                  |||   | j                  |            z  }> |	t        |      z
  }|| j                  |||      z  }t        |      }t        j                  |       |
r!| j                  dj                  |            gS dj                  |      gS c c}w c c}w c c}w c c}w )N)var_optionsdirectr   r    override_allignore_similar_charssimilar_charsr   upperlowernumbersspecialoverride_special)min_numeric	min_lower	min_uppermin_special)stringdigitsascii_lowercaseascii_uppercasepunctuationrandomSystemRandomset_options
get_optionr   r   lenlistshuffler!   )selfterms	variableskwargsnumber_charslower_charsupper_charsspecial_charsr   r   base64_flagr&   r'   r(   valuesavailable_chars_setscr)   r*   r+   r,   r-   mappingmremaining_pass_lenshuffled_valuess                             r   runzLookupModule.run   s   }},,,,**!..0Yv>*ooh/~6#/EF8 77#Y2=AXB#YZL''"W"r?V2"WXK''"W"r?V2"WXKGG-$[B2]CZR$[\M".OOG,EOOG,Eooi0Gooi0G#/AB 0#{2##{2##|3##}4#  ,((,	G  `??1%doo.>
DOO\]L^__F` $c&k1$//"24GI[\\ v,'NN277?#;<==())c $Z"W"W$[s0   	JJ6	J J	J$J>	JJ)zutf-8r   )__name__
__module____qualname__staticmethodr   r!   rN    r   r   r   r      s0    R R 
 
C*r   r   )
__future__r   r   r   type__metaclass__DOCUMENTATIONEXAMPLESRETURNr    r7   r2   ansible.errorsr   ansible.plugins.lookupr   +ansible.module_utils.common.text.convertersr   r	   r   rS   r   r   <module>r]      sM    A @Yv'R
    - - IT*: T*r   