Remove package ansible playbook

Removing the package from playbook will ensure that package is not installed on a newly provisioned machine, but the package will not be removed if that is already installed on a provisioned machine. If possible, you can destroy the machine using vagrant destroy and create/provision a new one.

If it is not possible to destroy the machine and provision a new one then you can add an ansible task to remove the installed package. Using absent as the state will remove the package.

- name: Remove TIG
  action: apt pkg=tig state=absent

Ansible cannot automatically remove packages when you remove them from you playbook. Ansible is stateless. This means it will not keep track of what it does and therefore does not know what it did in recent runs or if your playbook/role has been modified. Ansible will only do what you explicitly describe in the playbook/role. So you have to write a task to remove it.

You can easily do this with the apt module.

- name: Remove TIG
  apt:
    pkg: tig
    state: absent
  become: true

Sudo is now deprecated and no longer recommended. Use become instead.

- name: Remove TIG
  apt:
    pkg: tig
    state: absent
  become: yes