Hello together,
hope that someone can explain it to me or help me to understand.
I try to only “upgrade” for example notepadplusplus.install via ansible and chocolatey to Windows Machines.
My Expectation by using this Code on a collection of Servers was:
When choosing present or latest with win_chocolatey that means, install it when it is not there. And latest also means, when it is already there, upgrade when a newer version is available.
If you want to only upgrade if it is already present, you first need to check if it is already there. With ansible.windows.win_stat you can check if the C:\Program Files\Notepad++\notepad++.exe file already exists
That’s the way the latest works. If you look at the documentation:
When latest' or upgrade’, will ensure the package is installed to the latest available version.
The way I read your question is you don’t want the package to be installed if it’s not there, but you want it updated if it is. This module cannot do that, afaict.
You could for instance stat to see if the binary exists and if so, then update it to latest.
Edit: and I think @tonk and I typed simulataneously
I like to install, when exists is true
maybe you give me one more Help
I got it:
tasks:
- name: Obtain information about a file
ansible.windows.win_stat:
path: C:\Program Files\Notepad++\notepad++.exe
register: file_info
- name: Upgrade Notepad++ when it is installed
win_chocolatey:
name: notepadplusplus.install
state: latest
when: file_info.stat.exists
Couple things, you could use the win_chocolatey_facts module to find an accurate list of outdated chocolatey packages to upgrade. If you’re only concerned about specific packages, that will be an easier/more consistent way of finding your installed packages.
tasks:
- name: Gather chocolatey facts
chocolatey.chocolatey.win_chocolatey_facts:
- name: Upgrade Notepad++ when it is installed
chocolatey.chocolatey.win_chocolatey:
name: notepadplusplus.install
state: latest
when:
- ansible_chocolatey.outdated | length > 0
- "notepadplusplus.install" in ansible_chocolatey.outdated|map(attribute='name')|flatten
However, if you want to upgrade all chocolatey packages that are already installed, there’s a much easier way to do that. From the documentation:
- name: Upgrade all chocolatey installed packages
chocolatey.chocolatey.win_chocolatey:
name: all # Set to "all" to run the action on all of the installed packages
state: latest