提问人:Ziad 提问时间:5/17/2023 最后编辑:U880DZiad 更新时间:5/21/2023 访问量:190
使用 Ansible 生成具有非常特定属性的随机密码
Using Ansible to generate a random password with very specific properties
问:
我有以下代码,它完美地为我生成了一个长度在 20 到 25 之间的密码,其中包含我想要的字符。但是,我注意到有时相同的字符会彼此相邻重复。
有没有一种方法或代码可以使字符不会在生成的密码中重复?
--- #This is how a yml file starts
- set_fact:
size:
- "{{20 + (5 | random )}}"
- debug:
var: size
- name: create a passowrd of length "{{ size }}"
set_fact:
random_password: "{{ lookup('password', '/dev/null length={{ size | random }} chars=ascii_lowercase,ascii_uppercase,digits,_-!.,;:') }}"
- name: Debug to show password created above
debug:
msg: "{{random_password}}"
我不太确定这部分该怎么做。
答:
0赞
U880D
5/19/2023
#1
...有时,相同的字符会彼此相邻重复。有没有一种方法或代码可以使字符不会在生成的密码中重复?
一个最小的例子
task.yml
---
- name: Create a password
set_fact:
random_password: "{{ lookup('password', '/dev/null length={{ 20 + (5 | random) | int }} chars=ascii_lowercase,ascii_uppercase,digits,_-!.,;:') }}"
playbook.yml
---
- hosts: localhost
become: false
gather_facts: false
tasks:
- name: Generate a random_password
include: task.yml
until: random_password is not match('.*(.)\\1.*')
- debug:
msg: "{{ random_password }}"
可能导致输出
TASK [debug] ***************
ok: [localhost] =>
msg: tN.NsnTmm6:NInnbd6Fe4
还有一个最小的例子
---
- hosts: localhost
become: false
gather_facts: false
tasks:
- name: Create a password
set_fact:
random_password: "{{ lookup('password', '/dev/null length={{ 20 + (5 | random) | int }} chars=ascii_lowercase,ascii_uppercase,digits,_-!.,;:') }}"
until: random_password is not match('.*(.)\\1.*')
retries: 10
可能导致输出
PLAY [localhost] **************************************
FAILED - RETRYING: Create a password (10 retries left).
FAILED - RETRYING: Create a password (9 retries left).
FAILED - RETRYING: Create a password (8 retries left).
FAILED - RETRYING: Create a password (7 retries left).
FAILED - RETRYING: Create a password (6 retries left).
FAILED - RETRYING: Create a password (5 retries left).
FAILED - RETRYING: Create a password (4 retries left).
FAILED - RETRYING: Create a password (3 retries left).
FAILED - RETRYING: Create a password (2 retries left).
FAILED - RETRYING: Create a password (1 retries left).
TASK [Create a password] ******************************
fatal: [localhost]: FAILED! => changed=false
ansible_facts:
random_password: 'J2LLMsf5CKFOvg8x3S8rub:'
attempts: 10
它可能会第一次生成,之后不会更新。这是因为 Ansible 的本质,变量是如何注册的,任务是如何编译和执行的。
要解决该行为,有必要将 放入 然后阻止它。lookup
until
register
有没有办法......我可以用...
由于 Ansible 不是一种编码语言,如果您的用例是生成具有此类特定属性的随机字符串,因此似乎建议编写一个小脚本或自定义模块来执行任务并仅提供所需的结果,这些结果可以注册。进一步研究的起点可以是
评论
until: random_password is not match('.*(.)\\1.*')
match
re.search