Featured image of post How to Upgrade Dotnet Core Runtime With Ansible

How to Upgrade Dotnet Core Runtime With Ansible

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
---
- name: Upgrade .NET and .NET CORE VERSION ON WINDOWS SERVER
  hosts: "windows"
  gather_facts: no
  vars:
    #User Define
    # Find the information asp .net core hosting bundle from here https://dotnet.microsoft.com/en-us/download
    # If you want to upgrade .net 7 or 8. It will support. Change download_url, checksum and latest_dotnet_core_full_version
    download_url: https://download.visualstudio.microsoft.com/download/pr/34343c71-eb52-4537-b2b9-f25bc8b6c894/c6a39b3b387ad3f9662cd77c220902f5/dotnet-hosting-6.0.23-win.exe
    checksum: b0fab7d01aac899623fa5dc79940749ee58b29980d82d16e6aca794e323de46234b2263d70ba7e83cf989cb6c48d07e293f25a39b748e5a88874791566d38616
    latest_dotnet_core_full_version: 6.0.23
    
    # Don't change anything here,please.
    latest_dotnet_core_major_version: "{{ latest_dotnet_core_full_version.split('.')[0] }}"
    temp_dir: D:\installer
    checksum_algorithm: sha512
    installer: "{{ temp_dir }}\\dotnet-hosting-win.exe" 
    log_path: "{{ temp_dir }}\\dotnet-hosting-win.exe.log"    

  tasks:
    - name: Check if .NET Core is installed
      win_reg_stat:
        path: HKLM:\SOFTWARE\dotnet\Setup\InstalledVersions\x64\sharedhost
        name: Version
      register: dotnet_registry_key
      ignore_errors: yes

    - name: Extract .NET Core version if installed
      set_fact:
        installed_dotnet_core_full_version: "{{ dotnet_registry_key.value }}"
      when: dotnet_registry_key.exists

    - name: display installed_dotnet_core_full_version
      debug:
        msg: "{{installed_dotnet_core_full_version}}"
      when: dotnet_registry_key.exists

    - name: Extract installed dotnet core major version if installed 
      set_fact:
        installed_dotnet_core_major_version: "{{ installed_dotnet_core_full_version.split('.')[0] }}"
      when: dotnet_registry_key.exists

    - name: display installed dotnet core major version if installed 
      debug:
        msg: "{{installed_dotnet_core_major_version}}"
      when: dotnet_registry_key.exists

    - name: Check if installed .NET Core major version is {{ latest_dotnet_core_major_version }} 
      debug:
        msg: "Installed .NET Core major version is {{ latest_dotnet_core_major_version }}"
      when: 
        - dotnet_registry_key.exists
        - installed_dotnet_core_major_version is version(latest_dotnet_core_major_version, '=' ) 

    - name: .NET Core is not installed or not major version {{ latest_dotnet_core_major_version }} 
      debug:
        msg: ".NET Core is not installed or not  major version {{latest_dotnet_core_major_version}}"
      when: not(dotnet_registry_key.exists  and installed_dotnet_core_major_version is version(latest_dotnet_core_major_version, '=' ) )


    - name: Current installed .NET Core Version
      debug:
        msg: "Currently Install .net core version is {{ installed_dotnet_core_full_version}}"
      when:
        - dotnet_registry_key.exists
        - installed_dotnet_core_full_version is version(latest_dotnet_core_full_version, "<")

    - name: Download  dotnet  {{ latest_dotnet_core_full_version }} hosting bundle installer and checksum downloaded file
      win_get_url:
        url: "{{ download_url }}"
        dest: "{{ installer }}"
        checksum: "{{ checksum }}"
        checksum_algorithm: "{{ checksum_algorithm }}"
        force: True
      when:
        - dotnet_registry_key.exists
        - installed_dotnet_core_full_version is version(latest_dotnet_core_full_version, "<")

    - name: Install dotnet {{ latest_dotnet_core_full_version }} hosting bundle  installer 
      win_command: 
        "{{ installer }}  /install /quiet  /log  {{ log_path }}"
      become: yes
      when:
        - dotnet_registry_key.exists
        - installed_dotnet_core_full_version is version(latest_dotnet_core_full_version, "<")

    - name: Clean up dotnet  {{ latest_dotnet_core_full_version }} hosting bundle installer
      win_file:
        path:  "{{ installer }}"
        state: absent
      become: yes
      when:
        - dotnet_registry_key.exists
        - installed_dotnet_core_full_version is version(latest_dotnet_core_full_version, "<")

    - name: Check .NET Core Version in Registry
      ansible.windows.win_reg_stat:
        path: 'HKLM:\SOFTWARE\dotnet\Setup\InstalledVersions\x64\sharedhost'
        name: "Version"
      register: check_dotnetcore_version
      when:
        - dotnet_registry_key.exists
      
    - name: Check .NET Core Version after install finished
      debug:
        msg: "Currently Installed .net core version is {{ check_dotnetcore_version.value}}"
      when:
        - dotnet_registry_key.exists
Built with Hugo
Theme Stack designed by Jimmy