community.zabbix icon indicating copy to clipboard operation
community.zabbix copied to clipboard

The actual roles just consider that will install the components but it's not possible to uninstall/remove the components

Open possebon opened this issue 3 years ago • 6 comments

SUMMARY

When I had some issues installing the Zabbix Agent using the role, I looked at how to revert all actions made by the playbook and could not find any way to do it. Since I'm installing the Zabbix Agent on a new environment, before installing it on all hosts, I do some tests on specific hosts to ensure that it will work as expected. During my tests, it's sometimes interesting (or even required) to uninstall the Zabbix Agent and to do that now, I have to remove it manually on hosts.

ISSUE TYPE
  • Feature Idea
COMPONENT NAME
  • zabbix_agent
  • zabbix_javagateway
  • zabbix_proxy
  • zabbix_server
  • zabbix_web
ADDITIONAL INFORMATION
  1. On zabbix-ansible-role/defaults/main.yml I would define a variable flag

flag_remove: false

  1. On zabbix-ansible-role/tasks/main.yml I would have
- name: task foo bar
  command: do something here

- name: undoing task foo bar
  command: undo something here
  when: flag_remove=true

Not all tasks need to be "undone" when removing the component. I did not looked all tasks of component, just the role of Zabbix Agent, and does not look too much work to accomplish that.

  1. So by default, our flag is always false. So when installing things I would use the first command below to run my playbook. And to uninstall I would use the second command.
ansible-playbook myplaybook.yml
ansible-playbook myplaybook.yml --extra-vars "flag_remove=true"

If this approach is acceptable, I can work on all components to implement it and then submit a PR.


possebon avatar Jul 18 '20 15:07 possebon

Hi @possebon

What kind of components do you want to uninstall? For the agent there is at least the zabbix_agent_package_state which can be set to absent and there is also a zabbix_server_package_state with setting it to absent will remove the package. But it is only the rpm/deb package. So very curious what other things you would have seen.

dj-wasabi avatar Jul 18 '20 15:07 dj-wasabi

Hi @dj-wasabi

Probably I choose the term component incorrectly.

My first need is about uninstalling the Zabbix Agent on hosts, and to uninstalling I mean, in Windows for instance, stop the Zabbix Agent service, delete the Zabbix Agent service, remove the zabbix-agent folder.

I saw the zabbix_agent_package_state too and I understood that it will remove the package on Linux, but it's not clear what will do on Windows since on Windows there is no package concept.

I looked at tasks on different platforms, at least on zabbix-agent role, and I can be wrong, but on Windows the zabbix_agent_package_state is only used as a condition to set a fact of update the agent.

I think that on Linux platforms the zabbix_agent_package_state can be used to uninstall the agent, because, on RedHat for example, it will do a yum/dnf remove package when zabbix_agent_package_state is set to absent, but on Windows it will have effect only to update the Zabbix Agent.

possebon avatar Jul 19 '20 14:07 possebon

Hi @possebon

Ok, clear. Yes, Windows is best effort for me as I don't have any machines running Windows to test it. So if you can provide a PR for that, then I don't see any reasons to not merge it.

But yes, zabbix_agent_package_state setting this to absent will make sure at least the package is removed from the host. But maybe some other files needs to be removed as well, to make sure it is really gone.

dj-wasabi avatar Jul 20 '20 06:07 dj-wasabi

Hi,

Because the var zabbix_agent_package_state=absent was insufficient and would leave plenty of files behind, including a broken systemd service, I've written a role to cleanup the Zabbix agent packages and files (on Ubuntu).

- hosts: linux
  become: true
  serial: 5
  vars:
    allow_world_readable_tmpfiles: true
    ansible_shell_allow_world_readable_temp: true

  tasks:
    - name: Populate service facts
      service_facts:

    - debug:
        msg: Zabbix is installed!
      when: ansible_facts.services['zabbix-agent.service'] is defined

    - name: Stop service Zabbix Agent if running
      ansible.builtin.systemd_service:
        name: zabbix-agent
        state: stopped
      when: ansible_facts.services['zabbix-agent.service'] is defined

    - name: Disable service Zabbix Agent
      ansible.builtin.systemd_service:
        name: zabbix-agent
        enabled: false
      when: ansible_facts.services['zabbix-agent.service'] is defined

    - name: Remove zabbix-agent package
      ansible.builtin.apt:
        name: zabbix-agent
        state: absent

    - name: Remove zabbix-get package
      ansible.builtin.apt:
        name: zabbix-get
        state: absent

    - name: Remove zabbix-sender package
      ansible.builtin.apt:
        name: zabbix-sender
        state: absent

    - name: Delete Zabbix files
      ansible.builtin.file:
        state: absent
        path: "{{ item }}"
      with_items:
        - /etc/zabbix
        - /etc/init.d/zabbix-agent
        - /etc/systemd/system/zabbix-agent.service
        - /etc/logrotate.d/zabbix-agent
        - /var/lib/dpkg/info/zabbix-agent.list
        - /var/lib/dpkg/info/zabbix-agent.postrm
        - /var/lib/systemd/deb-systemd-helper-enabled/zabbix-agent.service.dsh-also
        - /var/lib/systemd/deb-systemd-helper-enabled/multi-user.target.wants/zabbix-agent.service
        - /var/lib/systemd/deb-systemd-helper-masked/zabbix-agent.service
        - /var/log/zabbix

Regards,

DamienDaco avatar Mar 05 '24 12:03 DamienDaco

@DamienDaco would you please create a PR? Can you do this for other Linux distributions?

BGmot avatar Mar 07 '24 13:03 BGmot

@DamienDaco would you please create a PR? Can you do this for other Linux distributions?

Sure , I'll make a PR :) I've got Ubuntu at work, not sure about other distros but I could spin a few VMs up.

DamienDaco avatar Mar 07 '24 19:03 DamienDaco