Managing Software & Subscription Using Ansible

In this lesson, you will learn how to use the Ansible yum module and other types of software and subscription-related module to manage software and subscription in Linux.

Automating Software Processes In Linux With Ansible

Software, or rather packages, can be installed and managed or automated with Ansible. In this lesson, we will look at how to automate software processes using the Ansible yum module, Ansible dnf module, Ansible package module, etc.

Using the Ansible yum Module with examples

The Ansible yum module is used to manage packages/software; such as installation & un-installation of software, software updates, etc.

The manual page of the Ansible yum module can be displayed by using the command, “ansible-doc yum”.

Some of the important options of the yum module are present, absent, latest, etc.

Let’s look at what these options are used for.

*The Ansible yum Module present Option

The present option is used to install packages or software. If the package is already installed, it checks and makes sure the package is present.

*The Ansible yum Module absent Option

The absent option is used to uninstall or remove packages or software

*The Ansible yum Module latest Option

The latest option will make sure the packages or software has the latest version if it is already installed. If it is not already installed, Ansible installs the latest version of the packages or software.

The yum module can also be used to install package group. One can use the command, (yum group list) to see the lists of package group on the system.

Let’s see how the yum module can be used with some examples using the playbook below.

[lisa@drsdev1 ansible]$ vi yum.yml
---
- name: install and manage software
  hosts: hqsdev1.tekneed.com
  tasks:
    - name: install the httpd package
      yum :
        name: httpd
        state: present
      when: ansible_distribution == 'RedHat'

    - name: install tmux package
      yum:
        name: tmux
        state: latest

    - name: uninstall nginx
      yum:
        name: nginx
        state: absent

The playbook above will use the yum module to install the httpd package only when the managed hosts are Red Hat enterprise Linux.

Secondly, the latest version of tmux package will also be installed.

Thirdly, nginx will be removed from the managed host.

*Run the playbook.

[lisa@drsdev1 ansible]$ ansible-playbook yum.yml --syntax-check

playbook: yum.yml
[lisa@drsdev1 ansible]$ ansible-playbook yum.yml

PLAY [install and manage software] ************************************************************

........................
ansible yum module

Using The Ansible dnf Module With Examples

The yum module can only be used if you are operating against managed hosts that are Red Hat Enterprise Linux.

The dnf module should be used if you are operating against managed hosts that are Suze, Ubuntu, and Fedora Linux.

Moving this exercise forward, let’s include some tasks with the dnf module in the playbook above.

[lisa@drsdev1 ansible]$ vi yum.yml
----------------
- name: install system tools package group
      dnf:
        name: '@System Tools'
        state: present
      when: "ansible_distribution == 'Fedora'"

At the end, the playbook will look like this:

- name: install and manage software
  hosts: hqsdev1.tekneed.com
  tasks:
    - name: install the httpd package
      yum :
        name: httpd
        state: present
      when: ansible_distribution == 'RedHat'

    - name: install tmux package
      yum:
        name: tmux
        state: latest

    - name: uninstall nginx
      yum:
        name: nginx
        state: absent

    - name: install system tools package group
      dnf:
        name: '@System Tools'
        state: present
      when: "ansible_distribution == 'Fedora'"

The last task will use the dnf module to install a package group, called “System Tools” only when the managed host is Fedora.

To install a package group with Ansible, the name of the package group must start with @ just as it’s written in the playbook above.

*Run the playbook

[lisa@drsdev1 ansible]$ ansible-playbook yum.yml

PLAY [install and manage software] ************************************************************

........................

Using Ansible Loop With The Ansible yum & dnf Module With Examples

We can also install multiple packages with the yum or dnf module all at once. An example of such a playbook would look like the one below.

---
- name: install various packages
  hosts: hqsdev1.tekneed.com
  tasks:
     - name: install packages
       yum:
          name:
            - httpd
            - nginx
            - mysql   
          state: present

However, the use of loop will be very efficient in the installation of multiple packages. To use loop, the playbook will be written as:

