# Only Upgrade but NOT install a software by chocolatey

**URL:** <https://forum.ansible.com/t/only-upgrade-but-not-install-a-software-by-chocolatey/6621>\
**Category:** Get Help\
**Tags:** windows, playbook\
**Created:** [June 18, 2024, 7:42am UTC](https://forum.ansible.com/t/only-upgrade-but-not-install-a-software-by-chocolatey/6621 "2024-06-18T07:42:15Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![mastadook](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/mastadook/32/3889_2.png) [@mastadook](https://forum.ansible.com/u/mastadook)\
**Post date:** [June 18, 2024, 7:42am UTC](https://forum.ansible.com/t/only-upgrade-but-not-install-a-software-by-chocolatey/6621/1 "2024-06-18T07:42:15Z")

</div>

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:

```auto
  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:

```auto
  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:

```auto
- 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

---

<div class="post-metadata">

**Author:** ![tonk](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/tonk/32/214_2.png) [@tonk](https://forum.ansible.com/u/tonk)\
**Post date:** [June 19, 2024, 7:33am UTC](https://forum.ansible.com/t/only-upgrade-but-not-install-a-software-by-chocolatey/6621/2 "2024-06-19T07:33:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jpmens](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jpmens/32/5543_2.png) [@jpmens](https://forum.ansible.com/u/jpmens)\
**Post date:** [June 19, 2024, 7:41am UTC](https://forum.ansible.com/t/only-upgrade-but-not-install-a-software-by-chocolatey/6621/3 "2024-06-19T07:41:08Z")

</div>

> [@mastadook](#):
>
> But this is not true, it also installs it when it is not installed before.

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 😉

---

<div class="post-metadata">

**Author:** ![tonk](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/tonk/32/214_2.png) [@tonk](https://forum.ansible.com/u/tonk)\
**Post date:** [June 19, 2024, 7:43am UTC](https://forum.ansible.com/t/only-upgrade-but-not-install-a-software-by-chocolatey/6621/4 "2024-06-19T07:43:06Z")

</div>

> Edit: and I think @tonk and I typed simulataneously 😉  
> True 🙂

---

<div class="post-metadata">

**Author:** ![mastadook](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/mastadook/32/3889_2.png) [@mastadook](https://forum.ansible.com/u/mastadook)\
**Post date:** [June 19, 2024, 9:39am UTC](https://forum.ansible.com/t/only-upgrade-but-not-install-a-software-by-chocolatey/6621/5 "2024-06-19T09:39:40Z")

</div>

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

```auto
  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:

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

```

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

I got it:

```auto
  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

```

😃

---

<div class="post-metadata">

**Author:** ![Denney-tech](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/denney-tech/32/1556_2.png) [@Denney-tech](https://forum.ansible.com/u/Denney-tech)\
**Post date:** [June 19, 2024, 7:36pm UTC](https://forum.ansible.com/t/only-upgrade-but-not-install-a-software-by-chocolatey/6621/6 "2024-06-19T19:36:40Z")

</div>

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.

```yaml
  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](https://docs.ansible.com/ansible/latest/collections/chocolatey/chocolatey/win_chocolatey_module.html#parameter-name):

```yaml
   - 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

```

---

<div class="post-metadata">

**Author:** ![mastadook](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/mastadook/32/3889_2.png) [@mastadook](https://forum.ansible.com/u/mastadook)\
**Post date:** [June 20, 2024, 8:52am UTC](https://forum.ansible.com/t/only-upgrade-but-not-install-a-software-by-chocolatey/6621/7 "2024-06-20T08:52:52Z")

</div>

Hi Denney,

thanks for this hints, they alspo work great for me. 🙂

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [July 20, 2024, 8:53am UTC](https://forum.ansible.com/t/only-upgrade-but-not-install-a-software-by-chocolatey/6621/8 "2024-07-20T08:53:18Z")

</div>

Closed on July 20, 2024, 8:53am UTC

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