Linux

通過 ansible 禁用與值匹配的 repo

  • March 28, 2021

這個寫錯了。我們要禁用除 rhel & epel 之外的所有 repos。

- name: yum-clean-metadata
 command: yum clean metadata
 args:
   warn: no

- name: Repos disabled if not rhel.repo
 debug: msg={{ lookup('fileglob', '/etc/yum.repo.d/rhel.repo') }}
 yum:
   name: 
   disablerepo: "ora,ol7_latest"

- name: Ensure the yum package index is up to Date
 yum:
   update_cache: yes
   name: '*'
   state: latest

如果您不反對自己操作 repo 文件,則該播放應重命名列表/etc/yum.repos.d/*.repo中指定的文件以外的所有文件allowed_repos

如果您確切知道要禁用哪些文件,則另一個答案更直接。

 - name: Disable extra repositories
   vars:
     allowed_repos:
       - /etc/yum.repos.d/epel.repo
       - /etc/yum.repos.d/rhel.repo
     found_repo_files: []
   block:
     - name: Find all repositories
       find:
         paths: "/etc/yum.repos.d"
         file_type: file
         recurse: no
         patterns: "*.repo"
       register: repos_d

     - name: Compile repository list
       set_fact:
         found_repo_files: "{{ found_repo_files }} + [ '{{ item.path }}' ]"
       loop_control:
         label: "{{ item.path }}"
       with_items:
         - "{{ repos_d.files }}"

     - name: Rename any extra repositories
       when: not ansible_check_mode
       command:
         cmd: "mv {{ item }} {{ item }}.orig"
         removes: "{{ item }}"
       with_items:
         - "{{ found_repo_files | difference(allowed_repos) }}"

引用自:https://unix.stackexchange.com/questions/637580