top of page
Writer's pictureSiddhesh Kadam

Ansible Yum Module

Ansible YUM module is useful to perform different kind of package management activities like Installing / Uninstalling / Upgrading / Removing of package.


Lets try to cover this different scenarios one by one to get more hands on it.


Scenario 1 : Installation Of Package In this scenario we'll see how we can use Ansible yum module to install a specific package.

[root@siddhesh ~]# cat yum.yml
- hosts: dbserver
  tasks:
  - name: Install package wget
    yum:
      name: wget
      state: present
[root@siddhesh ~]#

Here :

- hosts: dbserver ==> Host Inventory Group On which this action needs to be performed. - name: Install package wget ==> Task Description yum: ==> Load Yum Module name: wget ==> Name of package to install on remote server.

state: present ==> Make playbook to install mention package on remote server if not present.

So lets execute this playbook and see what changes it makes on remote server.

[root@siddhesh ~]# ansible-playbook yum.yml

PLAY [dbserver] **********************************************************************************************************************************************************

TASK [Gathering Facts] **********************************************************************************************************************************************
ok: [node1.tecgeek.info]

TASK [Install package wget] *****************************************************************************************************************************************
changed: [node1.tecgeek.info]

PLAY RECAP **********************************************************************************************************************************************************
node1.tecgeek.info         : ok=2    changed=1    unreachable=0    failed=0
[root@siddhesh ~]#

You can see one changed activity in logs of Package installation on remote server without any failed log. This can be verified on remote server by running :

[root@node1 ~]# rpm -qa |grep wget
wget-1.14-18.el7_6.1.x86_64
[root@node1 ~]#

Scenario 2 : Up-gradation Of Package. In this scenario we'll see how to upgrade already installed package.

This can be achieve with state: latest flag to make sure latest git package on remote server.

[root@siddhesh ~]# cat yum.yml
- hosts: dbserver
  tasks:
  - name: upgrade git package
    yum:
      name: git
      state: latest
[root@siddhesh ~]#

Here :

state: latest ==> This will upgrade package to version which is available in respective repo.


So lets run this playbook and see what changes it makes on remote server.

[root@siddhesh ~]# ansible-playbook yum.yml

PLAY [dbserver] *****************************************************************************************************************************************************

TASK [Gathering Facts] **********************************************************************************************************************************************
ok: [node1.tecgeek.info]

TASK [upgrade git package] ******************************************************************************************************************************************
changed: [node1.tecgeek.info]

PLAY RECAP **********************************************************************************************************************************************************
node1.tecgeek.info         : ok=2    changed=1    unreachable=0    failed=0

[root@siddhesh ~]#

Scenario 3 : Installing multiple packages In this scenario we'll see how to install multiple packages at the same time using Ansible yum module. So to achieve this we are going to use with_items statement to loop through a list of defined package in playbook.

[root@siddhesh ~]# cat yum.yml
- hosts: dbserver
  tasks:
  - name: install multiple packages
    yum:
      name: "{{ item }}"
      state: present
    with_items:
    - git
    - wget
    - tcpdump
[root@siddhesh ~]#

Here : name: "{{ item }}" ==> Item loop execution with_items: ==> statement to define list for loop execution

So lets execute this playbook to see its changes.

[root@siddhesh ~]# ansible-playbook yum.yml

PLAY [dbserver] *****************************************************************************************************************************************************

TASK [Gathering Facts] **********************************************************************************************************************************************
ok: [node1.tecgeek.info]

TASK [install multiple packages] ************************************************************************************************************************************
changed: [node1.tecgeek.info] => (item=[u'git', u'wget', u'tcpdump'])

PLAY RECAP **********************************************************************************************************************************************************
node1.tecgeek.info         : ok=2    changed=1    unreachable=0    failed=0

[root@siddhesh ~]# 

As you can from its output that execution of package installation was done in single task. Scenario 4 : Removing Package Lets also see how to remove package from this module.

[root@siddhesh ~]# cat yum.yml
- hosts: dbserver
  tasks:
  - name: upgrade git package
    yum:
      name: git
      state: absent
[root@siddhesh ~]#

Here : state: absent ==> make sure that package should be remove from remote server. So state absent is to erase/remove package.

Lets run this playbook to remove package git from remote server.

[root@siddhesh ~]# ansible-playbook yum.yml

PLAY [dbserver] *****************************************************************************************************************************************************

TASK [Gathering Facts] **********************************************************************************************************************************************
ok: [node1.tecgeek.info]

TASK [upgrade git package] ******************************************************************************************************************************************
changed: [node1.tecgeek.info]

PLAY RECAP **********************************************************************************************************************************************************
node1.tecgeek.info         : ok=2    changed=1    unreachable=0    failed=0

[root@siddhesh ~]#

To find more such action please refer ansible-doc guide as follow.

[root@siddhesh ~]# ansible-doc -s yum




Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating
bottom of page