Search code examples
ansiblenotepad++

Why does Ansible win_package not uninstall Notepad++?


Ansible v2.11.0

Instead of win_chocolatey, I ended up installing Notepad++ via win_package module.

- name: "Install {{ notepadpp_product_id }} version {{ notepadpp_version }}"
  win_package:
    path: "{{ remote_temp }}\\notepadplusplus.exe"
    arguments: /S
    product_id: "{{ notepadpp_product_id }}"
    state: present
  when: install

and I get this when I run the playbook, and Notepad++ is indeed installed and I see it in `C:\Program Files\Notepad++``

TASK [exa-prereqs : Install Notepad++ (64-bit x64) version 7.9.5] **************
changed: [10.227.xx.xx]

However when I run the uninstaller, by passing a -e '{install: false}', it doesn't get uninstalled, that is, C:\Program Files\Notepad++ folder and contents are still there, and still shows up in Control Panel -> Uninstall programs list.

- name: "Uninstall {{ notepadpp_product_id }} version {{ notepadpp_version }}"
  win_package:
    path: "{{ notepadpp_install_dir }}\\uninstall.exe"
    arguments: /S
    product_id: "{{ notepadpp_product_id }}"
    state: absent
  ignore_errors: true
  when: not install

I see this when running the playbook with verbose option.

TASK [exa-prereqs : Uninstall Notepad++ (64-bit x64) version 7.9.5] ************
task path: /path/ansible/exa-playbooks/roles/exa-prereqs/tasks/notepadpp.yml:16
redirecting (type: modules) ansible.builtin.win_package to ansible.windows.win_package
Using module file /usr/local/Cellar/ansible/3.3.0/libexec/lib/python3.9/site-packages/ansible_collections/ansible/windows/plugins/modules/win_package.ps1
Pipelining is enabled.
<10.227.xx.xx> ESTABLISH WINRM CONNECTION FOR USER: jenkins on PORT 5986 TO 10.227.xx.xx
EXEC (via pipeline wrapper)
ok: [10.227.xx.xx] => {
    "changed": false,
    "invocation": {
        "module_args": {
            "arguments": "/S",
            "chdir": null,
            "client_cert": null,
            "client_cert_password": null,
            "creates_path": null,
            "creates_service": null,
            "creates_version": null,
            "expected_return_code": [
                0,
                3010
            ],
            "follow_redirects": "safe",
            "force_basic_auth": false,
            "headers": null,
            "http_agent": "ansible-httpget",
            "log_path": null,
            "maximum_redirection": 50,
            "password": null,
            "path": "C:\\Program Files\\Notepad++\\uninstall.exe",
            "product_id": "Notepad++ (64-bit x64)",
            "provider": "auto",
            "proxy_password": null,
            "proxy_url": null,
            "proxy_use_default_credential": false,
            "proxy_username": null,
            "state": "absent",
            "url_method": null,
            "url_password": null,
            "url_timeout": 30,
            "url_username": null,
            "use_default_credential": false,
            "use_proxy": true,
            "username": null,
            "validate_certs": true,
            "wait_for_children": false
        }
    },
    "rc": 0,
    "reboot_required": false
}
META: ran handlers
META: ran handlers

Odd thing is, I use the same task for uninstalling another package (7-Zip) and it works for that. What gives?


Solution

  • I struggled with the same problem recently so I post my findings here.

    If you list the packages with choco:

    PS C:\Windows\system32> choco list --localonly
    Chocolatey v1.2.1
    chocolatey 1.2.1
    chocolatey-compatibility.extension 1.0.0
    chocolatey-core.extension 1.4.0
    notepadplusplus 8.4.9
    notepadplusplus.install 8.4.9
    

    ...you will see two entries for Nodepad++.

    In my ansible playbook I created two task: one for notepadplusplus and one for notepadplusplus.install

    - name: "Gather stats for {{ notepadpp_install_location }}"
        win_stat:
          path: "{{ notepadpp_install_location }}"
        register: notepadpp_dir
          
    - name: "Uninstall {{ notepadpp_product_id }} version {{ notepadpp_version }}"
        win_package:
          path: "{{ notepadpp_install_location }}\\uninstall.exe"
          arguments: /S
          product_id: "{{ notepadpp_product_id }}"
          state: absent
        when: notepadpp_dir.stat.exists == True
    
      - name: "Uninstall {{ notepadpp_product_id}}.install version {{ notepadpp_version }}"
        win_chocolatey:
          name: "{{ notepadpp_product_id}}.install"
          version: "{{ notepadpp_version }}"
          state: absent
    

    variables

    notepadpp_product_id: notepad++
    notepadpp_product_name: notepadplusplus
    notepadpp_version: 8.4.9
    notepadpp_install_location: C:\\Program Files\\Notepad++
    

    thanks to my co-worker Marco for pointing out notepadplusplus.install