Install NuGet and Other Package Providers on Windows Using Ansible

Learn how to install the NuGet Package Provider using Ansible for Windows systems. This is essential for Ansible projects targeting Microsoft Windows hosts, such as those in the RWS GIS Platform.

Projects: c2platform/rws/ansible-gis, c2platform.wincore


When working with Ansible projects that target Windows hosts, you’ll often need to install the NuGet Package Provider. Without it, you may encounter the following message:

PowerShellGet requires NuGet provider version ‘’ or newer to interact with NuGet-based repositories

Ansible, currently, does not provide dedicated modules for this task, but you can manually install the NuGet Package Provider with the following PowerShell command:

Find-PackageProvider -name Nuget -ForceBootstrap -IncludeDependencies -Force

Alternatively, you can use the win_powershell Ansible module to accomplish this task.

Using the Role

An even more convenient approach is to use the Ansible role. This role offers a template for installing package providers like NuGet and Chocolatey in a controlled manner.

The advantage of using the role is that it requires minimal Ansible engineering; you only need to configure it. The RWS reference implementation project, c2platform/rws/ansible-gis provides an example of how to configure this role in the group_vars/windows/main.yml file. Here’s a snippet from that file:

    - name: Chocolatey Package Provider
      type: win_powershell
      script: "{{ lookup('ansible.builtin.template', '', template_vars={'win_package_provider': 'Chocolatey'}) }}"
      # debug_path: C:\vagrant\logs\chocolately-package-provider.yml
    - name: NuGet Package Provider
      type: win_powershell
      script: "{{ lookup('ansible.builtin.template', '', template_vars={'win_package_provider': 'Nuget'}) }}"

Template for Installing Package Providers

The template, part of the Ansible role, is included below for your convenience:

$Ansible.Failed = $false
$Ansible.Changed = $false
{% if win_proxy_url is defined %}
$proxy = '{{ win_proxy_url }}'
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
[]::defaultwebproxy = new-object$proxy)
[]::defaultwebproxy.BypassProxyOnLocal = $true
{% endif %}

Write-Output "Install {{ win_package_provider }} Package Provider (if not installed already)"

try {
    $packageProvider = Get-PackageProvider -Name {{ win_package_provider }}

    if (-not $packageProvider) {
        Write-Output "Installing {{ win_package_provider }} Package Provider"
        $result = Install-PackageProvider -Name {{ win_package_provider }} -Force -Confirm:$false

        if ($result -eq $null) {
            $Ansible.Failed = $true
            Write-Error "Failed to install {{ win_package_provider }} Package Provider"
        } else {
            $Ansible.Changed = $true

    if ($Ansible.Changed) {
        Write-Output "{{ win_package_provider }} Package Provider has been installed or updated."
    } elseif ($Ansible.Failed) {
        Write-Output "{{ win_package_provider }} Package Provider failed to install."
    else {
        Write-Output "{{ win_package_provider }} Package Provider is installed, and the installation was not changed"
catch {
    $Ansible.Failed = $true
    Write-Error "An error occurred: $_"