Only Upgrade but NOT install a software by chocolatey

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:

  tasks:
   - name: Installing Notepad++
     win_chocolatey:
      name: notepadplusplus.install
      state: latest

it should only upgrade on systems that has the Software installed.
But this is not true, it also installs it when it is not installed before.

Installation where it is not installed, should happen this way:

  tasks:
   - name: Installing Notepad++
     win_chocolatey:
      name: notepadplusplus.install
      state: present

what can I do, to ONLY upgrade the Software where it installed???

this are the Examples from ansible Page:

- name: Install git
  win_chocolatey:
    name: git
    state: present

- name: Upgrade installed packages
  win_chocolatey:
    name: all
    state: latest

hope someone can help here

regards

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

1 Like

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 :wink:

1 Like

Edit: and I think @tonk and I typed simulataneously :wink:
True :slight_smile:

thank you both.
I´m sure it was a missunderstanding from my side.
I read that “latest” just update and “present” install it.

You help me both, thank you again

How can I start the Installation when the “stat - exists” Value is true?
Nothing what i try works for me

  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 = true

part of the output:

 stat:
    attributes: Archive
    checksum: b4fb61430e6af6f1bd95c26c6123ba077960b079
    creationtime: 1717455076
    exists: true
    extension: .exe
    filename: notepad++.exe

I like to install, when exists is true :slight_smile:
maybe you give me one more Help :slight_smile:

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

:smiley:

1 Like

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

Hi Denney,

thanks for this hints, they alspo work great for me. :slight_smile:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.