Check for existing installed software

How can I incorporate this registry check before my playbook runs to install .net 4.8:

(Get-ItemProperty “HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full”).version -lt 4.8

The above should only run my playbook, if 4.8 isnt installed. If version is less than 4.8 run the below playbook, else skip

My playbook to install .net 4.8:

  • name: Copy .net 4.8 Files
    ansible.windows.win_copy:
    src: /etc/ansible/roles/net481/files/ndp48-x86-x64-allos-enu.exe
    dest: C:\Scripts
    state: present

  • name: Install .net 4.8
    ansible.builtin.script: /etc/ansible/roles/onprembaseline/files/net48.ps1
    register: script_run

  • name: Reboot after .NET 4.8 Install

ansible.windows.win_reboot:

Thanks.

any suggestions?

anyone with ideas?

Not a ansible hero here, but i think you can do something with the when: command in your playbook.

So somewhere in de the task where you check the version you will do a
register: dotnet_version

And in the taks where you execute it:
when: “‘4.8’ in {{ dotnet_version }}”

Probably check: https://docs.ansible.com/ansible/latest/user_guide/playbooks_conditionals.html

This is what I get:

TASK [net481 : Check if .net 4.8 is installed] **************************************************************************************************************************************************************************
task path: /etc/ansible/roles/net481/tasks/main.yml:1
[WARNING]: conditional statements should not include jinja2 templating delimiters such as {{ }} or {% %}. Found: ‘4.8’ in {{ script_run }}
fatal: [xxxxx]: FAILED! => {
“msg”: “The conditional check ‘‘4.8’ in {{ script_run }}’ failed. The error was: error while evaluating conditional (‘4.8’ in {{ script_run }}): ‘script_run’ is undefined\n\nThe error appears to be in ‘/etc/ansible/roles/net481/tasks/main.yml’: line 1, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: Check if .net 4.8 is installed\n ^ here\n”
}

Role is this:

  • import_role:
    name: net481
    when: “‘4.8’ in {{ script_run }}”

Task Ran:

  • name: Check if .net 4.8 is installed
    ansible.windows.win_shell:
    (Get-ItemProperty “HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full”).version -lt 4.8
    register: script_run - name: Install .net 4.8, if not installed

ansible.builtin.script: /etc/ansible/roles/onprembaseline/files/net48.ps1
register: script_run

Use idempotent way. It will be installed if it has not already been installed

  • ansible.windows.win_package:
    path: ‘c:\path\to\ndp48-x86-x64-allos-enu.exe’
    product_id: “{BAAF5851-0759-422D-A1E9-90061B597188}”
    arguments: “/q /norestart”

среда, 19 января 2022 г. в 21:33:41 UTC+3, Nitrous:

Thanks.

I have a pretask before installing, where it copies files to the target server, and then run your idempotent way.

How can I do a check, before copying the .net 4.8 exe, if 4.8 exists, and only run the copy/install if .net 4.8 isnt installed/exists.

If it exists, then skip.

Thanks

hm. try to use win_reg_stat with register variable to determine what version your need is installed/not installed https://docs.microsoft.com/en-us/dotnet/framework/migration-guide/how-to-determine-which-versions-are-installed#use-registry-editor
and wrap all other installation tasks with block and when condition

пятница, 4 февраля 2022 г. в 01:09:06 UTC+3, Nitrous:

Thanks, mind showing me an example, on how to use the win_reg_stat with register variable?

Bonjour. je suis nouveau dans l’utilisation de ansible. Besoin d’aide
Comment pointer sur la nouvelle version de ansible puisqu’on me dit que ansible2.7.10 est dépassé.
voici le message d’erreur. Je vous remercie.
/home/user-ansible/ansible2.7.10/local/lib/python2.7/site-packages/ansible/parsing/vault/init.py:41: CryptographyDeprecationWarning: Python 2 is no longer supported by the Python core team. Support for it is now deprecated in cryptography, and will be removed in the next release.
from cryptography.exceptions import InvalidSignature
ERROR! - the directory ./apache already exists.you can use --force to re-initialize this directory,
however it will reset any main.yml files that may have
been modified there already.