ansible 普通用户执行root权限操作

ansible -h可以看到有

--become (-b)

--become-method  

--become-user

--ask-become-pass (-K)大写K

四个参数,利用这四个参数可以在ansible命令和playbook中用普通用户执行root操作。

命令: ansible 10.12.3.27,10.31.2.38 -m shell -a 'date' -b --become-method su --become-user root --ask-become-pass(-K)大写K

输入密码后,相当于用root执行了date命令,可以把date换成 cat /etc/shadow验证root权限。 多个ip(主机列表)的主机需要相同的root密码(只用输入一次),密码不同可以分多次或者直接配好密码。

如果想配好密码,可以在/etc/ansible/hosts为每个机器添加密码或者为组添加密码。例如:

[groupa]

10.10.10.1 ansible_become_pass=rootpwd1

10.10.10.2 ansible_become_pass=rootpwd2

[groupb]

10.10.10.3

10.10.10.4

[groupb:vars]

ansible_become_pass=rootpwd34

如果失败可能是ssh过去后,执行su -后没有提示输入密码,应该是编码问题,在受控机器上执行echo export en_CN.utf-8 >>~/.bashrc,然后再执行ansible命令试试。

playbook:

---
- hosts: 10.12.3.28,10.12.3.27
  become: yes
  become_user: root
  become_method: su
  tasks:
  - name: date
    shell: date

 

发布了15 篇原创文章 · 获赞 5 · 访问量 9057

猜你喜欢

转载自blog.csdn.net/qq_35702095/article/details/90486379