In this blog I will remove and install vlc player on my ubuntu box from the ansible control node and skip centos OS boxes. I will create two yml files to run these playbooks. Remember spacing is very important in your yml files.

First I will update my inventory file to include the ips of my remove machines

cd /etc/ansible
cat inventory.ini
#centos
192.168.1.151
#Ubuntu
192.168.1.130

Next on my manage node I will check vlc is already installed

cat /etc/os-release
sudo apt list --installed | grep vlc

Next I will create my two playbooks one for removing and one for installing, the key difference is state: absent which will remove the software. We will use –become –ask-become-pass as we require sudo permissions to install packages.

cd /etc/ansible/playbooks
sudo nano remove-vlc-ubuntu.yml
---
- hosts: all
  become: true
  tasks:
  - name: remove vlc Ubuntu
    apt:
      name: vlc
      state: absent
    when: ansible_distribution == 'Ubuntu'
sudo nano install-vlc-ubuntu.yml
---
- hosts: all
  become: true
  tasks:
  - name: install vlc Ubuntu
    apt:
      name: vlc
    when: ansible_distribution == 'Ubuntu'

Next we will run the remove

ansible-playbook --ask-become-pass remove-vlc-ubuntu.yml

It has been removed

Next we will install it back

ansible-playbook --ask-become-pass install-vlc-ubuntu.yml

it is now installed

By Kad