Debian Codegraph @

ansible_collections/amazon/aws/docs/docsite/rst/guide_aws.rst

raw
1 .. _ansible_collections.amazon.aws.docsite.aws_intro:
2
3 *************************
4 Amazon Web Services Guide
5 *************************
6
7 The ``amazon.aws`` collection contains a number of modules and plugins for controlling Amazon Web Services (AWS). This guide explains how to use the modules and inventory scripts to automate your AWS resources with Ansible.
8
9 .. contents::
10 :local:
11
12 Requirements for the AWS modules are minimal.
13
14 All of the modules require and are tested against recent versions of botocore and boto3. Starting with the 2.0 AWS collection releases, it is generally the policy of the collections to support the versions of these libraries released 12 months prior to the most recent major collection revision. Individual modules may require a more recent library version to support specific features or may require the boto library, check the module documentation for the minimum required version for each module. You must have the boto3 Python module installed on your control machine. You can install these modules from your OS distribution or using the python package installer: ``pip install boto3``.
15
16 Starting with the 2.0 releases of both collections, Python 2.7 support will be ended in accordance with AWS' `end of Python 2.7 support <https://aws.amazon.com/blogs/developer/announcing-end-of-support-for-python-2-7-in-aws-sdk-for-python-and-aws-cli-v1/>`_ and Python 3.6 or greater will be required.
17
18 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.
19
20 In your playbook steps we'll typically be using the following pattern for provisioning steps::
21
22 - hosts: localhost
23 gather_facts: False
24 tasks:
25 - ...
26
27 .. _ansible_collections.amazon.aws.docsite.aws_authentication:
28
29 Authentication
30 ``````````````
31
32 Authentication with the AWS-related modules is handled by either
33 specifying your access and secret key as ENV variables or module arguments.
34
35 For environment variables::
36
37 export AWS_ACCESS_KEY_ID='AK123'
38 export AWS_SECRET_ACCESS_KEY='abc123'
39
40 For storing these in a vars_file, ideally encrypted with ansible-vault::
41
42 ---
43 aws_access_key: "--REMOVED--"
44 aws_secret_key: "--REMOVED--"
45
46 Note that if you store your credentials in vars_file, you need to refer to them in each AWS-module. For example::
47
48 - amazon.aws.ec2_instance:
49 aws_access_key: "{{ aws_access_key }}"
50 aws_secret_key: "{{ aws_secret_key }}"
51 key_name: "example-ssh-key"
52 image_id: "..."
53
54 Or they can be specified using "module_defaults" at the top of a playbook.::
55
56 # demo_setup.yml
57
58 - hosts: localhost
59 module_defaults:
60 group/aws:
61 aws_access_key: '{{ aws_access_key }}'
62 aws_secret_key: '{{ aws_secret_key }}'
63 region: '{{ region }}'
64 tasks:
65 - amazon.aws.ec2_instance:
66 key_name: "example-ssh-key"
67 image_id: "..."
68
69 Credentials can also be accessed from a `Credentials Profile <https://docs.aws.amazon.com/cli/v1/userguide/cli-configure-files.html>`_.::
70
71 - amazon.aws.ec2_instance:
72 aws_profile: default
73 key_name: "example-ssh-key"
74 image_id: "..."
75
76 .. _ansible_collections.amazon.aws.docsite.aws_provisioning:
77
78 Provisioning
79 ````````````
80
81 The ec2_instance module provisions and de-provisions instances within EC2.
82
83 An example of creating an instance with a public IP assigned follows.
84
85 The "name" parameter will create a "tag:Name" on the instance. Additional tags can be specified with the "tags" parameter.::
86
87 # demo_setup.yml
88
89 - hosts: localhost
90 gather_facts: False
91
92 tasks:
93
94 - name: Provision an EC2 instance with a public IP address
95 amazon.aws.ec2_instance:
96 name: Demo
97 key_name: "example-ssh-key"
98 vpc_subnet_id: subnet-5ca1ab1e
99 instance_type: c5.large
100 security_group: default
101 network:
102 assign_public_ip: true
103 image_id: ami-123456
104 tags:
105 Environment: Testing
106 register: result
107
108 The data about the instance that has been created is being saved by the "register" keyword in the variable named "result".
109
110 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.::
111
112 # demo_setup.yml
113
114 - hosts: localhost
115 gather_facts: False
116
117 tasks:
118
119 - name: Provision an EC2 instance with a public IP address
120 amazon.aws.ec2_instance:
121 name: Demo
122 key_name: "example-ssh-key"
123 vpc_subnet_id: subnet-5ca1ab1e
124 instance_type: c5.large
125 security_group: default
126 network:
127 assign_public_ip: true
128 image_id: ami-123456
129 tags:
130 Environment: Testing
131 register: result
132
133 - name: Add all instance public IPs to host group
134 add_host: hostname={{ item.public_ip }} groups=ec2hosts
135 loop: "{{ result.instances }}"
136
137 With the host group now created, a second play at the bottom of the same provisioning playbook file might now have some configuration steps::
138
139 # demo_setup.yml
140
141 - name: Provision a set of instances
142 hosts: localhost
143 # ... AS ABOVE ...
144
145 - hosts: ec2hosts
146 name: configuration play
147 user: ec2-user
148 gather_facts: true
149
150 tasks:
151
152 - name: Check NTP service
153 service: name=ntpd state=started
154
155 .. _ansible_collections.amazon.aws.docsite.aws_security_groups:
156
157 Security Groups
158 ```````````````
159
160 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.
161 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.::
162
163 - name: fetch raw ip ranges for aws s3
164 set_fact:
165 raw_s3_ranges: "{{ lookup('aws_service_ip_ranges', region='eu-central-1', service='S3', wantlist=True) }}"
166
167 - name: prepare list structure for ec2_group module
168 set_fact:
169 s3_ranges: "{{ s3_ranges | default([]) + [{'proto': 'all', 'cidr_ip': item, 'rule_desc': 'S3 Service IP range'}] }}"
170 loop: "{{ raw_s3_ranges }}"
171
172 - name: set S3 IP ranges to egress rules
173 ec2_group:
174 name: aws_s3_ip_ranges
175 description: allow outgoing traffic to aws S3 service
176 region: eu-central-1
177 state: present
178 vpc_id: vpc-123456
179 purge_rules: true
180 purge_rules_egress: true
181 rules: []
182 rules_egress: "{{ s3_ranges }}"
183 tags:
184 Name: aws_s3_ip_ranges
185
186 .. _ansible_collections.amazon.aws.docsite.aws_host_inventory:
187
188 Host Inventory
189 ``````````````
190
191 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
192 in text files. Rather, the best way to handle this is to use the aws_ec2 inventory plugin. See :ref:`dynamic_inventory`.
193
194 The plugin will also return instances that were created outside of Ansible and allow Ansible to manage them.
195
196 .. _ansible_collections.amazon.aws.docsite.aws_tags_and_groups:
197
198 Tags And Groups And Variables
199 `````````````````````````````
200
201 When using the inventory plugin, you can configure extra inventory structure based on the metadata returned by AWS.
202
203 For instance, you might use ``keyed_groups`` to create groups from instance tags::
204
205 plugin: amazon.aws.aws_ec2
206 keyed_groups:
207 - prefix: tag
208 key: ec2_tags
209
210
211 You can then target all instances with a "class" tag where the value is "webserver" in a play::
212
213 - hosts: tag_class_webserver
214 tasks:
215 - ping
216
217 You can also use these groups with 'group_vars' to set variables that are automatically applied to matching instances.
218
219 .. _ansible_collections.amazon.aws.docsite.aws_pull:
220
221 Autoscaling with Ansible Pull
222 `````````````````````````````
223
224 Amazon Autoscaling features automatically increase or decrease capacity based on load. There are also Ansible modules shown in the cloud documentation that
225 can configure autoscaling policy.
226
227 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.
228
229 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.
230
231 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.
232 For this reason, the autoscaling solution provided below in the next section can be a better approach.
233
234 Read :ref:`ansible-pull` for more information on pull-mode playbooks.
235
236 .. _ansible_collections.amazon.aws.docsite.aws_autoscale:
237
238 Autoscaling with Ansible Automation Platform
239 ````````````````````````````````````````````
240
241 `Ansible Automation Platform (AAP) <https://access.redhat.com/documentation/en-us/red_hat_ansible_automation_platform/>`_
242 also contains a very nice feature for auto-scaling use cases. In this mode, a simple curl script can call
243 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
244 to reconfigure ephemeral nodes. See the install and product documentation for more details.
245
246 A benefit of using the callback in AAP over pull mode is that job results are still centrally recorded and less information has to be shared
247 with remote hosts.
248
249 .. _ansible_collections.amazon.aws.docsite.aws_cloudformation_example:
250
251 Ansible With (And Versus) CloudFormation
252 ````````````````````````````````````````
253
254 CloudFormation is a Amazon technology for defining a cloud stack as a JSON or YAML document.
255
256 Ansible modules provide an easier to use interface than CloudFormation in many examples, without defining a complex JSON/YAML document.
257 This is recommended for most users.
258
259 However, for users that have decided to use CloudFormation, there is an Ansible module that can be used to apply a CloudFormation template
260 to Amazon.
261
262 When using Ansible with CloudFormation, typically Ansible will be used with a tool like Packer to build images, and CloudFormation will launch
263 those images, or ansible will be invoked through user data once the image comes online, or a combination of the two.
264
265 Please see the examples in the Ansible CloudFormation module for more details.
266
267 .. _ansible_collections.amazon.aws.docsite.aws_image_build:
268
269 AWS Image Building With Ansible
270 ```````````````````````````````
271
272 Many users may want to have images boot to a more complete configuration rather than configuring them entirely after instantiation. To do this,
273 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
274 the ec2 module or other Ansible AWS modules such as ec2_asg or the cloudformation module. Possible tools include Packer, aminator, and Ansible's
275 ec2_ami module.
276
277 Generally speaking, we find most users using Packer.
278
279 See the Packer documentation of the `Ansible local Packer provisioner <https://www.packer.io/docs/provisioners/ansible/ansible-local>`_ and `Ansible remote Packer provisioner <https://www.packer.io/docs/provisioners/ansible/ansible>`_.
280
281 If you do not want to adopt Packer at this time, configuring a base-image with Ansible after provisioning (as shown above) is acceptable.
282
283 .. _ansible_collections.amazon.aws.docsite.aws_next_steps:
284
285 Next Steps: Explore Modules
286 ```````````````````````````
287
288 Ansible ships with lots of modules for configuring a wide array of EC2 services. Browse the "Cloud" category of the module
289 documentation for a full list with examples.
290
291 .. seealso::
292
293 :ref:`list_of_collections`
294 Browse existing collections, modules, and plugins
295 :ref:`working_with_playbooks`
296 An introduction to playbooks
297 :ref:`playbooks_delegation`
298 Delegation, useful for working with loud balancers, clouds, and locally executed steps.