- name: Add new SFTP user (user provided by prompt)' hosts: '{{ target }}' vars: pwd_alias: "{{ lookup('password', '/dev/null length=15 chars=ascii_letters') }}" user_to_add: "{{ newuser }}" tasks: - set_fact: my_pass: "{{ pwd_alias | password_hash('sha512') }}" - name: Create user user: name: "{{ user_to_add }}" password: "{{ my_pass }}" shell: /bin/bash create_home: yes home: "/home/sftp/{{ user_to_add }}" group: cgred - debug: msg: "{{ pwd_alias }},{{ my_pass }}"当我运行它时,它会做它应该做的事情。它创建我指定的用户和主目录。它还会打印出密码和哈希值,但无论我做什么,我都无法记录对该用户的更改。任何有关如何正确获取分配给该用户的密码的建议将不胜感激。
1 回答
慕沐林林
TA贡献2016条经验 获得超9个赞
问题是pwd_alias,当放入 , 时vars,每次引用都会被评估。比如下面的戏
vars:
pwd_alias: "{{ lookup('password', '/dev/null length=15 chars=ascii_letters') }}"
tasks:
- debug: var=pwd_alias
- debug: var=pwd_alias
- debug: var=pwd_alias
给
"pwd_alias": "RrhCtAFEHievoTY"
"pwd_alias": "TxHCsdKlpweqVJL"
"pwd_alias": "xbFLVvuMkkNkqIE"
解决方案很简单。将评估pwd_alias放入任务中。例如
tasks:
- set_fact:
pwd_alias: "{{ lookup('password', '/dev/null length=15 chars=ascii_letters') }}"
- set_fact:
my_pass: "{{ pwd_alias | password_hash('sha512') }}"
添加回答
举报
0/150
提交
取消