2013-09-30 01:10:28 +02:00
Amazon Web Services Guide
=========================
2013-05-24 17:59:24 +02:00
2013-10-04 19:27:19 +02:00
.. _aws_intro:
2013-05-24 17:59:24 +02:00
Introduction
`` ` ` ` ` ` ` ` ` ``
2014-12-01 17:56:52 +01:00
Ansible contains a number of modules for controlling Amazon Web Services (AWS). The purpose of this
2013-10-03 02:58:10 +02:00
section is to explain how to put Ansible modules together (and use inventory scripts) to use Ansible in AWS context.
2013-05-24 17:59:24 +02:00
2019-11-11 21:09:52 +01:00
Requirements for the AWS modules are minimal.
2013-05-24 17:59:24 +02:00
2014-12-01 17:56:52 +01:00
All of the modules require and are tested against recent versions of boto. You'll need this Python module installed on your control machine. Boto can be installed from your OS distribution or python's "pip install boto".
2013-10-03 02:58:10 +02:00
2015-05-30 03:49:52 +02:00
Whereas classically ansible will execute tasks in its host loop against multiple remote machines, most cloud-control steps occur on your local machine with reference to the regions to control.
2013-10-07 14:29:03 +02:00
2014-12-01 17:56:52 +01:00
In your playbook steps we'll typically be using the following pattern for provisioning steps::
2013-10-07 14:29:03 +02:00
- hosts: localhost
gather_facts: False
2014-12-01 17:56:52 +01:00
tasks:
- ...
.. _aws_authentication:
Authentication
`` ` ` ` ` ` ` ` ` ` ` ``
2019-11-11 21:09:52 +01:00
Authentication with the AWS-related modules is handled by either
2014-12-01 17:56:52 +01:00
specifying your access and secret key as ENV variables or module arguments.
For environment variables::
export AWS_ACCESS_KEY_ID='AK123'
export AWS_SECRET_ACCESS_KEY='abc123'
For storing these in a vars_file, ideally encrypted with ansible-vault::
---
ec2_access_key: "--REMOVED--"
ec2_secret_key: "--REMOVED--"
2013-10-07 14:29:03 +02:00
2017-03-05 10:25:39 +01:00
Note that if you store your credentials in vars_file, you need to refer to them in each AWS-module. For example::
- ec2
aws_access_key: "{{ec2_access_key}}"
aws_secret_key: "{{ec2_secret_key}}"
image: "..."
2013-10-04 19:27:19 +02:00
.. _aws_provisioning:
2013-05-24 17:59:24 +02:00
Provisioning
`` ` ` ` ` ` ` ` ` ``
2019-11-11 21:09:52 +01:00
The ec2 module provisions and de-provisions instances within EC2.
2013-05-24 17:59:24 +02:00
2019-11-11 21:09:52 +01:00
An example of making sure there are only 5 instances tagged 'Demo' in EC2 follows.
2013-05-24 17:59:24 +02:00
2014-12-01 17:56:52 +01:00
In the example below, the "exact_count" of instances is set to 5. This means if there are 0 instances already existing, then
5 new instances would be created. If there were 2 instances, only 3 would be created, and if there were 8 instances, 3 instances would
be terminated.
2013-05-24 17:59:24 +02:00
2014-12-01 17:56:52 +01:00
What is being counted is specified by the "count_tag" parameter. The parameter "instance_tags" is used to apply tags to the newly created
2014-12-11 04:04:02 +01:00
instance.::
2013-05-24 17:59:24 +02:00
2014-12-09 14:28:57 +01:00
# demo_setup.yml
2014-12-01 17:56:52 +01:00
- hosts: localhost
gather_facts: False
2013-05-24 17:59:24 +02:00
2014-12-01 17:56:52 +01:00
tasks:
2013-05-24 17:59:24 +02:00
2014-12-01 17:56:52 +01:00
- name: Provision a set of instances
2019-11-11 21:09:52 +01:00
ec2:
2014-12-01 17:56:52 +01:00
key_name: my_key
group: test
instance_type: t2.micro
image: "{{ ami_id }}"
2019-11-11 21:09:52 +01:00
wait: true
2014-12-01 17:56:52 +01:00
exact_count: 5
count_tag:
Name: Demo
instance_tags:
Name: Demo
register: ec2
2013-05-24 17:59:24 +02:00
2014-12-01 17:56:52 +01:00
The data about what instances are created is being saved by the "register" keyword in the variable named "ec2".
2013-05-24 17:59:24 +02:00
2014-12-11 04:04:02 +01:00
From this, we'll use the add_host module to dynamically create a host group consisting of these new instances. This facilitates performing configuration actions on the hosts immediately in a subsequent task.::
2013-05-24 17:59:24 +02:00
2014-12-01 17:56:52 +01:00
# demo_setup.yml
- hosts: localhost
gather_facts: False
2013-05-24 17:59:24 +02:00
tasks:
2014-12-01 17:56:52 +01:00
- name: Provision a set of instances
2019-11-11 21:09:52 +01:00
ec2:
2014-12-01 17:56:52 +01:00
key_name: my_key
group: test
instance_type: t2.micro
image: "{{ ami_id }}"
2019-11-11 21:09:52 +01:00
wait: true
2014-12-01 17:56:52 +01:00
exact_count: 5
count_tag:
Name: Demo
instance_tags:
Name: Demo
2015-01-18 14:27:26 +01:00
register: ec2
2019-11-11 21:09:52 +01:00
2014-12-01 17:56:52 +01:00
- name: Add all instance public IPs to host group
2015-03-21 06:21:55 +01:00
add_host: hostname={{ item.public_ip }} groups=ec2hosts
move from with_<lookup>: to loop:
- old functionality is still available direct lookup use, the following are equivalent
with_nested: [[1,2,3], ['a','b','c']]
loop: "{{lookup('nested', [1,2,3], ['a','b','c'])}}"
- avoid squashing with 'loop:'
- fixed test to use new intenal attributes
- removed most of 'lookup docs' as these now reside in the plugins
2017-09-17 05:32:34 +02:00
loop: "{{ ec2.instances }}"
2013-10-03 02:58:10 +02:00
2017-08-19 23:00:51 +02:00
With the host group now created, a second play at the bottom of the same provisioning playbook file might now have some configuration steps::
2013-05-24 17:59:24 +02:00
2014-12-01 17:56:52 +01:00
# demo_setup.yml
2013-10-04 19:27:19 +02:00
2014-12-01 17:56:52 +01:00
- name: Provision a set of instances
hosts: localhost
# ... AS ABOVE ...
- hosts: ec2hosts
name: configuration play
user: ec2-user
gather_facts: true
tasks:
- name: Check NTP service
service: name=ntpd state=started
2013-05-24 17:59:24 +02:00
2019-04-26 16:31:49 +02:00
.. _aws_security_groups:
Security Groups
`` ` ` ` ` ` ` ` ` ` ` ` ``
Security groups on AWS are stateful. The response of a request from your instance is allowed to flow in regardless of inbound security group rules and vice-versa.
In case you only want allow traffic with AWS S3 service, you need to fetch the current IP ranges of AWS S3 for one region and apply them as an egress rule.::
- name: fetch raw ip ranges for aws s3
set_fact:
raw_s3_ranges: "{{ lookup('aws_service_ip_ranges', region='eu-central-1', service='S3', wantlist=True) }}"
- name: prepare list structure for ec2_group module
set_fact:
s3_ranges: "{{ s3_ranges | default([]) + [{'proto': 'all', 'cidr_ip': item, 'rule_desc': 'S3 Service IP range'}] }}"
2019-11-11 21:09:52 +01:00
loop: "{{ raw_s3_ranges }}"
2019-04-26 16:31:49 +02:00
- name: set S3 IP ranges to egress rules
ec2_group:
name: aws_s3_ip_ranges
description: allow outgoing traffic to aws S3 service
region: eu-central-1
state: present
vpc_id: vpc-123456
purge_rules: true
purge_rules_egress: true
rules: []
rules_egress: "{{ s3_ranges }}"
tags:
Name: aws_s3_ip_ranges
2013-11-27 21:36:49 +01:00
.. _aws_host_inventory:
2013-10-04 19:27:19 +02:00
2013-05-24 17:59:24 +02:00
Host Inventory
2014-12-01 17:56:52 +01:00
`` ` ` ` ` ` ` ` ` ` ` ``
2013-05-24 17:59:24 +02:00
2014-12-01 17:56:52 +01:00
Once your nodes are spun up, you'll probably want to talk to them again. With a cloud setup, it's best to not maintain a static list of cloud hostnames
2019-11-11 21:09:52 +01:00
in text files. Rather, the best way to handle this is to use the ec2 dynamic inventory script. See :ref: `dynamic_inventory` .
2013-05-24 17:59:24 +02:00
2014-12-01 17:56:52 +01:00
This will also dynamically select nodes that were even created outside of Ansible, and allow Ansible to manage them.
2013-05-24 17:59:24 +02:00
2018-03-14 20:44:21 +01:00
See :ref: `dynamic_inventory` for how to use this, then return to this chapter.
2013-05-24 17:59:24 +02:00
2014-12-01 17:56:52 +01:00
.. _aws_tags_and_groups:
2013-05-24 17:59:24 +02:00
2014-12-01 17:56:52 +01:00
Tags And Groups And Variables
`` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ``
2013-12-27 21:19:19 +01:00
2014-12-01 17:56:52 +01:00
When using the ec2 inventory script, hosts automatically appear in groups based on how they are tagged in EC2.
2013-12-27 21:19:19 +01:00
For instance, if a host is given the "class" tag with the value of "webserver",
it will be automatically discoverable via a dynamic group like so::
- hosts: tag_class_webserver
tasks:
- ping
2014-12-04 23:23:35 +01:00
Using this philosophy can be a great way to keep systems separated by the function they perform.
2014-12-01 17:56:52 +01:00
In this example, if we wanted to define variables that are automatically applied to each machine tagged with the 'class' of 'webserver', 'group_vars'
2015-05-05 22:50:46 +02:00
in ansible can be used. See :ref: `splitting_out_vars` .
2014-12-01 17:56:52 +01:00
Similar groups are available for regions and other classifications, and can be similarly assigned variables using the same mechanism.
2013-12-27 21:19:19 +01:00
2013-11-27 21:36:49 +01:00
.. _aws_pull:
2013-10-04 19:27:19 +02:00
2014-12-01 17:56:52 +01:00
Autoscaling with Ansible Pull
`` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ``
2013-05-24 17:59:24 +02:00
2015-08-13 18:00:55 +02:00
Amazon Autoscaling features automatically increase or decrease capacity based on load. There are also Ansible modules shown in the cloud documentation that
2014-12-01 17:56:52 +01:00
can configure autoscaling policy.
2013-05-24 17:59:24 +02:00
2019-11-11 21:09:52 +01:00
When nodes come online, it may not be sufficient to wait for the next cycle of an ansible command to come along and configure that node.
2014-12-01 17:56:52 +01:00
2019-11-11 21:09:52 +01:00
To do this, pre-bake machine images which contain the necessary ansible-pull invocation. Ansible-pull is a command line tool that fetches a playbook from a git server and runs it locally.
2013-05-24 17:59:24 +02:00
2014-12-01 17:56:52 +01:00
One of the challenges of this approach is that there needs to be a centralized way to store data about the results of pull commands in an autoscaling context.
For this reason, the autoscaling solution provided below in the next section can be a better approach.
Read :ref: `ansible-pull` for more information on pull-mode playbooks.
2013-05-29 16:25:54 +02:00
2013-11-27 21:36:49 +01:00
.. _aws_autoscale:
2013-10-04 19:27:19 +02:00
2014-01-28 17:04:34 +01:00
Autoscaling with Ansible Tower
2014-12-01 17:56:52 +01:00
`` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ``
2013-10-03 02:58:10 +02:00
2018-03-14 20:44:21 +01:00
:ref: `ansible_tower` also contains a very nice feature for auto-scaling use cases. In this mode, a simple curl script can call
2013-10-03 02:58:10 +02:00
a defined URL and the server will "dial out" to the requester and configure an instance that is spinning up. This can be a great way
2014-12-01 17:56:52 +01:00
to reconfigure ephemeral nodes. See the Tower install and product documentation for more details.
2013-10-03 02:58:10 +02:00
2014-01-28 17:04:34 +01:00
A benefit of using the callback in Tower over pull mode is that job results are still centrally recorded and less information has to be shared
2013-10-03 02:58:10 +02:00
with remote hosts.
2013-11-27 21:36:49 +01:00
.. _aws_cloudformation_example:
2013-10-04 19:27:19 +02:00
2014-12-01 17:56:52 +01:00
Ansible With (And Versus) CloudFormation
`` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ``
2013-05-24 17:59:24 +02:00
2019-11-11 21:09:52 +01:00
CloudFormation is a Amazon technology for defining a cloud stack as a JSON or YAML document.
2013-05-29 16:25:54 +02:00
2018-10-01 18:23:56 +02:00
Ansible modules provide an easier to use interface than CloudFormation in many examples, without defining a complex JSON/YAML document.
2014-12-01 17:56:52 +01:00
This is recommended for most users.
2013-05-29 16:25:54 +02:00
2014-12-01 17:56:52 +01:00
However, for users that have decided to use CloudFormation, there is an Ansible module that can be used to apply a CloudFormation template
to Amazon.
2013-05-29 16:25:54 +02:00
2014-12-01 17:56:52 +01:00
When using Ansible with CloudFormation, typically Ansible will be used with a tool like Packer to build images, and CloudFormation will launch
those images, or ansible will be invoked through user data once the image comes online, or a combination of the two.
2013-05-29 16:25:54 +02:00
2014-12-01 17:56:52 +01:00
Please see the examples in the Ansible CloudFormation module for more details.
2013-12-19 03:05:10 +01:00
2014-12-01 17:56:52 +01:00
.. _aws_image_build:
2013-12-19 03:05:10 +01:00
2014-12-01 17:56:52 +01:00
AWS Image Building With Ansible
`` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ``
2013-12-19 03:05:10 +01:00
2014-12-01 17:56:52 +01:00
Many users may want to have images boot to a more complete configuration rather than configuring them entirely after instantiation. To do this,
2015-05-30 03:49:52 +02:00
one of many programs can be used with Ansible playbooks to define and upload a base image, which will then get its own AMI ID for usage with
2014-12-01 17:56:52 +01:00
the ec2 module or other Ansible AWS modules such as ec2_asg or the cloudformation module. Possible tools include Packer, aminator, and Ansible's
2019-11-11 21:09:52 +01:00
ec2_ami module.
2013-12-19 03:05:10 +01:00
2014-12-01 17:56:52 +01:00
Generally speaking, we find most users using Packer.
2013-12-19 03:05:10 +01:00
2016-04-08 16:55:23 +02:00
See the Packer documentation of the `Ansible local Packer provisioner <https://www.packer.io/docs/provisioners/ansible-local.html> `_ and `Ansible remote Packer provisioner <https://www.packer.io/docs/provisioners/ansible.html> `_ .
2013-10-03 02:58:10 +02:00
2014-12-01 17:56:52 +01:00
If you do not want to adopt Packer at this time, configuring a base-image with Ansible after provisioning (as shown above) is acceptable.
2013-10-04 19:27:19 +02:00
2014-12-16 21:25:41 +01:00
.. _aws_next_steps:
2013-05-29 16:25:54 +02:00
2014-12-01 17:56:52 +01:00
Next Steps: Explore Modules
`` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ``
2013-05-29 16:25:54 +02:00
2014-12-01 17:56:52 +01:00
Ansible ships with lots of modules for configuring a wide array of EC2 services. Browse the "Cloud" category of the module
documentation for a full list with examples.
2013-05-24 17:59:24 +02:00
2013-10-05 18:31:16 +02:00
.. seealso ::
2018-03-14 20:44:21 +01:00
:ref: `all_modules`
2013-10-05 18:31:16 +02:00
All the documentation for Ansible modules
2018-03-14 20:44:21 +01:00
:ref: `working_with_playbooks`
2013-10-05 18:31:16 +02:00
An introduction to playbooks
2018-03-14 20:44:21 +01:00
:ref: `playbooks_delegation`
2013-10-05 18:31:16 +02:00
Delegation, useful for working with loud balancers, clouds, and locally executed steps.
2018-07-21 15:48:47 +02:00
`User Mailing List <https://groups.google.com/group/ansible-devel> `_
2013-10-05 18:31:16 +02:00
Have a question? Stop by the google group!
`irc.freenode.net <http://irc.freenode.net> `_
#ansible IRC chat channel