其他分享
首页 > 其他分享> > ansible 普通用户执行root权限操作

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

作者:互联网

https://blog.csdn.net/qq_35702095/article/details/90486379?utm_medium=distribute.pc_relevant.none-task-blog-title-2&spm=1001.2101.3001.4242

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

熟悉开机关机重启 2019-05-23 19:53:05  8757  收藏 6

分类专栏: ansible 文章标签: 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

标签:--,密码,ansible,pass,become,普通用户,root
来源: https://blog.csdn.net/xdmstar/article/details/110265263