如何用ansible生成随机密码

时间:2017-10-13 14:54:16

标签: ansible

或者我应该问:如何只评估一次值?

Table name: images

id      project_name        imagelink       img_timestamp
1       Travier             image1          (timestamp of upload)
2       Travier             image2          (timestamp of upload)
3       Travier             image3          (timestamp of upload)
4       Travier             image4          (timestamp of upload)
5       Collosion           image1          (timestamp of upload)
6       Collosion           image2          (timestamp of upload)
7       Collosion           image3          (timestamp of upload)

每个调试语句将打印出不同的值,例如:

- name: Demo
  hosts: localhost
  gather_facts: False
  vars:
    my_pass: "{{ lookup('password', '/dev/null length=15 chars=ascii_letters') }}"
  tasks:
  - debug:
      msg: "{{ my_pass }}"
  - debug:
      msg: "{{ my_pass }}"
  - debug:
      msg: "{{ my_pass }}"

ansible 2.3.2.0

3 个答案:

答案 0 :(得分:14)

使用set_fact分配永久事实:

- name: Demo
  hosts: localhost
  gather_facts: False
  vars:
    pwd_alias: "{{ lookup('password', '/dev/null length=15 chars=ascii_letters') }}"
  tasks:
    - set_fact:
        my_pass: "{{ pwd_alias }}"
    - debug:
        msg: "{{ my_pass }}"
    - debug:
        msg: "{{ my_pass }}"
    - debug:
        msg: "{{ my_pass }}"

答案 1 :(得分:0)

我一直在这样做,从来没有问题。

- name: Demo
  hosts: localhost
  gather_facts: False 

  tasks:
   - set_fact:
       my_pass: "{{ lookup('password', '/dev/null length=15 chars=ascii_letters') }}"
   - debug:
       msg: "{{ my_pass }}"

答案 2 :(得分:0)

查找密码很好,但是如果你有密码规范,比如它必须包含特定字符,或者不能包含大写......如果需要,查找也不保证密码将包含特殊字符...

我以自定义 jinja 过滤器结束,这可能对某人有所帮助(对我来说很好用 :))

https://gitlab.privatecloud.sk/vladoportos/custom-jinja-filters