Ansible基础Centos 7.4

环境都为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

然后我们进行测试ping操作:

简单命令测试:

ansible all -m ping
ansible all -m shell -a "ls /root" -u root -k 
ansible webservers -m copy –a "src=/etc/hosts dest=/tmp/hosts"

 

发布了48 篇原创文章 · 获赞 3 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_40210617/article/details/104221701
7.4