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

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

环境说明:

主机IP 系统平台
192.168.100.1 主控
192.168.100.2 redhat8 被控
192.168.100.3 redhat7 被控
192.168.100.4 centos8 被控
192.168.100.5 centos7 被控

项目结构预览:

[root@ansible yum]# tree
.
├── ansible.cfg
├── inventory
└── yum.yml

0 directories, 3 files

本次环境YUM源(centos和epel)为:"阿里云官方镜像站"

准备工作:

//映射主机名
[root@ansible ~]# vim /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.100.1 ansible
192.168.100.2 redhat8
192.168.100.3 redhat7
192.168.100.4 centos8
192.168.100.5 centos7

//安装ansible
[root@ansible ~]# yum -y install ansible

//创建项目文件夹
[root@ansible ~]# mkdir ~/yum

//修改清单文件位置
[root@ansible ~]# vim /etc/ansible/ansible.cfg
inventory      = ./inventory

[root@ansible ~]# cp /etc/ansible/ansible.cfg ~/yum/

//编写清单
[root@ansible ~]# vim ~/yum/inventory
[redhat]
redhat8
redhat7

[centos]
centos8
centos7

//使用ssh-keygen生成私钥和公钥
[root@ansible ~]# ssh-keygen -t rsa

//设置免密登录

[root@ansible ~]# ssh-copy-id root@redhat8
[root@ansible ~]# ssh-copy-id root@redhat7
[root@ansible ~]# ssh-copy-id root@centos8
[root@ansible ~]# ssh-copy-id root@centos7

编写配置yum的playbook

[root@ansible ~]# vim ~/yum/yum.yml
---
- hosts: all
  vars:
    baseurl_8: https://mirrors.aliyun.com/epel/8/Modular/x86_64/
    baseurl_7: https://mirrors.aliyun.com/epel/7/x86_64/

  tasks:
    - name: yum config for 8
      yum_repository:
        name: "{{ item }}"
        baseurl: https://mirrors.aliyun.com/centos/8/{{ item }}/x86_64/os/
        enabled: yes
        gpgcheck: no
        mode: 0644
        file: "{{ item }}"
        description: "{{ item }}"                               
        state: present
      loop:
        - BaseOS
        - AppStream
      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: yum config for 7
      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"] == "RedHat" and
          ansible_facts["distribution_major_version"] == "7" )
        or
        ( ansible_facts["distribution"] == "CentOS" and
          ansible_facts["distribution_major_version"] == "7" )
        
    - name: yum config epel for 8
      yum_repository:
        name: epel
        baseurl: "{{ baseurl_8 }}"
        enabled: yes
        gpgcheck: no
        mode: 0644
        file: epel
        description: epel
        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: yum config epel for 7
      yum_repository:
        name: epel
        baseurl: "{{ baseurl_7 }}"
        enabled: yes
        gpgcheck: no
        mode: 0644
        file: epel
        description: epel
        state: present
      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 ~]# cd ~/yum/
[root@ansible yum]# ansible-playbook yum.yml

验证

//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

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

//centos8
[root@centos8 ~]# 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@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
updates/x86_64          CentOS-7 - Updates - mirrors.aliyun.com                  1,155
repolist: 25,167
posted @ 2021-01-13 23:50  Serein)  阅读(370)  评论(0编辑  收藏  举报