编辑playbook

vim addpasswd.yml

---
- hosts: all
vars:
user: nedyl
password: "$6$rounds=656000$MamEUGNFqtAUVais$tlIOtSalxSsRL3RcIrofg304JC0FKrvOdxOQcOnopEKh/bvg3I9HI3rl5zFiUmv8Q8ZkPvUp8u/htVm/OgYyX."
remote_user: root
tasks:
- name: Add user {{ user }}
user: name={{user}} comment="ceph user" password={{ password }}
- name: Config /etc/sudoers
lineinfile: dest=/etc/sudoers state=present line='{{item}}' validate='visudo -cf %s'
with_items:
- "{{ user}} ALL=(ALL) NOPASSWD: ALL"
- "Defaults: {{user}} !requiretty"

此playbook将添加一个具有sudo权限的 nedyl 用户 密码为woshinedy@14

更改用户只用更改vars:中的user 即可

生成密码:

#pip install passlib
python -c “from passlib.hash import sha512_crypt; import getpass; print sha512_crypt.encrypt(getpass.getpass())”
输入密码后确认

会生成一长串字符替换playbook中的即可

例如:

ansible添加具有sudo权限的用户_用户名


用户名:nedyl

密 码:woshinedy@14执行:

ansible-playbook addpasswd.yml

ansible添加具有sudo权限的用户_vim_02


测试

ansible添加具有sudo权限的用户_python_03