+

Search Tips   |   Advanced Search

Windows Remote Management

Unlike Linux/Unix hosts, which use SSH by default, Windows hosts are configured with WinRM. This topic covers how to configure and use WinRM with Ansible.

Topics


What is WinRM?

WinRM is a management protocol used by Windows to remotely communicate with another server. It is a SOAP-based protocol that communicates over HTTP/HTTPS, and is included in all recent Windows operating systems. Since Windows Server 2012, WinRM has been enabled by default, but in most cases extra configuration is required to use WinRM with Ansible.

Ansible uses the pywinrm package to communicate with Windows servers over WinRM. It is not installed by default with the Ansible package, but can be installed by running the following:

on distributions with multiple python versions, use pip2 or pip2.x, where x matches the python minor version Ansible is running under.

Warning

Using the winrm or psrp connection plugins in Ansible on MacOS in the latest releases typically fail. This is a known problem that occurs deep within the Python stack and cannot be changed by Ansible. The only workaround today is to set the environment variable no_proxy=* and avoid using Kerberos auth.


Authentication Options

When connecting to a Windows host, there are several different options that can be used when authenticating with an account. The authentication type may be set on inventory hosts or groups with the ansible_winrm_transport variable.

The following matrix is a high level overview of the options:

Option

Local Accounts

Active Directory Accounts

Credential Delegation

HTTP Encryption

Basic Yes No No No
Certificate Yes No No No
Kerberos No Yes Yes Yes
NTLM Yes Yes No Yes
CredSSP Yes Yes Yes Yes


Basic

Basic authentication is one of the simplest authentication options to use, but is also the most insecure. This is because the username and password are simply base64 encoded, and if a secure channel is not in use (eg, HTTPS) then it can be decoded by anyone. Basic authentication can only be used for local accounts (not domain accounts).

The following example shows host vars configured for basic authentication:

Basic authentication is not enabled by default on a Windows host but can be enabled by running the following in PowerShell:


Certificate

Certificate authentication uses certificates as keys similar to SSH key pairs, but the file format and key generation process is different.

The following example shows host vars configured for certificate authentication:

Certificate authentication is not enabled by default on a Windows host but can be enabled by running the following in PowerShell:

Encrypted private keys cannot be used as the urllib3 library that is used by Ansible for WinRM does not support this functionality.

Generate a Certificate

A certificate must be generated before it can be mapped to a local user. This can be done using one of the following methods:

Active Directory Certificate Services is beyond of scope in this documentation but may be the best option to use when running in a domain environment. For more information, see the Active Directory Certificate Services documentation.

Using the PowerShell cmdlet New-SelfSignedCertificate to generate a certificate for authentication only works when being generated from a Windows 10 or Windows Server 2012 R2 host or later. OpenSSL is still required to extract the private key from the PFX certificate to a PEM file for Ansible to use.

To generate a certificate with OpenSSL:

To generate a certificate with New-SelfSignedCertificate:

To convert the PFX file to a private key that pywinrm can use, run the following command with OpenSSL openssl pkcs12 -in cert.pfx -nocerts -nodes -out cert_key.pem -passin pass: -passout pass:

Import a Certificate to the Certificate Store

Once a certificate has been generated, the issuing certificate needs to be imported into the Trusted Root Certificate Authorities of the LocalMachine store, and the client certificate public key must be present in the Trusted People folder of the LocalMachine store. For this example, both the issuing certificate and public key are the same.

Following example shows how to import the issuing certificate:

If using ADCS to generate the certificate, then the issuing certificate will already be imported and this step can be skipped.

The code to import the client certificate public key is:

Mapping a Certificate to an Account

Once the certificate has been imported, map it to the local user account:

Once this is complete, the hostvar ansible_winrm_cert_pem should be set to the path of the public key and the ansible_winrm_cert_key_pem variable should be set to the path of the private key.


NTLM

NTLM is an older authentication mechanism used by Microsoft that can support both local and domain accounts. NTLM is enabled by default on the WinRM service, so no setup is required before using it.

NTLM is the easiest authentication protocol to use and is more secure than Basic authentication. If running in a domain environment, Kerberos should be used instead of NTLM.

Kerberos has several advantages over using NTLM:

This example shows host variables configured to use NTLM authentication:


Kerberos

Kerberos is the recommended authentication option to use when running in a domain environment. Kerberos supports features like credential delegation and message encryption over HTTP and is one of the more secure options that is available through WinRM.

Kerberos requires some additional setup work on the Ansible host before it can be used properly.

The following example shows host vars configured for Kerberos authentication:

As of Ansible version 2.3, the Kerberos ticket will be created based on ansible_user and ansible_password. If running on an older version of Ansible or when ansible_winrm_kinit_mode is manual, a Kerberos ticket must already be obtained. See below for more details.

There are some extra host variables that can be set:

Installing the Kerberos Library

Some system dependencies that must be installed prior to using Kerberos. The script below lists the dependencies based on the distro:

Once the dependencies have been installed, the python-kerberos wrapper can be install using pip:

