一、受控机/etc/sudoers配置
ansible的become提权配合linux的sudo提权。需要修改受控机的/etc/sudoers文件
受控端主机用root用户输入visudo添加如下配置
ans ALL=(ALL) /bin/sh,(另外加上你需要使用的命令)
#因为ansible模块shell、script、copy等模块都是使用/bin/sh -c命令执行的,
所以需要在sudo权限中添加/bin/sh,不然会报错一直执行不成功
也可以写成ans ALL=(ALL) ALL 或 ans ALL=(ALL) NOPASSWD:ALL
二、ansible inventory hosts文件配置
ansible控制端主机inventory hosts文件,与root用户连接的区别是多加了几个become的参数
【ansible_become=true ansible_become_user=root ansible_sudo_pass='1' ansible_become_method=sudo】
[web] # 用密钥连接,然后提权到root
172.16.1.7 ansible_ssh_private_key_file=/root/.ssh/id_rsa_tom ansible_ssh_user=ans ansible_ssh_port=22 ansible_become=true ansible_become_user=root ansible_sudo_pass='1' ansible_become_method=sudo
[nfs] # 用用户名密码连接,然后提权到root
172.16.1.31 ansible_password='1' ansible_ssh_user=ans ansible_ssh_port=22 ansible_become=true ansible_become_user=root ansible_sudo_pass='1' ansible_become_method=sudo