playbook配置不同系统版本的yum源配置

主机 ip 系统
localhost 192.168.122.134 rhel8
cb2 192.168.122.137 centos7
cb3 192.168.122.138 rhel8

结构树

[root@localhost ~]# tree .
.
├── anaconda-ks.cfg
└── yum
    ├── ansible.cfg
    ├── inventory
    ├── scripts
    │   ├── centos6.sh
    │   ├── centos7.sh
    │   └── centos8.sh
    └── yum.yml

准备环境

[root@localhost ~]# mkdir yum
[root@localhost ~]# mkdir yum/scripts
[root@localhost yum]# cp /etc/ansible/ansible.cfg .
[root@localhost yum]# vim /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.122.137 cb2
192.168.122.138 cb3
[root@localhost yum]# vim ansible.cfg 
inventory      = ./inventory
[root@localhost yum]# vim inventory
[centos]
cb2

[redhat]
cb3
[root@localhost ~]# ssh-keygen -t rsa
[root@localhost ~]# ssh-copy-id root@192.168.122.137
[root@localhost ~]# ssh-copy-id root@192.168.122.138

配置脚本

//编写centos8脚本
[root@localhost ~]# vim yum/scripts/centos8.sh
#!/bin/bash
mount /dev/cdrom /mnt
# CentOS 8
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 8
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 all && yum makecache

//编写centos7脚本
[root@localhost ~]# vim yum/scripts/centos7.sh
#!/bin/bash
mount /dev/cdrom /mnt
# CentOS 7
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 7
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 all && yum makecache

//编写centos6脚本
[root@localhost ~]# vim yum/scripts/centos6.sh
#!/bin/bash
mount /dev/cdrom /mnt
# CentOS 6
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-6.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|6|' /etc/yum.repos.d/CentOS-Base.repo
# epel 6
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
sed -i 's|$releasever|6|' /etc/yum.repos.d/epel*
yum clean all && yum makecache

编写yum的playbook

[root@localhost ~]# vim yum/yum.yml
---
- name: yum
  hosts: all
  tasks:
    - name: centos8
      script: ./scripts/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/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" )
          
    - name: centos6
      script: ./scripts/centos6.sh
      when: >
        ( ansible_facts["distribution"] == "RedHat" and
          ansible_facts["distribution_major_version"] == "6" )
        or
        ( ansible_facts["distribution"] == "CentOS" and
          ansible_facts["distribution_major_version"] == "6" )

运行

[root@localhost ~]# cd yum/
[root@localhost yum]# ansible-playbook yum.yml

验证

//centos8
[root@cb2 ~]# 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

//centos7
[root@cb3 ~]# 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
updates/x86_64          CentOS-7 - Updates - mirrors.aliyun.com                  1,155
repolist: 25,167

 

上一篇:playbook部署lamp架构


下一篇:Ansible 之 ansible-playbook基础入门使用