While Ansible has supported Kerberos auth through pywinrm for some time, optional features or more secure options may only be available in newer versions of the pywinrm and/or pykerberos libraries. It is recommended you upgrade each version to the latest available to resolve any warnings or errors. This can be done through tools like pip or a system package manager like dnf, yum, apt but the package names and versions available may differ between tools.

Configuring Host Kerberos

Once the dependencies have been installed, Kerberos needs to be configured so that it can communicate with a domain. This configuration is done through the /etc/krb5.conf file, which is installed with the packages in the script above.

To configure Kerberos, in the section that starts with:

Add the full domain name and the fully qualified domain names of the primary and secondary Active Directory domain controllers. It should look something like this:

In the section that starts with:

Add a line like the following for each domain that Ansible needs access for:

You can configure other settings in this file such as the default domain. See krb5.conf for more details.

Automatic Kerberos Ticket Management

Ansible version 2.3 and later defaults to automatically managing Kerberos tickets when both ansible_user and ansible_password are specified for a host. In this process, a new ticket is created in a temporary credential cache for each host. This is done before each task executes to minimize the chance of ticket expiration. The temporary credential caches are deleted after each task completes and will not interfere with the default credential cache.

To disable automatic ticket management, set ansible_winrm_kinit_mode=manual via the inventory.

Automatic ticket management requires a standard kinit binary on the control host system path. To specify a different location or binary name, set the ansible_winrm_kinit_cmd hostvar to the fully qualified path to a MIT krbv5 kinit-compatible binary.

Manual Kerberos Ticket Management

To manually manage Kerberos tickets, the kinit binary is used. To obtain a new ticket the following command is used:

The domain must match the configured Kerberos realm exactly, and must be in upper case.

To see what tickets (if any) have been acquired, use the following command:

To destroy all the tickets that have been acquired, use the following command:

Troubleshooting Kerberos

Kerberos is reliant on a properly-configured environment to work. To troubleshoot Kerberos issues, ensure that:


CredSSP

CredSSP authentication is a newer authentication protocol that allows credential delegation. This is achieved by encrypting the username and password after authentication has succeeded and sending that to the server using the CredSSP protocol.

Because the username and password are sent to the server to be used for double hop authentication, ensure that the hosts that the Windows host communicates with are not compromised and are trusted.

CredSSP can be used for both local and domain accounts and also supports message encryption over HTTP.

To use CredSSP authentication, the host vars are configured like so:

There are some extra host variables that can be set as shown below:

CredSSP authentication is not enabled by default on a Windows host, but can be enabled by running the following in PowerShell:

Installing CredSSP Library

The requests-credssp wrapper can be installed using pip:

CredSSP and TLS 1.2

By default the requests-credssp library is configured to authenticate over the TLS 1.2 protocol. TLS 1.2 is installed and enabled by default for Windows Server 2012 and Windows 8 and more recent releases.

There are two ways that older hosts can be used with CredSSP:

See TLS 1.2 Support for more information on how to enable TLS 1.2 on the Windows host.

Set CredSSP Certificate

CredSSP works by encrypting the credentials through the TLS protocol and uses a self-signed certificate by default. The CertificateThumbprint option under the WinRM service configuration can be used to specify the thumbprint of another certificate.

This certificate configuration is independent of the WinRM listener certificate. With CredSSP, message transport still occurs over the WinRM listener, but the TLS-encrypted messages inside the channel use the service-level certificate.

To explicitly set the certificate to use for CredSSP:


Non-Administrator Accounts

WinRM is configured by default to only allow connections from accounts in the local Administrators group. This can be changed by running:

This will display an ACL editor, where new users or groups may be added. To run commands over WinRM, users and groups must have at least the Read and Execute permissions enabled.

While non-administrative accounts can be used with WinRM, most typical server administration tasks require some level of administrative access, so the utility is usually limited.


WinRM Encryption

By default WinRM will fail to work when running over an unencrypted channel. The WinRM protocol considers the channel to be encrypted if using TLS over HTTP (HTTPS) or using message level encryption. Using WinRM with TLS is the recommended option as it works with all authentication options, but requires a certificate to be created and used on the WinRM listener.

The ConfigureRemotingForAnsible.ps1 creates a self-signed certificate and creates the listener with that certificate. If in a domain environment, ADCS can also create a certificate for the host that is issued by the domain itself.

If using HTTPS is not an option, then HTTP can be used when the authentication option is NTLM, Kerberos or CredSSP. These protocols will encrypt the WinRM payload with their own encryption method before sending it to the server. The message-level encryption is not used when running over HTTPS because the encryption uses the more secure TLS protocol instead. If both transport and message encryption is required, set ansible_winrm_message_encryption=always in the host vars.

Message encryption over HTTP requires pywinrm>=0.3.0.

A last resort is to disable the encryption requirement on the Windows host. This should only be used for development and debugging purposes, as anything sent from Ansible can be viewed, manipulated and also the remote session can completely be taken over by anyone on the same network. To disable the encryption requirement:

