Running Ansible in AWS

Published on 25 November 2018

I have another post on how to install ansible here, but if you want to run it inside AWS (maybe for testing Ansible, or maybe for production), you will need to do a few things first. Before starting though, this isn't about managing AWS with Ansible, although that can be done (you can use Ansible to create your instance, security groups, etc.). To do that, you may want to have Ansible running on-premise, and a VPN connection to AWS.

Here, I will assume you have installed Ansible on a Linux AWS AMI as covered in the previous post, and you should be able to spin up a couple of additional Windows VMs to use as clients. The run the following on your Ansible VM to create some folders

    mkdir ansiblestuff
    Mkdir /etc/ansible
    Mkdir /etc/ansible/roles
    Cd /ansiblestuff
    Mkdir group_vars

You then need to create your inventory.yml file, which will contain details for your webserver VMs:


You then need to create a a file in the group_vars folder that will contain the connection information:


Before a ping will work, you may need to set WINRM authentication to basic on your hosts. The easiest way to do this is to do so from a powershell session:

    set-item wsman:\localhost\client\trustedhosts -value '*'

Run a test command:

    invoke-command -computer {get-childitem} -Credential (get-credential)

Put the following commands in a file, winrmcommands.ps1:

    winrm set winrm/config/service/auth '@{Basic="true"}'
    winrm set winrm/config/service '@{AllowUnencrypted="true"}'

    invoke-command -comp -file "winrmcommands.ps1" -cred (get-credential)

Your win-ping should now work:


You can then run the setup module to get some information about the servers you have configured:


After that, you can create an inventory file entry: nano ansible.cfg

In the file, put the following (obviously change the past as appropriate):

    inventory = ~/ansiblestuff/inventory.yml 

After that, you can run the command without inventory, as we do here to run the "raw" module

    ansible webservers -m raw -a "ipconfig"


comments powered by Disqus