Ansible Setup on AWS EC2 Instance with windows Nodes

Please visit my website and subscribe to my youtube channel for more articles

For Ansible to work,python and SSH(in case of linux) should be configured on all the servers




On AWS EC2 Linux Free Tier Instance, python and ssh both are already installed

Python Version — 2.7.13

Three servers

How to connect between these servers ?

To ping these servers(webserver and dbserver) from ansible control server , you have to add one inbound rule “All ICAMP traffic” in both the instances)

Step 1

Ansible Control Server

Install Ansible

sudo pip install markupsafe

sudo pip install xmltodict

sudo pip install pywinrm

if getting sudo pip command not found
sudo env "PATH=$PATH" pip install pytz

sudo pip install ansible

Image for post
Image for post
Ansible Version

Ansible installed successfully.

Step 2:

Create a Windows EC2 Instance

Check ping command from Ansible control server

Next make sure the Amazon network rules allow Echo Requests. Go to the Security Group for the EC2.

  • right click, select inbound rules
  • A: select Add Rule
  • B: Select Custom ICMP Rule — IPv4
  • C: Select Echo Request
  • D: Select either Anywhere or My IP
  • E: Select Save
  • Added All traffic also

If ping doesn’t work, do the below step also

  • Next, Windows firewall blocks inbound Echo requests by default. Allow Echo requests by creating a windows firewall exception…
  • Go to Start and type Windows Firewall with Advanced Security
  • Select inbound rules

Prerequisite on Windows Server

  1. Powershell 3.0 or higher should be installed. (Version 5 is present on AWS EC2 Windows instance). Check version using below command

2. WinRM setup on windows machine

examples/scripts/ConfigureRemotingForAnsible.ps1 script on the remote machine and then execute it in PowerShell console as an administrator.

powershell.exe -File ConfigureRemotingForAnsible.ps1powershell.exe -File ConfigureRemotingForAnsible.ps1

Ansible Control Server

create folder named windowsplaybook using below command

mkdir windowsplaybook

cd windowsplaybook/

create file named inventory by using command

vi inventory

put in below content


Ip address of machine

Create File Named all in group_vars folder

Create folder named group_vars using mkdir group_vars

Create files named all using vi group_vars/all and put below contents

ansible_user: windows_username

ansible_password: SecretPasswordGoesHere

ansible_port: 5986

ansible_connection: winrm

# The following is necessary for Python 2.7.9+ (or any older Python that has backported SSLContext, eg, Python 2.7.5 on RHEL7) when using default WinRM self-signed certificates:

ansible_winrm_server_cert_validation: ignore

after this done please run below command to test if you are able to ping windows machine

ansible web -i inventory -m win_ping
ansible web -i
-i /home/ec2-user/windowsplaybook/inventory -m win_feature

Ansible control server connection with windows nodes is completed.

Now run some playbooks

Create a directory with file main.yml

mkdir /home/ec2-user/windowsplaybook/roles/basic/tasks

vi main.yml

ansible-playbook -i /home/ec2-user/windowsplaybook/inventory main.yml

changed: []

TASK [Install IIS] *****************************************************************************************************************************************************
ok: []

PLAY RECAP ************************************************************************************************************************************************************* : ok=4 changed=1 unreachable=0 failed=0

[root@ip-10–0–0–20 tasks]# cat main.yml
— -
# YAML documents begin with the document separator — -

# The minus in YAML this indicates a list item. The playbook contains a list
# of plays, with each play being a dictionary

# Target: where our play will run and options it will run with
hosts: all

# Task: the list of tasks that will be executed within the play, this section
# can also be used for pre and post tasks
— name: Set a fact
our_fact: Ansible Rocks!

- name: Install IIs WebServer
name: Web-Server
state: present
— name: Install IIS
name: Web-Mgmt-Tools,
state: present
include_sub_features: no

# Three dots indicate the end of a YAML document

Some more examples

Devops Automation Enginneer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store