设备4台
设备1 ansibie 192.168.70.80
设备2 web1 192.168.70.70
设备3 web2 192.168.70.60
设备4 web3 192.168.70.50
设备1
[root@localhost ~]# hostnamectl set-hostname server
设备2、3、4
[root@localhost ~]# hostnamectl set-hostname web1
[root@localhost ~]# hostnamectl set-hostname web2
[root@localhost ~]# hostnamectl set-hostname web3
设备1
关闭防火墙、内核
[root@server ~]# systemctl stop firewalld
[root@server ~]# systemctl disable firewalld
[root@server ~]# setenforce 0
[root@server ~]# sed -i 's/SELINUX=enforcing/SELINUX=permissive/' /etc/selinux/config
生成秘钥
[root@server ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:C/YMTOosyDFBDMPLaGdYApQix2kemBNn24OEgiIvFMg root@server
The key's randomart image is:
+---[RSA 2048]----+
|#X*. |
|#E*= |
|XBB.o . |
|o=+o = |
|.+o . = S |
|..oo . = . |
|... o + |
| . |
| |
+----[SHA256]-----+
hosts解析
[root@server ~]# vi /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.70.70 web1
192.168.70.60 web2
192.168.70.50 web3
批量修改web1 web2 web3 防火墙 、内核、复制秘钥
[root@server ~]# for i in web1 web2 web3
> do
> ssh-copy-id $i
> ssh $i systemctl stop firewalld
> ssh $i setenforce 0
> ssh $i sed -i 's/SELINUX=enforcing/SELINUX=permissive/' /etc/SELINUX/config;
> done
安装ansible
[root@server ~]# yum -y install epel-release
[root@server ~]# yum -y install ansible
查看版本
[root@server ~]# ansible --version
[root@server ~]# vi /etc/ansible/hosts #添加以下信息
[test-servers]
192.168.70.70
192.168.70.60
192.168.70.50
测试连通性
- [root@server ~]# ansible -m ping 'test-servers'
- [WARNING]: Invalid characters were found in group names but not replaced, use -vvvv to see details
- 192.168.70.50 | SUCCESS => {
- "ansible_facts": {
- "discovered_interpreter_python": "/usr/bin/python"
- },
- "changed": false,
- "ping": "pong"
- }
- 192.168.70.70 | SUCCESS => {
- "ansible_facts": {
- "discovered_interpreter_python": "/usr/bin/python"
- },
- "changed": false,
- "ping": "pong"
- }
- 192.168.70.60 | SUCCESS => {
- "ansible_facts": {
- "discovered_interpreter_python": "/usr/bin/python"
- },
- "changed": false,
- "ping": "pong"
- }
利用shell模块批量安装
安装必要的一些系统工具
[root@server ~]# ansible test-servers -m shell -a 'yum install -y yum-utils device-mapper-persistent-data lvm2'
添加软件源信息
[root@server ~]# ansible test-servers -m shell -a 'yum install -y yum-utils device-mapper-pe'
rs.aliyun.com/docker-ce/linux/centos/docker-ce.repo'
[root@server ~]# ansible test-servers -m shell -a 'sed -i 's+download.docker.com+mirrors.aliyun.com/docker-ce+' /etc/yum.repos.d/docker-ce.repo'
更新并安装Docker-CE
[root@server ~]# ansible test-servers -m shell -a 'yum makecache fast'
[root@server ~]# ansible test-servers -m shell -a 'yum -y install docker-ce'
查看版本
[root@server ~]# ansible test-servers -m shell -a 'docker -v'
开启Docker服务
[root@server ~]# ansible test-servers -m shell -a 'systemctl start docker'
安装结束