zoukankan      html  css  js  c++  java
  • ansible-playbook配置不同系统yum源

    准备工作

    环境

    主机 ip 系统
    yc 192.168323.132 RHEL8
    yc1 192.168.23.133 RHEL8
    yc2 192.168.23.134 CentOS7

    yum任务目录结构

    [root@yc yum]# tree .
    .
    ├── ansible.cfg
    ├── inventory
    ├── scripts
    │   ├── centos7yum.sh
    │   └── rhel8yum.sh
    └── yum.yml
    

    配置

    关闭防火墙和selinux

    [root@yc ~]# systemctl stop firewalld
    [root@yc ~]# setenforce 0
    

    映射

    [root@yc yum]# vim /etc/hosts
    
    192.168.23.133 yc1
    192.168.23.134 yc2
    

    复制ansible配置文件到yum目录下

    [root@yc yum]# cp /etc/ansible/ansible.cfg .
    [root@yc yum]# vim ansible.cfg 
    
    
    [defaults]
    ...
    # some basic default values...
    
    inventory      = ./inventory  //设定清单在当前目录下
    

    创建清单文件

    [root@yc yum]# cat inventory 
    
    [yum]
    yc1
    yc2
    

    公钥

    root@yc yum]# ssh-keygen -t rsa //直接回车
    Generating public/private rsa key pair.
    Enter file in which to save the key (/root/.ssh/id_rsa): 	
    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:8uEqvn1A0Ef392b9TJsW818mjZHhD5zHLHNhs1A0dOs root@ansible
    The key's randomart image is:
    +---[RSA 3072]----+
    |     . .. .   o=.|
    |    . . .. .  . +|
    |     . .    ..o= |
    |      .      ++B=|
    |     .. S     XE@|
    |      .+ .     /B|
    |       .o     o+O|
    |    .. ..     .oo|
    |   .ooo.        .|
    +----[SHA256]-----+
    
    //复制公钥到受控机
    
    [root@yc yum]# ssh-copy-id root@yc1
    [root@yc yum]# ssh-copy-id root@yc2
    
    查看清单
    [root@yc yum]# ansible all --list-hosts
      hosts (2):
        yc1
        yc2
    

    测试连接

    [root@yc yum]# ansible yum -m ping
    yc1 | SUCCESS => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/bin/python"
        },
        "changed": false,
        "ping": "pong"
    }
    yc2 | SUCCESS => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/libexec/platform-python"
        },
        "changed": false,
        "ping": "pong"
    }
    

    编写yum配置剧本

    [root@yc yum]# cat yum.yml 
    ---
    - name: yum config
      hosts: yum
      gather_facts: yes 
      tasks:
    #判断distribution是否为CentOS,主版本是否为7。二者均满足则配置centos7源,否则跳过
        - name: get centos7 yum
          script: ./scripts/centos7yum.sh
          when: 
            - ansible_facts["distribution"] == "CentOS"
            - ansible_facts["distribution_major_version"] == "7" 
            
    #判断distribution是否为RedHat,主版本是否为8。二者均满足则配置rhel8源,否则跳过
        - name: get rhel8 yum
          script: ./scripts/rhel8yum.sh
          when: 
            - ansible_facts["distribution"] == "RedHat"
            - ansible_facts["distribution_major_version"] == "8"
    

    编写yum配置脚本

    //CentOS7 yum配置
    
    [root@yc yum]# cat scripts/centos7yum.sh 
    #! /bin/bash
    curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
    wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
    yum clean all && yum makecache
    
    //RHEL8 yum配置
    [root@yc yum]# cat scripts/rhel8yum.sh 
    #! /bin/bash
    #yumconfig
    curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-8.repo
    #epel config
    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*
    #makecache
    yum clean all && yum makecache
    

    验证

    运行剧本

    [root@yc yum]# ansible-playbook yum.yml 
    
    PLAY [yum config] ***************************************************************************************************
    
    TASK [Gathering Facts] **********************************************************************************************
    ok: [yc1]
    ok: [yc2]
    
    TASK [get centos7 yum] **********************************************************************************************
    skipping: [yc1]
    changed: [yc2]
    
    TASK [get rhel8 yum] ************************************************************************************************
    skipping: [yc1]
    changed: [yc2]
    
    PLAY RECAP **********************************************************************************************************
    yc1                      : ok=2    changed=1    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0   
    yc2                      : ok=2    changed=1    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0 
    

    查看yc1

    [root@yc1 ~]# yum repolist all
    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                                                                      status
    AppStream                                      CentOS-8 - AppStream - mirrors.aliyun.com                                      enabled
    PowerTools                                     CentOS-8 - PowerTools - mirrors.aliyun.com                                     disabled
    base                                           CentOS-8 - Base - mirrors.aliyun.com                                           enabled
    centosplus                                     CentOS-8 - Plus - mirrors.aliyun.com                                           disabled
    extras                                         CentOS-8 - Extras - mirrors.aliyun.com                                         enabled
    

    查看yc2

    [root@yv2 ~]# yum repolist all
    Loaded plugins: fastestmirror
    Loading mirror speeds from cached hostfile
     * base: mirrors.aliyun.com
     * extras: mirrors.aliyun.com
     * updates: mirrors.aliyun.com
    repo id                                   repo name                                                                    status
    base/7/x86_64                             CentOS-7 - Base - mirrors.aliyun.com                                         enabled: 10,072
    centosplus/7/x86_64                       CentOS-7 - Plus - mirrors.aliyun.com                                         disabled
    contrib/7/x86_64                          CentOS-7 - Contrib - mirrors.aliyun.com                                      disabled
    epel/x86_64                               Extra Packages for Enterprise Linux 7 - x86_64                               enabled: 13,492
    epel-debuginfo/x86_64                     Extra Packages for Enterprise Linux 7 - x86_64 - Debug                       disabled
    epel-source                               Extra Packages for Enterprise Linux 7 - x86_64 - Source                      disabled
    extras/7/x86_64                           CentOS-7 - Extras - mirrors.aliyun.com                                       enabled:    448
    updates/7/x86_64                          CentOS-7 - Updates - mirrors.aliyun.com                                      enabled:  1,155
    repolist: 25,167
    
  • 相关阅读:
    Linux: 安装和启用firefox浏览器的java
    Latex: beamer
    时频分析:窗口傅立叶变换
    Python: 面向对象
    Linux: 安装NVIDIA显卡驱动
    Matlab: 路径的操作
    python--文件读写
    python--函数
    python--数据类型
    网络基础——网络协议
  • 原文地址:https://www.cnblogs.com/Ycqifei/p/14275088.html
Copyright © 2011-2022 走看看