Ansible: Adding a YUM or DNF repository

Redhat Ansible

A yum or dnf repository can be added to managed nodes using Ansible. This is done using the ansible builtin yum_repository module.

Basics of creating yum repositories can be found at this link.

Below playbook adds a yum repository.

[root@centos9vm ~]# cat create_yum_repo.yml

====== ==
– – –
– name: Playbook to create a yum repository in manage nodes
   hosts: 192.168.48.132
   tasks:
      – name: Task to create yum repository
         ansible.builtin.yum_repository:
            file: fedora_example
            name: ExampleFedora
            baseurl: https://dl.fedoraproject.org/pub/epel/9/Everything/x86_64/
            gpgcheck: no
            enabled: yes
            description: fedora_example

==== ==

Let us check the managed node before running the playbook

[root@centos9test1 ~]# dnf repolist all | grep fedo

The above command may not return any result

Now let us run the playbook

[root@centos9vm ~]# ansible-navigator run -m stdout create_yum_repo.yml

=== ==

PLAY [Playbook to create a yum repository in manage nodes] *********************

TASK [Gathering Facts] *********************************************************
ok: [192.168.48.132]

TASK [Task to create yum repository] *******************************************
changed: [192.168.48.132]

PLAY RECAP *********************************************************************
192.168.48.132 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

==== ===

Let us recheck now

[root@centos9test1 ~]# dnf repolist all | grep fedo
ExampleFedora fedora_example enabled