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

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
上一篇:Ansible 之 外部变量文件调用


下一篇:ansible系统复习学习笔记-从零到无