For those learning Ansible this is a quick and simple document on how to create AWS instances, create and attach AWS volumes, and create an ami using Ansible from CentOS 7 Linux.

As a quick first step make sure the following are install on CentOS 7 Linux:

yum install python2-pip
pip install awscli
pip install boto
pip install boto3
pip install bs4

If upgrading Ansible to the new 2.8 version simply execute:

pip install ansible --upgrade

The following Ansible Playbook will create an AWS instance using the free Amazon Linux ami in the us-east-1 region which is ami-0d19227302e8e4bb5. If using a different region make sure you use an ami that is established for that region. To read about which ami’s to use for which region go to here: https://aws.amazon.com/amazon-linux-ami/2018.03-release-notes/.

Use your own AWS access keys, subnet, and security group in the following Playbook which will create an AWS instance. You can execute it with the following command:

ansible-playbook create-instance.yml

create-instance.yml:

---
- hosts: localhost # put localhost. We are processing against aws
connection: local # put local. We are processing against aws
gather_facts: False # don't gather facts against localhost
vars:
aws_access_key: 'put your aws access key here'
aws_secret_key: 'put your aws secret key here'
instance_name: 'NameYourServer' key_name: 'YourPrivatePemKeyFile' instance_type: 't2.micro' # keep it free
image: 'ami-0d19227302e8e4bb5' # free ami from Amazon us-east-1 region
vpc_subnet_id: 'subnet-yourSubnet'
group_id: 'sg-yourSecurityGroup'
zone: 'us-east-1a'
region: 'us-east-1'
tasks:

- name: create an aws instance
ec2:
aws_access_key: "{{ aws_access_key }}"
aws_secret_key: "{{ aws_secret_key }}"
key_name: "{{ key_name }}"
instance_type: "{{ instance_type }}"
image: "{{ image }}"
wait: true
count: 1
instance_tags:
Name: "{{ instance_name }}"
BuildStatus: 'Provisioning'
monitoring: no
vpc_subnet_id: "{{ vpc_subnet_id }}"
group_id: "{{ group_id }}"
zone: "{{ zone }}"
region: "{{ region }}"
assign_public_ip: yes
register: ec2_instance
- name: get volume facts
ec2_vol_facts:
aws_access_key: "{{ aws_access_key }}"
aws_secret_key: "{{ aws_secret_key }}"
region: "{{ region }}"
filters:
attachment.instance-id: "{{ ec2_instance.instance_ids }}"
register: volume_facts
- name: create volume tags for instance
ec2_tag:
aws_access_key: "{{ aws_access_key }}"
aws_secret_key: "{{ aws_secret_key }}"
region: "{{ region }}"
resource: "{{ item.id }}"
state: present
tags:
Name: "{{ instance_name }}"
with_items: '{{ volume_facts.volumes }}'
- name: wait for SSH to come up.....
wait_for:
host: "{{ item.public_ip }}"
port: 22
delay: 10
timeout: 600
state: started
with_items: '{{ ec2_instance.instances }}'
loop_control:
label: "{{ item.id }} - {{ item.public_ip }}"

- name: change BuildStatus tag to Completed
ec2_tag:
aws_access_key: "{{ aws_access_key }}"
aws_secret_key: "{{ aws_secret_key }}"
region: "{{ region }}"
resource: "{{ item.id }}"
state: present
tags:
BuildStatus: 'Completed'
with_items: '{{ ec2_instance.instances }}'

- name: capture instance facts
ec2_instance_facts:
aws_access_key: "{{ aws_access_key }}"
aws_secret_key: "{{ aws_secret_key }}"
region: "{{ region }}"
instance_ids: "{{ ec2_instance.instance_ids }}"
register: instance_facts
- name: print out instance facts
debug:
msg: "{{ instance_facts }}"

To access the newly built AWS instance execute the following:

ssh -i “YourPrivatePemKeyFile.pem” ec2-user@ec2-your-public-ip-address.compute-1.amazonaws.com

The next Ansible Playbook will create, attach, and tag a new volume to an existing AWS instance.

create-and-attach-aws-volume.yml:

---
- hosts: localhost # put localhost. We are processing against aws
connection: local # put local. We are processing against aws
gather_facts: False # don't gather facts against localhost

vars:
aws_access_key: 'put your aws access key here'
aws_secret_key: 'put your aws secret key here'

instance_name: 'YourInstanceName' # needed to tag the volume name
instance_id: 'i-PutYourInstanceIdHere' volume_size: '8'
volume_type: 'gp2'
device_name: '/dev/xvdb'
zone: 'us-east-1a'
region: 'us-east-1'
tasks: - name: create a new volume
ec2_vol:
aws_access_key: "{{ aws_access_key }}"
aws_secret_key: "{{ aws_secret_key }}"
volume_size: "{{ volume_size }}"
volume_type: "{{ volume_type }}"
region: "{{ region }}"
zone: "{{ zone }}"
register: new_volume
- name: attach created volume
ec2_vol:
aws_access_key: "{{ aws_access_key }}"
aws_secret_key: "{{ aws_secret_key }}"
instance: "{{ instance_id }}"
id: "{{ new_volume.volume_id }}"
device_name: "{{ device_name }}"
delete_on_termination: true
region: "{{ region }}"
zone: "{{ zone }}"
register: attach_volume
- name: create volume tag
ec2_tag:
aws_access_key: "{{ aws_access_key }}"
aws_secret_key: "{{ aws_secret_key }}"
region: "{{ region }}"
resource: "{{ attach_volume.volume_id }}"
state: present
tags:
Name: "{{ instance_name }}"
- name: capture volume facts
ec2_vol_facts:
aws_access_key: "{{ aws_access_key }}"
aws_secret_key: "{{ aws_secret_key }}"
region: "{{ region }}"
filters:
volume-id: "{{ attach_volume.volume_id }}"
register: volume_facts
- name: degug # print out volume facts
debug:
msg: "{{ volume_facts }}"

The next Ansible Playbook creates an ami from an existing running AWS instance.

---
- hosts: localhost # put localhost. We are processing against aws
connection: local # put local. We are processing against aws
gather_facts: False # don't gather facts against localhost
vars:

aws_access_key: 'put your aws access key here'
aws_secret_key: 'put your aws secret key here'
instance_id: 'i-PutYourInstanceIdHere'
ami_name: 'NameYourAMI'
tasks: - name: create ami
ec2_ami:
aws_access_key: "{{ aws_access_key }}"
aws_secret_key: "{{ aws_secret_key }}"
instance_id: "{{ instance_id }}"
name: "{{ ami_name }}"
no_reboot: yes
delete_snapshot: yes
wait: yes
tags:
Name: "{{ ami_name }}"
device_mapping:
- device_name: '/dev/xvda'
volume_type: 'gp2'
delete_on_termination: true
register: ami_result
- name: degug
debug:
msg: "{{ ami_result }}"

To create a new instance from the ami use the create-instance.yml from above and plug in the needed variable information.

As you can see working with AWS and Ansible is easy and great fun.

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