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

主机 IP  系统
ansible/主控机 192.168.220.10 redhat8
T1 192.168.220.40 redhat8 
T2 192.168.220.140 centos7
//测试一下通信
[root@ansible yum]# cat inventory [group_yum] T1 t2 [root@ansible yum]# ansible all -m ping t2 | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": false, "ping": "pong" } T1 | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/libexec/platform-python" }, "changed": false,
//做一下免密
[root@ansible yum]# ssh-keygen -t rsa
[root@ansible yum]# ssh-copy-id root@T1
[root@ansible yum]# ssh-copy-id root@t2
//编写剧本
[root@ansible yum]# vim yum.yml 

---
- hosts: all
  vars:
    baseurl: https://mirrors.aliyun.com/epel/8/Modular/x86_64/
    major_version: ansible_facts["distribution_major_version"]
  tasks:
    - name: yum config for {{ major_version }}
      loop:
        - BaseOS
        - AppStream
      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
      when:
        - ansible_facts["distribution"] == "RedHat"
        - ansible_facts["distribution_major_version"] == "8"

    - name: yum config for 7
      loop:
        - BaseOS
        - AppStream
      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"
        - ansible_facts["distribution_major_version"] == "7"

    - name: yum config epel
      yum_repository:
        name: epel
        baseurl: "{{ baseurl }}"
        enabled: yes
        gpgcheck: no
        mode: 0644
        file: epel
        description: epel
        state: present
//执行一下剧本
[root@ansible yum]# ansible-playbook yum.yml 
//检查一下结果
[root@T1 yum.repos.d]# ls  //redhat8
AppStream.repo  BaseOS.repo  epel.repo
[root@t2 yum.repos.d]# ls  //centos7
CentOS-Base.repo  CentOS-Debuginfo.repo  CentOS-Media.repo    CentOS-Vault.repo
CentOS-CR.repo    CentOS-fasttrack.repo  CentOS-Sources.repo  epel.repo
原文地址:https://www.cnblogs.com/lichouluoyu/p/14283097.html