summaryrefslogtreecommitdiff
path: root/test/integration/targets/setup_win_psget/tasks/main.yml
blob: 6c84b903c9064c3e86bcc56f7a7212ce810843dc (plain)
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
# Installs PackageManagement and PowerShellGet to the required versions for testing
---
- name: check if PackageManagement has been installed
  win_shell: if (Get-Command -Name Install-Module -ErrorAction SilentlyContinue) { $true } else { $false }
  changed_when: False
  register: module_installed

- name: install PackageManagement and PowerShellGet
  when: not module_installed.stdout | trim | bool
  block:
  - name: install PackageManagement
    win_package:
      path: https://ansible-ci-files.s3.amazonaws.com/test/integration/targets/setup_win_psget/PackageManagement_x64.msi
      product_id: '{57E5A8BB-41EB-4F09-B332-B535C5954A28}'
      state: present

  - name: create the required folder
    win_file:
      path: C:\Program Files\PackageManagement\ProviderAssemblies\nuget\2.8.5.208
      state: directory

  - name: download nuget provider dll
    win_get_url:
      url: https://ansible-ci-files.s3.amazonaws.com/test/integration/targets/setup_win_psget/Microsoft.PackageManagement.NuGetProvider-2.8.5.208.dll
      dest: C:\Program Files\PackageManagement\ProviderAssemblies\nuget\2.8.5.208\Microsoft.PackageManagement.NuGetProvider.dll

- name: get version and install location of PackageManagement and PowerShellGet
  win_shell: |
    $info = @{}
    $modules = Get-Module -ListAvailable | Where-Object {
        ($_.Name -eq "PackageManagement" -and $_.Version -lt "1.1.7") -or ($_.Name -eq "PowerShellGet" -and $_.Version -lt "1.6.0")
    } | ForEach-Object {
        $module_info = @{}
        if ([System.IO.Path]::GetFileName($_.ModuleBase) -eq $_.Name) {
            $module_info.remove_path = $_.ModuleBase
            $module_info.install_path = $_.ModuleBase
        } else {
            $module_version = switch($_.Name) {
                PackageManagement { "1.1.7.0" }
                PowerShellGet { "1.6.0" }
            }
            $module_info.remove_path = ""
            $module_info.install_path = ([System.IO.Path]::Combine([System.IO.Path]::GetDirectoryName($_.ModuleBase), $module_version))
        }
        $info.($_.Name) = $module_info
    }

    ConvertTo-Json -InputObject $info -Compress
  changed_when: False
  register: installed_modules

- name: register installed_modules info
  set_fact:
    installed_modules: '{{ installed_modules.stdout | trim | from_json }}'

- name: update the PackageManagement and PowerShellGet versions
  when: installed_modules.keys() | list | length > 0
  block:
  - name: download newer PackageManagement and PowerShellGet nupkg
    win_get_url:
      url: '{{ item.url }}'
      dest: '{{ remote_tmp_dir }}\{{ item.name }}.zip'  # .zip is required for win_unzip
    when: item.name in installed_modules
    loop:
    - name: PackageManagement
      url: https://ansible-ci-files.s3.amazonaws.com/test/integration/targets/setup_win_psget/packagemanagement.1.1.7.nupkg
    - name: PowerShellGet
      url: https://ansible-ci-files.s3.amazonaws.com/test/integration/targets/setup_win_psget/powershellget.1.6.0.nupkg

  - name: remove the old versions of PackageManagement and PowerShellGet
    win_file:
      path: '{{ item.value.remove_path }}'
      state: absent
    # This isn't necessary on 2016+ as packages are installed in a version specific dir
    when: item.value.remove_path != ""
    with_dict: '{{ installed_modules }}'

  - name: extract new modules to correct location
    win_unzip:
      src: '{{ remote_tmp_dir }}\{{ item.name }}.zip'
      dest: '{{ item.path }}'
    when: item.path != ""
    loop:
    - name: PackageManagement
      path: '{{ installed_modules.PackageManagement.install_path | default("") }}'
    - name: PowerShellGet
      path: '{{ installed_modules.PowerShellGet.install_path | default("") }}'