Debian Codegraph @

ansible_collections/community/windows/tests/integration/targets/setup_win_psget/tasks/main.yml

raw
1 # Installs PackageManagement and PowerShellGet to the required versions for testing
2 ---
3 - name: check if PackageManagement has been installed
4 ansible.windows.win_powershell:
5 script: |
6 $ErrorActionPreference = 'Stop'
7 $Ansible.Changed = $false
8
9 if (-not (Get-Command -Name Install-Module -ErrorAction SilentlyContinue)) {
10 [PSCustomObject]@{
11 Install = $true
12 Action = "scratch"
13 }
14 return
15 }
16
17 $psGet = Get-Module -Name PowerShellGet -ListAvailable |
18 Sort-Object -Property Version -Descending |
19 Select-Object -First 1 -ExpandProperty Version
20 $package = Get-Module -Name PackageManagement -ListAvailable |
21 Sort-Object -Property Version -Descending |
22 Select-Object -First 1 -ExpandProperty Version
23
24 if ($psGet -lt [Version]"1.6.0" -or $package -lt [Version]"1.1.7") {
25 [PSCustomObject]@{
26 Install = $true
27 Action = "module"
28 }
29 }
30 else {
31 [PSCustomObject]@{
32 Install = $false
33 }
34 }
35
36 register: module_installed
37
38 - name: bootstrap required modules
39 when: module_installed.output[0].Install
40 block:
41 - name: install PackageManagement for older hosts
42 ansible.windows.win_package:
43 path: https://ansible-ci-files.s3.amazonaws.com/test/integration/targets/setup_win_psget/PackageManagement_x64.msi
44 product_id: '{57E5A8BB-41EB-4F09-B332-B535C5954A28}'
45 state: present
46 when: module_installed.output[0].Action == "scratch"
47 register: download_res
48 until: download_res is successful
49 retries: 3
50 delay: 5
51
52 - name: remove the old versions of PackageManagement and PowerShellGet
53 ansible.windows.win_file:
54 path: C:\Program Files\WindowsPowerShell\Modules\{{ item }}
55 state: absent
56 when: module_installed.output[0].Action == "scratch"
57 loop:
58 - PackageManagement
59 - PowerShellGet
60
61 - name: create the required folder for nuget
62 ansible.windows.win_file:
63 path: C:\Program Files\PackageManagement\ProviderAssemblies\nuget\2.8.5.208
64 state: directory
65
66 - name: download nuget provider dll
67 ansible.windows.win_get_url:
68 url: https://ansible-ci-files.s3.amazonaws.com/test/integration/targets/setup_win_psget/Microsoft.PackageManagement.NuGetProvider-2.8.5.208.dll
69 dest: C:\Program Files\PackageManagement\ProviderAssemblies\nuget\2.8.5.208\Microsoft.PackageManagement.NuGetProvider.dll
70 force: false
71 register: nuget_download_res
72 until: nuget_download_res is successful
73 retries: 3
74 delay: 5
75
76 - name: download newer PackageManagement and PowerShellGet nupkg
77 ansible.windows.win_get_url:
78 url: '{{ item.url }}'
79 dest: '{{ remote_tmp_dir }}\{{ item.name }}.{{ "nupkg" if module_installed.output[0].Action == "module" else "zip" }}' # .zip is required for win_unzip
80 when: module_installed.output[0].Install
81 register: download_res
82 until: download_res is successful
83 retries: 3
84 delay: 5
85 loop:
86 - name: PackageManagement
87 url: https://ansible-ci-files.s3.amazonaws.com/test/integration/targets/setup_win_psget/packagemanagement.1.1.7.nupkg
88 - name: PowerShellGet
89 url: https://ansible-ci-files.s3.amazonaws.com/test/integration/targets/setup_win_psget/powershellget.1.6.0.nupkg
90
91 - name: extract new modules to correct location for older hosts
92 win_unzip:
93 src: '{{ remote_tmp_dir }}\{{ item }}.zip'
94 dest: C:\Program Files\WindowsPowerShell\Modules\{{ item }}
95 when: module_installed.output[0].Action == "scratch"
96 loop:
97 - PackageManagement
98 - PowerShellGet
99
100 - name: update PackageManagement and PowerShellGet
101 when: module_installed.output[0].Action == "module"
102 block:
103 - name: register local PSRepo
104 ansible.windows.win_powershell:
105 script: |
106 param($Path)
107
108 Register-PSRepository -Name LocalNuget -SourceLocation $Path
109 parameters:
110 Path: '{{ remote_tmp_dir }}'
111
112 - name: ensure PowerShellGet and PackageManagement requirements have been met
113 win_psmodule:
114 name: PowerShellGet
115 repository: LocalNuget
116 accept_license: true
117 state: present
118
119 always:
120 - name: unregister local PSRepo
121 ansible.windows.win_powershell:
122 script: |
123 if (Get-PSRepository -Name LocalNuget -ErrorAction SilentlyContinue) {
124 Unregister-PSRepository -Name LocalNuget
125 $Ansible.Changed = $true
126 }
127 else {
128 $Ansible.Changed = $false
129 }