ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

playbook实现不同系统yum仓库配置

2021-01-19 04:32:48  阅读:225  来源: 互联网

标签:x86 仓库 ansible yum playbook cst facts 64 root


playbook实现不同系统yum仓库配置

环境介绍

ip地址 系统和版本号 名称
192.168.102.134 ansible主控机 cst
192.168.102.140 CentOS7 c7
192.168.102.141 CentOS8 c8
192.168.102.142 RedHat7 r7
192.168.102.143 RedHat8 r8

配置主控机

//创建项目
[root@cst ~]# yum -y install ansible
[root@cst ~]# mkdir myrepo
[root@cst ~]# cd myrepo
[root@cst myrepo]# cp /etc/ansible/ansible.cfg .
[root@cst ~]# vim /etc/ansible/ansible.cfg 

inventory      = /etc/ansible/inventory

//为其主机写映射,方便操作
[root@cst ~]# vim /etc/hosts 

192.168.102.140 c7
192.168.102.141 c8
192.168.102.142 r7
192.168.102.143 r8

//配置清单文件
[root@cst myrepo]# vim inventory

c7
c8
r7
r8

//为其他主机做ssh连接
[root@cst myrepo]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
/root/.ssh/id_rsa already exists.
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:FgrLx7dWSGIrz2KAlNH4VgVIHXjGncx3B1NXgqpbkEw root@cst
The key's randomart image is:
+---[RSA 3072]----+
| .+.=oB..  ooo..o|
| .o+ * =E. .o... |
| o. = oo+....    |
|.. + = =+o.      |
|. o + = So.      |
|   . = o.o.      |
|    o o oo       |
|   . . ..        |
|                 |
+----[SHA256]-----+
[root@cst myrepo]# ssh-copy-id root@c7
[root@cst myrepo]# ssh-copy-id root@c8
[root@cst myrepo]# ssh-copy-id root@r7
[root@cst myrepo]# ssh-copy-id root@r8
[root@cst myrepo]# ansible all -m ping
c7 | SUCCESS => {
  "ansible_facts": {
      "discovered_interpreter_python": "/usr/libexec/platform-python"
  },
  "changed": false,
  "ping": "pong"
}
c8 | SUCCESS => {
  "ansible_facts": {
      "discovered_interpreter_python": "/usr/libexec/platform-python"
  },
  "changed": false,
  "ping": "pong"
}
r7 | SUCCESS => {
  "ansible_facts": {
      "discovered_interpreter_python": "/usr/libexec/platform-python"
  },
  "changed": false,
  "ping": "pong"
}
r8 | SUCCESS => {
  "ansible_facts": {
      "discovered_interpreter_python": "/usr/libexec/platform-python"
  },
  "changed": false,
  "ping": "pong"
}

编写playbook

[root@cst myrepo]# vim repo.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@cst myrepo]# ansible-playbook repo.yml
[root@c7 ~]# yum repolist 
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

[root@c8 ~]# yum repolist 
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

[root@r7 ~]# yum repolist
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

[root@r8 ~]# yum repolist 
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

标签:x86,仓库,ansible,yum,playbook,cst,facts,64,root
来源: https://www.cnblogs.com/sarenn/p/14296070.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有