password_hash 函式取得雜湊值,否則會將明文 '123qwe' 存入 /etc/shadow 中,而無法使用。經過運算的結果要以雙大括號括起來取其結果,而整串文字要用雙引號括起來,如下顯示:
ceuser@deyu:~/ansible_pb$ vi users1.yml
ceuser@deyu:~/ansible_pb$ cat users1.yml
---
- hosts: dev
tasks:
- name: Ensure group "sharegrp" exists
ansible.builtin.group:
name: sharegrp
state: present
- name: Add user 'james'
ansible.builtin.user:
name: james
groups: sharegrp
password: "{{ '123qwe' | password_hash('sha512') }}"
ceuser@deyu:~/ansible_pb$ ansible-playbook users1.yml PLAY [dev] ********************************************************************* TASK [Gathering Facts] ********************************************************* ok: [kvm11] TASK [Ensure group "sharegrp" exists] ****************************************** ok: [kvm11] TASK [Add user 'james'] ******************************************************** [DEPRECATION WARNING]: Encryption using the Python crypt module is deprecated. The Python crypt module is deprecated and will be removed from Python 3.13. Install the passlib library for continued encryption functionality. This feature will be removed in version 2.17. Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg. changed: [kvm11] PLAY RECAP ********************************************************************* kvm11: ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
ceuser@deyu:~/ansible_pb$ ssh james@kvm11 james@kvm11's password:
[james@kvm11 ~]$ exit logout Connection to kvm11 closed.