JPNagarajan
JPNagarajan

Reputation: 928

How to disable all repositories using yum module in ansible?

I'm trying to disable all the yum repos and enable just 1 repo for installing a yum package.How to disable all repos using yum module?

Tried to use disablerepo='*' but not sure whether this is the correct method

- name: Update the uek kernel pkg on gateways
    yum:
      name: "{{ packages }}"
      disablerepo: "*"
      enablerepo: test_iso
    vars:
      packages:
      - kernel-uek
    become_user: root

Upvotes: 2

Views: 23448

Answers (2)

jwhb
jwhb

Reputation: 546

As Nick pointed out, the current yum module requires us to provide a list of specific repository names.

We can use yum -q repolist to query and parse the list of repositories.

---
- hosts: localhost
  tasks:

  - name: Get list of yum repos (to disable them temporarily)
    ansible.builtin.command: yum -q repolist
    register: _yum_repolist_output
    changed_when: False

  - name: Install Docker RPMs
    yum:
      name: "http://mirror.centos.org/centos/7/os/x86_64/Packages/vim-enhanced-7.4.629-7.el7.x86_64.rpm"
      enablerepo: []
      disablerepo: "{{ _yum_repolist_output.stdout_lines[1:] | map('split',' ') | map('first') | list }}"

This is not an ideal solution, but it seems to be the only solution to use the yum module in a setup with broken repository configuration, e.g. on air-gap systems.

Upvotes: 0

Nick
Nick

Reputation: 2034

The Ansible documentation suggests you must supply a comma-separated list of repo ids.

disablerepo: Repoid of repositories to disable for the install/update operation. These repos will not persist beyond the transaction. When specifying multiple repos, separate them with a ",". As of Ansible 2.7, this can alternatively be a list instead of "," separated string

The example from the documentation:

- name: Install package with multiple repos disabled
  yum:
    name: sos
    disablerepo: "epel,ol7_latest"

You might look into using the yum_repository module as an alternative as well:

# Example removing a repository and cleaning up metadata cache
- name: Remove repository (and clean up left-over metadata)
  yum_repository:
    name: epel
    state: absent
  notify: yum-clean-metadata

Upvotes: 2

Related Questions