利用playbook配置不同版本系统的yum源
环境说明:
主机名和IP地址 | 系统版本 |
---|---|
ansible 192.168.153.10 | redhat 8.2 |
redhat8 192.168.153.11 | redhat 8.2 |
centos8 192.168.153.12 | centos 8 |
centos7 192.168.153.13 | centos7 |
项目结构
[root@ansible yum]# tree
.
├── ansible.cfg
├── inventory
├── scripts
│ ├── yum_centos7.sh
│ └── yum_centos8.sh
└── yum.yml
1 directory, 5 files
准备工作
本次环境yum源是阿里云官方镜像网站
//映射主机名IP
[root@ansible ~]# vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.153.10 ansible
192.168.153.11 redhat8
192.168.153.12 centos8
192.168.153.13 centos7
//编写inventory清单
[root@ansible ~]# mv /etc/ansible/inventory yum/ //把清单文件移到yum目录下
[root@ansible yum]# vim inventory
[redhat]
redhat8
[centos]
centos7
centos8
//修改清单的位置
[root@ansible ~]# mv /etc/ansible/ansible.cfg yum/ //把ansible.cfg移到yum目录下
[root@ansible yum]# vim ansible.cfg
# some basic default values...
inventory = ./inventory //当前目录的inventory
//设置免密登录
[root@ansible yum]# ssh-keygen -t rsa //生成密钥,直接回车
[root@ansible yum]# ssh-copy-id root@redhat8
[root@ansible yum]# ssh-copy-id root@centos8
[root@ansible yum]# ssh-copy-id root@centos7
//测试能否ping通
[root@ansible yum]# ansible all -m ping
redhat8 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
centos8 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
centos7 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
配置yum脚本
//编写centos8的yum配置批处理
[root@ansible yum]# vim scripts/yum_centos8.sh
#!/bin/bash
#centos8
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-8.repo
sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
sed -i 's|$releasever|8|' /etc/yum.repos.d/CentOS-Base.repo
#epel
yum install -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm
sed -i 's|^#baseurl=https://download.fedoraproject.org/pub|baseurl=https://mirrors.aliyun.com|' /etc/yum.repos.d/epel*
sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel*
#yum clean
yum clean all
yum makecache
//编写centos7的yum配置批处理
[root@ansible yum]# vim scripts/yum_centos7.sh
#!/bin/bash
#centos7
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
sed -i 's|$releasever|7|' /etc/yum.repos.d/CentOS-Base.repo
#epel
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
sed -i 's|$releasever|7|' /etc/yum.repos.d/epel*
#yum clean
yum clean all
yum makecache
编写yum的playbook
[root@ansible yum]# vim yum.yml
---
- name: yum
hosts: all
tasks:
- name: centos8
script: ./scripts/yum_centos8.sh
when: >
( ansible_facts["distribution"] == "RedHat" and
ansible_facts["distribution_major_version"] == "8" )
or
( ansible_facts["distribution"] == "CentOS" and
ansible_facts["distribution_major_version"] == "8" )
- name: centos7
script: ./scripts/yum_centos7.sh
when: >
( ansible_facts["distribution"] == "RedHat" and
ansible_facts["distribution_major_version"] == "7" )
or
( ansible_facts["distribution"] == "CentOS" and
ansible_facts["distribution_major_version"] == "7" )
执行剧本
[root@ansible yum]# ansible-playbook yum.yml
PLAY [yum] *************************************************************************
TASK [Gathering Facts] *************************************************************
ok: [redhat8]
ok: [centos8]
ok: [centos7]
TASK [centos8] *********************************************************************
changed: [redhat8]
changed: [centos8]
changed: [centos7]
TASK [centos7] *********************************************************************
skipping: [redhat8]
skipping: [centos8]
skipping: [centos7]
PLAY RECAP *************************************************************************
centos8 : ok=2 changed=1 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
centos7 : ok=2 changed=1 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
redhat8 : ok=2 changed=1 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
验证一下
//在centos7主机上查看一下
[root@centos7 ~]# yum repolist
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
repo id repo name status
base/x86_64 CentOS-7 - Base - mirrors.aliyun.com 10,072
epel/x86_64 Extra Packages for Enterprise Linux 7 - x86_64 13,492
extras/x86_64 CentOS-7 - Extras - mirrors.aliyun.com 448
//在centos8主机上查看一下
[root@centos8 ~]# yum repolist
Repository AppStream is listed more than once in the configuration
Repository extras is listed more than once in the configuration
Repository PowerTools is listed more than once in the configuration
Repository centosplus is listed more than once in the configuration
repo id repo name
AppStream CentOS-8 - AppStream
base CentOS-8 - Base - mirrors.aliyun.com
epel Extra Packages for Enterprise Linux 8 - x86_64
epel-modular Extra Packages for Enterprise Linux Modular 8 - x86_64
extras CentOS-8 - Extras - mirrors.aliyun.com
//在redhat8主机查看一下
[root@redhat8 ~]# yum repolist
Updating Subscription Management repositories.
Unable to read consumer identity
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
repo id repo name
AppStream CentOS-8 - AppStream - mirrors.aliyun.com
base CentOS-8 - Base - mirrors.aliyun.com
epel Extra Packages for Enterprise Linux 8 - x86_64
epel-modular Extra Packages for Enterprise Linux Modular 8 - x86_64
extras CentOS-8 - Extras - mirrors.aliyun.com