前提:從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
