A playbook is not working (with no error) after manual removal of the package from the server

Example: I installed java using ansible playbook on remote server. Later someone uninstalled manually. Then I tried installing java again using the same playbook.
the playbook is running successfully but not installing java this time.
Tried restarting the remote server and running playbook again. but no luck.

Do you get any useful output when you run the playbook with maximum verbosity?

If not, please provide the contents of the playbook and evidence that Java is not installed on the remote system, as a start to troubleshooting.

1 Like

This is the playbook-

  • name: install Java
    hosts: all
    tasks:

Output on Remote machine:
PS C:\Users\XXXX> java
java : The term ‘java’ is not recognized as the name of a cmdlet, function, script file, or operable program. Check
the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1

  • java
  •   + CategoryInfo          : ObjectNotFound: (java:String) [], CommandNotFoundException
      + FullyQualifiedErrorId : CommandNotFoundException

Unfortunately, I don’t have any win_chocolatey experience. I was hoping to see something the verbose output that I could give a tip on researching, but I’m out of my depth on this one.

The problem is that someone removed java manually instead of through chocolatey. Chocolatey makes its own database to track what it has installed, so it thinks that java is still installed. You can probably try state: reinstalled and to get it working again. Unfortunately, I don’t have enough experience with this to tell you how to detect false-positive chocolatey installs ahead of time.

1 Like

If that’s the case, I would stat the installed binary to see if it exists. If it doesn’t, run the chocolatey module again with the state: reinstall flag.

Edit: Autocorrect changed “stat” to “stay”.

3 Likes

Thanks for the information. Will try and update

2 Likes

no worries. Thanks for checking

2 Likes

Thanks. The playbook worked after changing the state from present to reinstalled.
But It would be great if we can add any condition to detect false-positive chocolatey installs just like you said

I think that’s what @Dustin was suggesting. You can use the stat: module to test if the java.exe binary exists where it’s supposed to be, and force a reinstall when it’s missing. Personally, I would prefer there be some native choco validate command or something that would scale to other application installs better, but I don’t know if anything like that exists.

1 Like

I checked the Chocolatey Documentation, and I didn’t see any reference to a module or parameter to verify a specific file to determine if the install was actually there.

I would recommend you open a feature request with the chocolatey project for your use case, to help Chocolately more intelligently determine install status.

As a workaround, I’d do something like the below playbook. Keep in mind, I’m doing this off the top of my head and not entirely familiar with Chocolately_module. So, your mileage may vary.

- name: install Java
  hosts: all
  tasks:
    - name: Install corretto17jdk
      win_chocolatey:
        name: corretto17jdk
        version: ‘17.X.X.X’
        state: present
      when: ansible_facts[‘os_family’] == “Windows”

    - name: Validate Executable Exists
      ansible.builtin.stat:
        path: /path/to/java/binary
      register: java

    - name: Force Reinstall if Binary Missing
      win_chocolatey:
        name: corretto17jdk
        version: ‘17.X.X.X’
        state: reinstalled
      when: ansible_facts[‘os_family’] == “Windows” and not java.stat.exists

Notice I install first, this will tackle first time installs. I re-install second, only if the java file does not exist. That way it will not re-install unless necessary.

1 Like

Sorry for the delay in reply . Got stuck in some other work.
Thanks. will check this solution

1 Like