ansible添加具有sudo权限的用户
原创
©著作权归作者所有:来自51CTO博客作者~午後的旅行的原创作品,请联系作者获取转载授权,否则将追究法律责任
编辑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中的即可
例如:
用户名:nedyl
密 码:woshinedy@14执行:
ansible-playbook addpasswd.yml
测试