方式一、 1、配置資源清單inventory文件 [root@test1 ~]# cat >/etc/ansible/hosts <<EOF [k8s] 192.168.0.92 ansible_ssh_port=22 ansible_ssh_user=k8s ansible_ssh_pass='123' ansible_become_pass='123456' EOF 解釋: ansible_ssh_pass='123' 是客戶端普通用戶的密碼 ansible_become_pass='123456' 是客戶端root用戶的密碼 2、Ansile客戶端禁止root直接遠程登陸,並創建普通用戶ywbz [root@test2 ~]# grep -n "PermitRootLogin" /etc/ssh/sshd_config PermitRootLogin no [root@test2 ~]# systemctl restart sshd [root@test2 ~]# useradd k8s [root@test2 ~]# echo '123' | passwd --stdin k8s Changing password for user k8s. passwd: all authentication tokens updated successfully. [root@test1 ~]# 3、服務端測試執行命令 [k8s@test1 root]$ ansible test3 -m shell -a 'chmod +x /home/*' 4、但是無法創建用戶 方式二、 1、給客戶端普通用戶配置sudo權限 2、配置主機組 cat >/etc/ansible/hosts<<EOF [k8s] 192.168.0.92 [test3] 192.168.0.93 3、執行 sudo ansible k8s -m shell -a 'chmod +x /home/*'