---
- name: install various packages
  hosts: hqsdev1.tekneed.com
  tasks:
     - name: install packages
       yum:
          name: "{{ item }}"
          state: present
       loop:
          - httpd
          - nginx
          - mysql   

With your playbook in this form, one can easily add or remove packages from the loop section.

Using The Ansible Package Module With Examples

There is also a very important module that is used to manage software installations called the package module.

The package module helps you determine the type of package manager that should be used on the managed hosts, and Ansible uses them.

For example, if the managed host is a RHEL host, the yum module will be automatically used, and if the host is Ubuntu, the dnf module will be automatically used.

An example of a playbook with the package module can be written as below:

---
- name: install various packages
  hosts: hqsdev1.tekneed.com
  tasks:
     - name: install packages
       package:
          name: httpd
          state: present

Managing Subscriptions In Linux Using Ansible

The redhat_subscription module, rhsm_repository, yum_repository, and rpm_key modules are some of the basic Ansible modules that are used to manage Red Hat subscriptions.

Normally, without the use of Ansible, one would manually register a system to access the Red Hat content delivery network (CDN), and also attach a subscription to the system, after which the needed repositories would be enabled.

The manual configuration is shown as below:

#subscription-manager register --username=<username> --password=<password>
subscription-manager attach --pool=<poolID>
subscription-manager repos --enable "rhel-8-for-x86_64-baseos-rpms"

As we go on, we will see how to automate all these processes using the Red Hat Ansible engine.

Using The Ansible redhat_subscription Module With Examples

The Ansible redhat_subscription module is used to manage, register, and subscribe systems to the Red Hat CDN.

Let’s see how this module can be used with examples using the playbook below.

[lisa@drsdev1 ansible]$ vi sub.yml
---
- name: manage subscriptions
  hosts: hqsdev1.tekneed.com
  tasks:
     - name: register the system
       redhat_subscription:
           username: your-username
           password: your-password
           pool_ids: poolID
           state: present

The playbook above will register the system to Red Hat CDN using the redhat_subscription module. The username, password, and pool_ids option is where the username, password, and pool ids will be imputed. The present option will make sure the system is registered and attached.

Moving forward, what comes after registering and subscribing a system is to enable the repositories. Let’s see how this can be done with the use of Ansible.

Using The Ansible rhsm_repository Module With Example

The rhsm_repository module is used to manage, enable, and disable Red Hat Repositories.

Let’s see how this module can be used with examples using the playbook below:

[lisa@drsdev1 ansible]$ vi sub.yml
---
- name: manage subscriptions
  hosts: hqsdev1.tekneed.com
  tasks:
     - name: enable RHEL repo
       rhsm_repository:
           name:
             - rhel-8-for-x86_64-baseos-rpms
             - rhel-8-for-x86_64-AppStream-rpms
           state: present

This playbook above will use the rhsm_repository module to enable the repositories, rhel-8-for-x86_64-baseos-rpms, and rhel-8-for-x86_64-AppStream-rpms.

Using The Ansible yum_repository Module With Examples

The Ansible yum_repository module is used to manage, add, or remove yum repositories on hosts. This module will create the repository file which can be used by the system.

This module is also used to install external repositories like the likes of EPEL, kubernetes, docker, etc.

Let’s see how the ansible yum_repository module can be used with examples by using the playbook below:

[lisa@drsdev1 ansible]$ vi repo.yml
---
- name: configure yum repository
  hosts: hqsdev1.tekneed.com
  tasks:
     - name: configure yum local repo
       yum_repository:
           file: local-repo
           name: rhel8-repo
           description: local yum repo
           enabled: yes
           gpgcheck: yes
           baseurl: http://tekneed.local.com/yum/local-repo
           state: present

*Do a playbook syntax check

[lisa@drsdev1 ansible]$ ansible-playbook repo.yml --syntax-check

playbook: repo.yml

*Run the playbook

[lisa@drsdev1 ansible]$ ansible-playbook repo.yml

PLAY [configure yum repository] *********************************************
...............

*Verify the configuration

