ansible 配置yum源


准備工作

[root@wang yum]# ssh-copy-id root@192.168.170.137 #設置免密登入

[root@wang yum]# cat /etc/hosts  #配置清單文件
192.168.170.134 apache
192.168.170.137 cos7

將ansible配置文件f復制到yum目錄下

 [root@wang yum]# cp /etc/lamp/inventory

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

嘗試能否ping通

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

查看清單

[root@wang yum]# ansible all --list-hosts
hosts (2):
apache
cos7

  劇本內容

[root@wang yum]# cat 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 {{ 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 centos7
      yum_repository:
        name: bash
        baseurl:  https://mirrors.aliyun.com/centos/7/os/x86_64/
        enabled: yes
        gpgcheck: no
        mode: 0644
        file: bash
        description: base
        state: present
        when:
          - ansible_facts["distribution"] == "CentOS"
          - ansible_facts["distribution_major_version"] == "7"
    - name: yum epel
      yum_repository:
        name: epel
        baseurl: "{{ baseurl }}"
        enabled: yes
        gpgcheck: no
        mode: 0644
        file: bash
        description: epel
        state: present

驗證

[root@apache ~]# ll /etc/yum.repos.d/
總用量 12
-rw-r--r--. 1 root root 122 1月 14 09:50 AppStream.repo
-rw-r--r--. 1 root root 113 1月 14 09:50 BaseOS.repo

[root@cos7 ~]# ll /etc/yum.repos.d/
總用量 4
-rw-r--r--. 1 root root 2523 1月 14 20:42 CentOS-Base.repo


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM