Ansible Modules for installing packages

Reading Time: 3 minutes

Welcome readers, I’ll be covering various modules that are used for installing any package on different Linux distributions via Ansible. To know the basics of Ansible, you can go through this blog: Introduction to Ansible.

What are Modules?

Ansible includes small programs which are remotely executed on the server. They are included in playbooks which are written in YAML format or they can be used with ansible Ad-Hoc command by -m flag, which is used to specify the name of the module. Here’s an example:

ansible -m ping localhost

Now aligning back with our topic, I’ll start with the modules that are used for installing a package in remote hosts. I’ll go through the following sequence of modules for different distributions:

  • apt for Debian
  • yum for Fedora
  • pkgng for FreeBSD

After these modules, I’ll cover one module: package which is a generic OS package manager.

apt

The apt module is used to manage packages on remote nodes based on Linux Debian/Ubuntu distribution.

Commonly used attributes of apt module are:

  • name: This attribute is used to specify the name of the package you want to install. You can also specify the version for that package. An example command for installing a package test: name: test=1.0
  • state: This attribute indicates the desired package state. Different package states are:
    • present: ensures that the package is installed.
    • absent: ensures that the package is removed.
    • latest: ensures that the package is on its latest version.
    • build-dep: ensures the package build dependencies are installed.

yum

The yum module is used to manage packages on remote nodes based on Linux Fedora distribution.

Commonly used attributes of yum module are:

  • name: This attribute is used to specify the name of the package along with the version you want to install like test-1.0. If any previous version is specified then the attribute allow_downgrade needs to be turned on.
  • allow_downgrade: To be turned on if the specified package is allowed to downgrade.
  • download_only: Used when we only want to download the packages, but do not install them on the hosts. The default value of this attribute is no.
  • state: It indicates the desired package state. Different package states are:
    • present/installed: ensures that the package is installed.
    • absent/removed: ensures that the package is removed.
    • latest: ensures that the package is on its latest version.

pkgng

The yum module is used to manage the binary packages on remote nodes for Linux FreeBSD distribution.

Commonly used attributes of pkgng module are:

  • name: Used to specify the name or list of packages you want to install/remove.
  • state: It indicates the desired package state. Different package states are:
    • present: ensures package is already installed.
    • absent: ensures package is removed.
    • latest: ensures package is on its latest version.

Running package installer by identifying the distribution

As we have different modules for different Linux distribution, we need to find out the distribution of the host Linux system. We can get that information from the ansible_os_family value which is gathered by the ansible by automatically running the setup module. We can use this as follows:

# installing openjdk on diff OS.
# on debian
apt:
name: openjdk-8-jdk
state: present
when: ansible_os_family == 'Debian'
# on redhat
yum:
name: java-1.8.0-openjdk
state: present
when: ansible_os_family == 'RedHat'
# on freebsd
pkgng:
name: openjdk8
state: present
when: ansible_os_family == 'FreeBSD'
view raw playbook.yml hosted with ❤ by GitHub

The generic installation module: package

The package module of ansible provides a generic OS package manager which installs, upgrades and removes packages from various Linux distributions. For windows, you can see win_package module.

Commonly used attributes of package module are:

  • name: Used to specify the name or list of packages you want to install/remove.
  • state: This attribute indicates the desired package state. Different package states are:
    • present ensures that the package is installed.
    • absentensures that the package is removed.
    • latest ensures that the package is on its latest version.
  • use: Used to specify the package manager module to be used. By default, its value is auto which use existing facts to auto-detect the distribution.

References:

After going through the contents, you’ll now be familiar with various ansible modules which are used for installing packages on various Linux distributions. For any queries, feel free to contact me at yatharth.sharma@knoldus.in.

Thank you for sticking to the end. If you like this blog, please do show your appreciation by giving thumbs ups and share this blog and give me suggestions on how I can improve my future posts to suit your needs. Follow me to get updates on different technologies.