Skip to content

Ansible is a radically simple IT automation platform that makes your applications and systems easier to deploy and maintain. Automate everything from code deployment to network configuration to cloud management, in a language that approaches plain English, using SSH, with no agents to install on remote systems. https://docs.ansible.com.

Notifications You must be signed in to change notification settings

oneclick-ag/ansible

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 

Repository files navigation

ConfigureRemotingForAnsible.ps1

A PowerShell script to configure a Windows host for remote management via Ansible over WinRM using HTTPS and CredSSP authentication.


⚙️ What It Does

  • Verifies:
    • Administrator privileges
    • PowerShell version ≥ 5.1
    • WinRM service status and port availability
  • Enables and configures the WinRM service and PowerShell Remoting
  • Generates or reuses a self-signed certificate for HTTPS
  • Sets up a WinRM HTTPS listener with the correct certificate
  • Optionally removes HTTP WinRM listeners
  • Adds a Windows Firewall rule to allow HTTPS traffic (port 5986)
  • Configures WSMan authentication options (enable/disable Basic, Kerberos, Negotiate, etc.)
  • Sets LocalAccountTokenFilterPolicy to support local user access
  • Temporarily enables CredSSP delegation via registry policy, tests it, and reverts changes
  • Logs to:
    • Console with timestamps
    • A log file under %TEMP%
    • Windows Event Log (Application under source oneclick.prepareWinRM)

🚀 Quick Start

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$url  = "https://raw.githubusercontent.com/oneclick-ag/ansible/oc_dev/examples/scripts/ConfigureRemotingForAnsible.ps1"
$file = "$env:TEMP\ConfigureRemotingForAnsible.ps1"
Invoke-WebRequest -Uri $url -OutFile $file -UseBasicParsing
powershell.exe -ExecutionPolicy Bypass -File $file

📌 Parameters

Parameter Description Default
SubjectName Subject (CN) name for the certificate $env:COMPUTERNAME
CertValidityDays Certificate validity duration (in days) 3650 (10 years)
WinRMPort HTTPS port for WinRM 5986
DisableBasicAuth Disable Basic authentication $false
DisableKerberosAuth Disable Kerberos authentication $false
DisableNegotiateAuth Disable Negotiate authentication $false
DisableCertificateAuth Disable Certificate authentication $false
DisableAllowUnencrypted Disable unencrypted communication $false
DisableCompatibilityHttpListener Disable legacy HTTP listener $false
DisableCompatibilityHttpsListener Disable legacy HTTPS listener $false
RemoveListenersThatRunOverHTTP Remove all HTTP-based WinRM listeners $false
TryRelaunchWithElevation Relaunch script with elevation if not already elevated $false
LogPathCMD Custom log path for secondary log output %TEMP%\*.log

Example:

.\ConfigureRemotingForAnsible.ps1 -DisableBasicAuth -RemoveListenersThatRunOverHTTP

🖥️ Requirements

  • PowerShell version 5.1 or later
  • Administrator privileges
  • Internet access (for downloading the script if needed)

📄 Logging

  • Console output with timestamps
  • File: %TEMP%\oneclick_prepare_winrm_script_logs_<timestamp>.txt
  • Windows Event Log (Application → Source: oneclick.prepareWinRM)

🔐 Ansible Configuration Example

After running the script on a Windows host, use the following configuration in your inventory.yml or host_vars:

ansible_user: <username>
ansible_password: <password>
ansible_connection: winrm
ansible_port: 5986
ansible_winrm_transport: credssp
ansible_winrm_server_cert_validation: ignore

ℹ️ Notes

  • CredSSP delegation policy is only temporarily enabled for testing and automatically cleaned up at the end.
  • The script is idempotent and can be safely re-run.



start.cmd

This batch script (start.cmd) is a Windows command-line launcher that runs ConfigureRemotingForAnsible.ps1 with elevation and logging. It is designed to provide a simple, user-friendly way to initiate Ansible WinRM configuration on a Windows host.


🚀 What It Does

  1. Verifies the PowerShell script exists
  2. Ensures the %TEMP% directory is present
  3. Executes the PowerShell script with:
    • -ExecutionPolicy Bypass
    • -TryRelaunchWithElevation flag
    • Custom log path for fallback logging
  4. Captures all standard output and errors into a log file
  5. Detects errors by scanning logs for "ERROR:"
  6. Waits (up to ~30s) for fallback logs to be written, if needed
  7. Extracts error message (if any) and shows it in a message box
  8. On success, displays a success message box
  9. Cleans up all temporary logs

📄 Generated Files

File Description
%TEMP%\oneclick_prepare_winrm_setup.log Main output log from the PowerShell script
%TEMP%\oneclick_prepare_winrm_script_internal.log Fallback internal log for error messages
%TEMP%\oneclick_prepare_winrm_error.log Message shown in the MessageBox on error

🧠 Error Handling

If the script fails (ExitCode ≠ 0):

  • The script looks for ERROR: in the main log file
  • If no errors are found, it falls back to oneclick_prepare_winrm_script_internal.log
  • A message box will display a summary of the error
  • If logs are missing, a generic error message is shown

🪟 MessageBox Integration

The script uses PowerShell to show a GUI MessageBox:

  • Error: If the configuration fails, error details are displayed
  • Information: If the configuration succeeds, a confirmation is shown

✅ Requirements

  • Windows with PowerShell 5.1+
  • ConfigureRemotingForAnsible.ps1 must be in the same directory
  • User must be able to elevate privileges (UAC prompt)

💡 Usage

Double-click start.cmd or run from an elevated command prompt:

start.cmd

This will silently run the configuration and display the result when complete.

About

Ansible is a radically simple IT automation platform that makes your applications and systems easier to deploy and maintain. Automate everything from code deployment to network configuration to cloud management, in a language that approaches plain English, using SSH, with no agents to install on remote systems. https://docs.ansible.com.

Resources

Stars

Watchers

Forks

Releases

No releases published