Ansible添加具有sudo权限的用户
代码
- hosts: all
vars:
user: wangguan
password: "$6$rounds=656000$w.sF/w4vQWOOn4Xx$q0MHTEsT4jDvmEWiZ7jcK4l/7iqSxSrnDFxY9H5wmfMpkJcXk41tgTc5Jt8S8YJyla7iKaZL4NYHI2p4mJ9NW."
remote_user: root
tasks:
- name: Add user {{ user }}
user: name={{user}} comment="wangguan 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"
register: result
- name: Show info
debug: var=result.stdout verbosity=0
# pip install passlib
python -c “from passlib.hash import sha512_crypt; import getpass; print sha512_crypt.encrypt(getpass.getpass())”
执行结果
[root@ansible ansible]# ansible-playbook adduser.yml -k
SSH password:
PLAY [all] *******************************************************************************************************************************************************************************************************************************************************************
TASK [Gathering Facts] *******************************************************************************************************************************************************************************************************************************************************
ok: [192.168.61.30]
TASK [Add user wangguan] *********************************************************************************************************************************************************************************************************************************************************
changed: [192.168.61.30]
TASK [Config /etc/sudoers] ***************************************************************************************************************************************************************************************************************************************************
changed: [192.168.61.30] => (item=wangguan ALL=(ALL) NOPASSWD: ALL)
changed: [192.168.61.30] => (item=Defaults: wangguan !requiretty)
TASK [Show create user info] *******************************************************************************************************************************************************************************************************************************************************
ok: [192.168.61.30] => {
"result.stdout": "VARIABLE IS NOT DEFINED!"
}
PLAY RECAP *******************************************************************************************************************************************************************************************************************************************************************
192.168.61.30 : ok=4 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
作者:一毛
本博客所有文章仅用于学习、研究和交流目的,欢迎非商业性质转载。
不管遇到了什么烦心事,都不要自己为难自己;无论今天发生多么糟糕的事,都不应该感到悲伤。记住一句话:越努力,越幸运。