环境都为Centos 7.4主机
192.168.241.100管理节点
192.168.241.101被控端
192.168.241.102被控端
安装Ansible(保证自己的虚拟机是可以上网的)
[root@liudongyi ~]# yum install ansible -y
连接远程主机认证方式一:配置Ansible的主机名文件(添加两台主机节点使用ssh密码的方式进行验证)
[root@liudongyi ~]# vim /etc/ansible/hosts
[webservers]
192.168.241.101 ansible_ssh_user=root ansible_ssh_pass='centos'
192.168.241.102 ansible_ssh_user=root ansible_ssh_pass='centos'
保存后。我们进行测试以下绿色代表我们成功进行了ping
[root@liudongyi ~]# ansible webservers -m ping
方式二:采用ssh密钥认证
[root@liudongyi ~]# vim /etc/ansible/hosts
[webservers]
192.168.241.101
192.168.241.102
生成密钥:
[root@liudongyi ~]# ssh-keygen 一路回车
把我们生成的公钥复制到其他主机上
[root@liudongyi ~]# ssh-copy-id [email protected]
然后我们ssh到192.168.241.101就不需要输入密码了
同样的方式我们把公钥复制到102主机上,然后我们修改主机配置文件:
[root@liudongyi ~]# vim /etc/ansible/hosts
[webservers]
192.168.241.101 ansible_ssh_user=root ansible_ssh_key=/root/.ssh/id_rsa
192.168.241.102 ansible_ssh_user=root ansible_ssh_key=/root/.ssh/id_rsa