Creating your first machine

Now that Ansible is able to connect to your AWS environment, you can proceed with the actual playbook by following these steps:

  1. Create the aws.yaml Playbook with the following content:
---
- hosts: localhost
tasks:
- name: Ensure key pair is present
ec2_key:
name: fale
key_material: "{{ lookup('file', '~/.ssh/fale.pub') }}"
- name: Gather information of the EC2 VPC net in eu-west-1
ec2_vpc_net_facts:
region: eu-west-1
register: aws_simple_net
- name: Gather information of the EC2 VPC subnet in eu-west-1
ec2_vpc_subnet_facts:
region: eu-west-1
filters:
vpc-id: '{{ aws_simple_net.vpcs.0.id }}'
register: aws_simple_subnet
- name: Ensure wssg Security Group is present
ec2_group:
name: wssg
description: Web Security Group
region: eu-west-1
vpc_id: '{{ aws_simple_net.vpcs.0.id }}'
rules:
- proto: tcp
from_port: 22
to_port: 22
cidr_ip: 0.0.0.0/0
- proto: tcp
from_port: 80
to_port: 80
cidr_ip: 0.0.0.0/0
- proto: tcp
from_port: 443
to_port: 443
cidr_ip: 0.0.0.0/0
rules_egress:
- proto: all
cidr_ip: 0.0.0.0/0
register: aws_simple_wssg
- name: Setup instance
ec2:
assign_public_ip: true
image: ami-3548444c
region: eu-west-1
exact_count: 1
key_name: fale
count_tag:
Name: ws01.ansible2cookbook.com
instance_tags:
Name: ws01.ansible2cookbook.coms
instance_type: t2.micro
group_id: '{{ aws_simple_wssg.group_id }}'
vpc_subnet_id: '{{ aws_simple_subnet.subnets.0.id }}'
volumes:
- device_name: /dev/sda1
volume_type: gp2
volume_size: 10
delete_on_termination: True
  1. Run it using the following command:
$ ansible-playbook aws.yaml

This command will return something like the following:

PLAY [localhost] **********************************************************************************

TASK [Gathering Facts] ****************************************************************************
ok: [localhost]

TASK [Ensure key pair is present] *****************************************************************
ok: [localhost]

TASK [Gather information of the EC2 VPC net in eu-west-1] *****************************************
ok: [localhost]

TASK [Gather information of the EC2 VPC subnet in eu-west-1] **************************************
ok: [localhost]

TASK [Ensure wssg Security Group is present] ******************************************************
ok: [localhost]

TASK [Setup instance] *****************************************************************************
changed: [localhost]

PLAY RECAP ****************************************************************************************
localhost : ok=6 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

If you check AWS Console, you will see that you now have one machine up and running!

To launch a virtual machine in AWS, we need a few things to be in place, as follows:

  • An SSH key pair
  • A network
  • A subnetwork
  • A security group

By default, a network and a subnetwork are already available in your accounts, but you need to retrieve their IDs.

That's why we started by uploading the public part of an SSH keypair to AWS, then queried for information about the network and the subnetwork, then ensured that the Security Group we wanted to use was present, and lastly triggered the machine build.

Now you have learned how to automate against Amazon Web Services, you'll learn how to complement Google Cloud Platform with automation.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset