Using Ansible to Run Powershell Scripts

Share on:

In the midst of playing with Ansible and figured my most common use case would be executing powershell scripts on remote servers, so scavenged around and put this together.


Preparing to Execute the Script

This script assumes you have already installed and configured Ansible, downloaded the Git Repo and installed the WinRM python module.

Define your group variables. Create a hostgroup.yml file in this folder. This is where you specify the username, password and WinRM port to use. This file should mimit the host group defined in your hosts file.

1ansible_ssh_user: admin
2ansible_ssh_pass: password
3ansible_ssh_port: 5985
4ansible_connection: winrm
5ansible_winrm_server_cert_validation: ignore

Here is the yaml code responsible for launching the script. Copy the script into the scripts directory and update the file.

1- name: Run Powershell Scripts
2  hosts: test
3  tasks:
4    - name: run a powershell script
5      script: scripts/hello.ps1
6      register: out
7    - debug: var=out

Execute the Script

  • Run ansible-playbook powershell.yml -i hosts
comments powered by Disqus

See Also