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.
This is the playbook-
-
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: Install on Linux
ansible.builtin.yum:
name: https://corretto.aws/downloads/latest/amazon-corretto-17-aarch64-linux-jdk.rpm
state: present
when: ansible_facts[âos_familyâ] == âRedHatâ
-
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.
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â.
Thanks for the information. Will try and update
no worries. Thanks for checking
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.
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.
Sorry for the delay in reply . Got stuck in some other work.
Thanks. will check this solution