准备环境如下:
序号 | IP | 角色 | 操作系统 |
1 | 172.26.94.193 | master | centos 7.6 |
2 | 172.26.94.192 | node1 | centos 7.6 |
3 | 172.26.94.194 | node2 | centos 7.6 |
修改各节点hosts文件:
vim /etc/hosts
172.26.94.193 master
172.26.94.194 node1
172.26.94.192 node2
master节点安装ansible:
yum -y install ansible
进行ansible的基础配置:
vim /etc/ansible/ansible.cfg
inventory = /etc/ansible/host #主机管理清单
remote_port = 22 #操作主机的端口
timeout = 10 #连接主机的超时时间
log_path = /var/log/ansible.log #设置日志路径
private_key_file = /root/.ssh/id_rsa #指定认证密钥的私钥
将主机信息加入主机清单:
[root@master ~]# vim /etc/ansible/hosts
[master]
172.26.94.193 ansible_ssh_user=root ansible_ssh_pass=123456
[web]
172.26.94.192 ansible_ssh_user=root
[db]
172.26.94.194 ansible_ssh_user=root
免密访问配置:
[root@master ~]# ssh-keygen
Generating public/private rsa key pair.
……
[root@master ~]# ssh-copy-id root@172.26.94.194
……
Now try logging into the machine, with: "ssh 'root@172.26.94.194'"
and check to make sure that only the key(s) you wanted were added.
[root@master ~]# ssh-copy-id root@172.26.94.192
……
Now try logging into the machine, with: "ssh 'root@172.26.94.192'"
and check to make sure that only the key(s) you wanted were added.
访问测试:
[root@master ~]# ansible db -a "df -h"
172.26.94.194 | CHANGED | rc=0 >>
……
[root@master ~]# ansible web -a "df -h"
172.26.94.192 | CHANGED | rc=0 >>
……