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. Then 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:

AWSAnsible1

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

AWSAnsible2

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 on the hosts that you will be connecting to:

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

Run a test command:

    invoke-command -computer 172.27.208.29 {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 172.27.208.24 -file "winrmcommands.ps1" -cred (get-credential)

Your win-ping should now work:

AWSAnsible3

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

AWSAnsible4

After that, you can create an inventory file entry:

    nano ansible.cfg

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

    [defaults]
    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"

AWSAnsible5

comments powered by Disqus