Thursday, April 28, 2022

[SOLVED] Executing yum clean expire-cache and remove commands using ansible script

Issue

Could anyone please support with equivalent tasks for clean and remove ?

yum clean expire-cache
yum -y remove packageX-S
yum -y install packageX-S

I got install already...

- name: deploy
  yum:
    name: llc-html-clients-S
    state: latest

Solution

TL;DR;

Here are your equivalent tasks:

- name: clean
  command: yum clean expire-cache

- name: remove
  yum:
    name: pkg-to-remove
    state: absent

- name: install
  yum:
    name: pkg-to-install
    state: present

Installing and removing is done with the same module yum.

When installing would test the installed or present state, removing is about testing removed or absent state.

Install:

- name: install
  yum:
    name: pkg-to-install
    state: present

Take care: yum install and state: latest are not the same, when the yum command will install if the package is absent and do nothing if it is present already, state: latest will do an install if the package is absent but also a yum update pkg-to-install if the package is not at its latest version.
The real equivalent is state: present.

present and installed will simply ensure that a desired package is installed.
latest will update the specified package if it's not of the latest available version.

Source: https://docs.ansible.com/ansible/latest/modules/yum_module.html#parameter-state

Remove:

- name: remove
  yum:
    name: pkg-to-remove
    state: absent

Then for the clean, sadly, there was a choice to not implement it, as this is not something that can be done in an idempotent way.

See this note on yum module page

Source: https://docs.ansible.com/ansible/latest/modules/yum_module.html#notes

So as pointed in the note, you could actually go by a simple command.

- name: clean
  command: yum clean expire-cache

So those are equivalent:

  • in bash
yum clean expire-cache
yum -y remove pkg-to-remove
yum -y install pkg-to-install
  • in playbook
- name: clean
  command: yum clean expire-cache

- name: remove
  yum:
    name: pkg-to-remove
    state: absent

- name: install
  yum:
    name: pkg-to-install
    state: present


Answered By - β.εηοιτ.βε
Answer Checked By - Willingham (WPSolving Volunteer)