Ansible script to uninstall multiple versions of OMS agent

585 views Asked by At

I wanted to do an uninstallation of OMS agent in our Linux machines. Unfortunately, we do have different OMS agent versions assigned to each machine. I hard coded the version from my Ansible script

command: sudo {{ file_path }}/omsagent-1.13.9-0.universal.x64.sh —-purge 

It only works for machine with that same OMS agent version else, it will fail.

I tried adding wildcard syntax, but it is getting an error stating that command not found

stderr: “sudo :/home/filename/omsagent-* : command not found

if I changed my previous command to

command: sudo {{file_path}}/omsagent-*.universal.x64.sh —-purge 
1

There are 1 answers

4
U880D On

Since I do not have this specific agent in place, I can't provide a full tested working example, but some some guidance.

According the package documentation and All bundle operations, the bundle has an option for

  --version-check        Check versions already installed to see if upgradable.

which should provide the installed version. Furthermore any installed agent has an directory with service control script

/opt/microsoft/omsagent/bin/service_control ...

and probably others, like scxadmin --version. By executing one or the other it should be possible to gather the correct installed version of the agent.

- name: Gather installed OMS agent version
  become: true
  become_method: sudo
  shell: 
    cmd: /opt/microsoft/omsagent/bin/service_control status | grep <whatever is necessary to get the version string only>
  register: VERSION
  changed_when: false
  check_mode: false

Please take note that instead of using sudo within the command, you should use become. Since it is a version reporting task only, you should also use changed_when and check_mode.

After the correct version is gathered you use it like

- name: Purge installed OMS agent version
  become: true
  become_method: sudo
  shell: 
    cmd: "omsagent-{{ VERSION }}.universal.x64.sh —-purge"

Is there any reason why the option --upgrade or --force can`t be used?

You may also have a look into How to troubleshoot issues with the Log Analytics agent for Linux, there is a standalone versionless purge script available.