1 修改ansible.cfg文件
将become前两行注释取消
[privilege_escalation]
become=True
become_method=sudo
#become_user=root
#become_ask_pass=False
2 补充hosts信息
ip ansible_ssh_user=**** ansible_ssh_pass='****' ansible_become_pass='****'
3 安装sshpass
# ubantu
sudo apt-get install sshpass
# centos
yum intall sshpass
4 执行命令
# 修改前
python@ubuntu:~$ ansible webserver -m shell -a "touch /root/test.txt"
192.168.138.137 | FAILED | rc=1 >>
touch: 无法创建'/root/test.txt': 权限不够
# 未安装sshpass
python@ubuntu:/etc/ansible$ ansible webserver -m shell -a "touch /root/test.txt"192.168.138.137 | FAILED | rc=0 >>
ERROR! to use the 'ssh' connection type with passwords, you must install the sshpass program
# 全部修改完毕后
python@ubuntu:~$ ansible webserver -m shell -a "touch /root/test.txt"
192.168.138.137 | SUCCESS | rc=0 >>