[lisa@drsdev1 ansible]$ ansible hqsdev1.tekneed.com -a 'ls -l /etc/yum.repos.d'

hqsdev1.tekneed.com | CHANGED | rc=0 >>
total 172
-rw-r--r--. 1 root root    265 Feb  9 14:07 kubernetes.repo
-rw-r--r--. 1 root root    110 Mar 18 13:47 local-repo.repo
-rw-r--r--. 1 root root 167330 Mar 10 15:47 redhat.repo
[lisa@drsdev1 ansible]$ ansible hqsdev1.tekneed.com -a 'cat /etc/yum.repos.d/local-repo.repo'

hqsdev1.tekneed.com | CHANGED | rc=0 >>
[rhel8-repo]
baseurl = http://ekneed.local.com/yum/local-repo
enabled = 1
gpgcheck = 1
name = local yum repo

Including and Importing RPM GPG Key Using Ansible

The gpgkey key needs to be installed if it’s enabled in the repository file.

The gpgkey can simply be added as an option so that it can be included in the file or one can use a module to import the gpgkey.

From the example playbook above, the gpgkey can be included as an option as seen below:

...........
gpgkey = http://hqdev1.tekneed.com/rpm/RPM-GPG-KEY-redhat-release

The whole playbook will look like this:

---
- name: configure yum repository
  hosts: hqsdev1.tekneed.com
  tasks:
     - name: configure yum local repo
       yum_repository:
           file: local-repo
           name: rhel8-repo
           description: local yum repo
           enabled: yes
           gpgcheck: yes
           baseurl: http://ekneed.local.com/yum/local-repo
           state: present
           gpgkey: http://hqdev1.tekneed.com/rpm/RPM-GPG-KEY-redhat-release

Let’s run the playbook so we can verify the result

*Run the playbook

[lisa@drsdev1 ansible]$ ansible-playbook repo.yml

PLAY [configure yum repository] ******************************
................

*Verify if the key has been added to the file

[lisa@drsdev1 ansible]$ ansible hqsdev1.tekneed.com -a 'cat /etc/yum.repos.d/local-repo.repo'

hqsdev1.tekneed.com | CHANGED | rc=0 >>
[rhel8-repo]
baseurl = http://ekneed.local.com/yum/local-repo
enabled = 1
gpgcheck = 1
gpgkey = http://hqdev1.tekneed.com/rpm/RPM-GPG-KEY-redhat-release
name = local yum repo

Using The Ansible rpm_key Module With Example

The GPG key can also be imported by using the rpm_key module.

Let’s see how this can be used by using the playbook below.

---
- name: configure yum repository
  hosts: hqsdev1.tekneed.com
  tasks:
     - name: configure yum local repo
       yum_repository:
           file: local-repo
           name: rhel8-repo
           description: local yum repo
           enabled: yes
           gpgcheck: yes
           baseurl: http://ekneed.local.com/yum/local-repo
           state: present

    - name: implement GPG key
      rpm_key:
          key: http://hqdev1.tekneed.com/rpm/RPM-GPG-KEY-r
      state: present

Finally, the playbook below is a very good example of how to automate red hat subscription management

---
- name: manage subscriptions
  hosts: hqsdev1.tekneed.com
  vars:
     access_username: your-username
     acess_password: your-password
     repos:
        - rhel-8-for-x86_64-baseos-rpms
        - rhel-8-for-x86_64-AppStream-rpms
  tasks:
     - name: register the system
       redhat_subscription:
           username: "{{ access_username }}"
           password: "{{ access_password }}"
           auto_attach: true
           state: present /enabled

     - name: disable all repositories
       rhsm_repository:
           name: '*'
           state: disabled

     - name: enable the needed repositores
       rhsm_repository:
           name: "{{ repos }}"
           state: enabled

RHCE Exam Practice Question On Using Ansible yum Module


Ansible Basics – Video

Your feedback is welcomed. If you love others, you will share with others

Be the first to comment

Leave a Reply

Your email address will not be published.


*