Ansible - Add repository using the yum_repository module

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
    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
    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
    yum_repository:
      name: epel
      state: absent

  - name: clean dnf metadata
    command: dnf clean metadata
    args:
      warn: no
...

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee

Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.





Please enter 5e682 in the box below so that we can be sure you are a human.