环境准备: 管理机ip(类比服务端ip): 192.168.124.73 被管理机ip(类比客户端ip):192.168.124.147 1.安装ansible yum install ansible -y 2.查看ansible版本 ansible --version 3.生成公钥私钥 ssh-keygen

Enter passphrase (empty for no passphrase):(回车) Enter same passphrase again:(回车) 4.进到.ssh目录下 cd .ssh

5.将公钥上传到被管理机 ssh-copy-id -i root@192.168.124.147(被管理机ip)

Are you sure you want to continue connecting (yes/no)? yes root@192.168.124.147's password: (输入被管理机的密码)

此时 被管理机的.ssh目录下会自动生成一个 authorized_keys(认证过的密钥) 6. 编辑配置文件 vim /etc/ansible/hosts

在末端添加一个模块 [test-servers] 192.168.124.147(被管理机ip) 7.测试连接 ansible -m ping 'test-servers'

会出现: 192.168.1.129 | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": false, "ping": "pong" } 8.此时我们可以测试操作一下,将管理机上家目录下的we.sh文件传到被管理机的/tmp目录下 ansible test-servers -m copy -a "src=/root/we.sh dest=/tmp/ owner=root group=root mode=0755 force=yes"

此时,在管理机上会显示传输成功 在被管理机的/tmp目录下会接收到管理机传输的文件