Ansible - Add repository using the yum_repository module
by
Jeremy Canfield |
Updated: April 15 2024
| Ansible articles
If you are not familiar with modules, check out Ansible - Getting Started with Modules.
The yum_repository module can be used to add or remove a .repo file from the /etc/yum.repos.d directory on a managed node.
AVOID TROUBLE
The /etc/yum.repos.d directory should be owned by root, thus you'll want to use the remote_user: root or become parameters.
In this example, the /etc/yum.repos.d/epel.repo will be added on the managed node.
---
- hosts: all
tasks:
- name: add the /etc/yum.repos.d/epel.repo
ansible.builtin.yum_repository:
name: epel
description: EPEL repo
baseurl: https://download.fedoraproject.org/pub/epel/$releasever/$basearch/
...
In this example, the /etc/yum.repos.d/epel.repo file on the managed node should contain something like this.
[epel]
baseurl = https://download.fedoraproject.org/pub/epel/$releasever/$basearch/
name = EPEL repo
Here is a more complete example.
---
- hosts: all
tasks:
- name: add the /etc/yum.repos.d/epel.repo
ansible.builtin.yum_repository:
name: epel
description: EPEL repo
baseurl: https://download.fedoraproject.org/pub/epel/$releasever/$basearch/
enabled: true
gpgcheck: true
gpgkey: file:///etc/pki/rpm-gpg/RPM-GPG-KEY-fedora-$releasever-$basearch
...
And here is how you would go about removing a .repo file.
---
- hosts: all
tasks:
- name: remove /etc/yum.repos.d/epel.repo
ansible.builtin.yum_repository:
name: epel
state: absent
- name: clean dnf metadata
ansible.builtin.command: dnf clean metadata
args:
warn: no
...
Did you find this article helpful?
If so, consider buying me a coffee over at