利用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
└── yum.yml
0 directories, 3 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的playbook
[root@ansible yum]# vim yum.yml
---
- hosts: all
tasks:
- name: CentOS7_base
yum_repository:
name: base
baseurl: https://mirrors.aliyun.com/centos/7/os/x86_64/
enabled: yes
gpgcheck: no
mode: 0644
file: base
description: base
state: present
when:
- ansible_facts["distribution"] == "CentOS"
- ansible_facts["distribution_major_version"] == "7"
- name: CentOS7_epel
yum_repository:
name: epel
description: EPEL YUM repo
file: epel
baseurl: https://mirrors.aliyun.com/epel/7/x86_64
gpgcheck: no
mode: 0644
state: present
when:
- ansible_facts["distribution"] == "CentOS"
- ansible_facts["distribution_major_version"] == "7"
- name: yum_RedHat8 yum_CentOS8
loop:
- AppStream
- BaseOS
yum_repository:
name: "{{ item }}"
description: "{{ item }}"
file: "{{ item }}"
baseurl: https://mirrors.aliyun.com/centos/8/{{ item }}/x86_64/os/
gpgcheck: no
mode: 0644
state: present
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: epel_RedHat8 epel_CentOS8
yum_repository:
name: epel
description: EPEL YUM repo
file: epel
baseurl: https://mirrors.aliyun.com/epel/8/Modular/x86_64//
gpgcheck: no
mode: 0644
state: present
when: >
( ansible_facts["distribution"] == "RedHat" and
ansible_facts["distribution_major_version"] == "8" )
or
( ansible_facts["distribution"] == "CentOS" and
ansible_facts["distribution_major_version"] == "8" )
执行剧本
[root@ansible yum]# ansible-playbook yum.yml
PLAY [all] *************************************************************************
TASK [Gathering Facts] *************************************************************
ok: [redhat8]
ok: [centos7]
ok: [centos8]
TASK [CentOS7_base] ****************************************************************
skipping: [redhat8]
skipping: [centos8]
changed: [centos7]
TASK [CentOS7_epel] ****************************************************************
skipping: [redhat8]
skipping: [centos8]
changed: [centos7]
TASK [yum_RedHat8 yum_CentOS8] *****************************************************
skipping: [centos7] => (item=AppStream)
skipping: [centos7] => (item=BaseOS)
changed: [redhat8] => (item=AppStream)
changed: [centos8] => (item=AppStream)
changed: [redhat8] => (item=BaseOS)
changed: [centos8] => (item=BaseOS)
TASK [epel_RedHat8 epel_CentOS8] ***************************************************
skipping: [centos7]
changed: [redhat8]
changed: [centos8]
PLAY RECAP *************************************************************************
centos7 : ok=3 changed=2 unreachable=0 failed=0 skipped=2 rescued=0 ignored=0
centos8 : ok=3 changed=2 unreachable=0 failed=0 skipped=2 rescued=0 ignored=0
redhat8 : ok=3 changed=2 unreachable=0 failed=0 skipped=2 rescued=0 ignored=0
验证
//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 AppStream
BaseOS BaseOS
epel EPEL YUM repo
//在centos8主机上
[root@centos8 ~]# yum repolist
Repository AppStream is listed more than once in the configuration
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 AppStream
BaseOS BaseOS
base CentOS-8 - Base - mirrors.aliyun.com
epel EPEL YUM repo
epel-modular Extra Packages for Enterprise Linux Modular 8 - x86_64
extras CentOS-8 - Extras - mirrors.aliyun.com
//在centos7主机上
[root@centos7 ~]# yum repolist
Loaded plugins: fastestmirror
Repository base is listed more than once in the configuration
Determining fastest mirrors
* base: mirrors.bfsu.edu.cn
* extras: mirrors.163.com
* updates: mirrors.bfsu.edu.cn
base | 3.6 kB 00:00:00
epel | 4.7 kB 00:00:00
extras | 2.9 kB 00:00:00
updates | 2.9 kB 00:00:00
(1/7): base/7/x86_64/group_gz | 153 kB 00:00:00
(2/7): epel/group_gz | 95 kB 00:00:00
(3/7): extras/7/x86_64/primary_db | 222 kB 00:00:00
(4/7): base/7/x86_64/primary_db | 6.1 MB 00:00:01
(5/7): epel/updateinfo | 1.0 MB 00:00:01
(6/7): epel/primary_db | 6.9 MB 00:00:01
(7/7): updates/7/x86_64/primary_db | 4.7 MB 00:00:01
repo id repo name status
base/7/x86_64 CentOS-7 - Base 10,072
epel EPEL YUM repo 13,494
extras/7/x86_64 CentOS-7 - Extras 448
updates/7/x86_64 CentOS-7 - Updates 1,155
repolist: 25,169