前提:从ansible控制端使用test用户可以免密登陆所有被控制端,并且被控端test用户支持sudo提权 # ansible主机清单 cat /etc/ansible/hosts [online-a1] 172.17.149.162 ansible_ssh_user=test [online-a2] 172.17.149.140 ansible_ssh_user=test [online-a3] 172.17.149.120 ansible_ssh_user=test [online-a4] 172.17.149.145 ansible_ssh_user=test [online-as] 172.17.149.162 ansible_ssh_user=test 172.17.149.140 ansible_ssh_user=test 172.17.149.120 ansible_ssh_user=test 172.17.149.145 ansible_ssh_user=test # ansible配置文件 /etc/ansible/ansible.cfg 取消注释的行: remote_tmp = ~/.ansible/tmp inventory = /etc/ansible/hosts host_key_checking = False # yaml文件内容 cat test.yaml - hosts: online-as become: true tasks: - name: just test shell: mkdir -p /root/test/test2 # 命令行ansible-doc测试 ansible online-as -m shell -a 'ls -lh /root/' -become=true