Do not disable the encryption check unless it is absolutely required. Doing so could allow sensitive information like credentials and files to be intercepted by others on the network.


Inventory Options

Ansible's Windows support relies on a few standard variables to indicate the username, password, and connection type of the remote hosts. These variables are most easily set up in the inventory, but can be set on the host_vars/ group_vars level.

When setting up the inventory, the following variables are required:

Using the variables above, Ansible will connect to the Windows host with Basic authentication through HTTPS. If ansible_user has a UPN value like username@MY.DOMAIN.COM then the authentication option will automatically attempt to use Kerberos unless ansible_winrm_transport has been set to something other than kerberos.

The following custom inventory variables are also supported for additional configuration of WinRM connections:

In addition, there are also specific variables that need to be set for each authentication option. See the section on authentication above for more information.

Ansible 2.0 has deprecated the 'ssh' from ansible_ssh_user, ansible_ssh_pass, ansible_ssh_host, and ansible_ssh_port to become ansible_user, ansible_password, ansible_host, and ansible_port. If using a version of Ansible prior to 2.0, the older style (ansible_ssh_*) should be used instead. The shorter variables are ignored, without warning, in older versions of Ansible.

ansible_winrm_message_encryption is different from transport encryption done over TLS. The WinRM payload is still encrypted with TLS when run over HTTPS, even if ansible_winrm_message_encryption=never.


IPv6 Addresses

IPv6 addresses can be used instead of IPv4 addresses or hostnames. This option is normally set in an inventory. Ansible will attempt to parse the address using the ipaddress package and pass to pywinrm correctly.

When defining a host using an IPv6 address, just add the IPv6 address as you would an IPv4 address or hostname:

The ipaddress library is only included by default in Python 3.x. To use IPv6 addresses in Python 2.7, make sure to run pip install ipaddress which installs a backported package.


HTTPS Certificate Validation

As part of the TLS protocol, the certificate is validated to ensure the host matches the subject and the client trusts the issuer of the server certificate. When using a self-signed certificate or setting ansible_winrm_server_cert_validation: ignore these security mechanisms are bypassed. While self signed certificates will always need the ignore flag, certificates that have been issued from a certificate authority can still be validated.

One of the more common ways of setting up a HTTPS listener in a domain environment is to use Active Directory Certificate Service (AD CS). AD CS is used to generate signed certificates from a Certificate Signing Request (CSR). If the WinRM HTTPS listener is using a certificate that has been signed by another authority, like AD CS, then Ansible can be set up to trust that issuer as part of the TLS handshake.

To get Ansible to trust a Certificate Authority (CA) like AD CS, the issuer certificate of the CA can be exported as a PEM encoded certificate. This certificate can then be copied locally to the Ansible controller and used as a source of certificate validation, otherwise known as a CA chain.

The CA chain can contain a single or multiple issuer certificates and each entry is contained on a new line. To then use the custom CA chain as part of the validation process, set ansible_winrm_ca_trust_path to the path of the file. If this variable is not set, the default CA chain is used instead which is located in the install path of the Python package certifi.

Each HTTP call is done by the Python requests library which does not use the systems built-in certificate store as a trust authority. Certificate validation will fail if the server's certificate issuer is only added to the system's truststore.


TLS 1.2 Support

As WinRM runs over the HTTP protocol, using HTTPS means that the TLS protocol is used to encrypt the WinRM messages. TLS will automatically attempt to negotiate the best protocol and cipher suite that is available to both the client and the server. If a match cannot be found then Ansible will error out with a message similar to:

Commonly this is when the Windows host has not been configured to support TLS v1.2 but it could also mean the Ansible controller has an older OpenSSL version installed.

Windows 8 and Windows Server 2012 come with TLS v1.2 installed and enabled by default but older hosts, like Server 2008 R2 and Windows 7, have to be enabled manually.

There is a bug with the TLS 1.2 patch for Server 2008 which will stop Ansible from connecting to the Windows host. This means that Server 2008 cannot be configured to use TLS 1.2. Server 2008 R2 and Windows 7 are not affected by this issue and can use TLS 1.2.

To verify what protocol the Windows host supports, you can run the following command on the Ansible controller:

The output will contain information about the TLS session and the Protocol line will display the version that was negotiated:

If the host is returning TLSv1 then it should be configured so that TLS v1.2 is enable. You can do this by running the following PowerShell script:

The below Ansible tasks can also be used to enable TLS v1.2:

There are other ways to configure the TLS protocols as well as the cipher suites that are offered by the Windows host. One tool that can give you a GUI to manage these settings is IIS Crypto from Nartac Software.


Limitations

Due to the design of the WinRM protocol , there are a few limitations when using WinRM that can cause issues when creating playbooks for Ansible. These include:

Some of these limitations can be mitigated by doing one of the following:


See also

Intro to playbooks

An introduction to playbooks

Tips and tricks

Tips and tricks for playbooks

List of Windows Modules

Windows specific module list, all implemented in PowerShell

User Mailing List

Have a question? Stop by the google group!

irc.freenode.net

#ansible IRC chat channel

Next Previous