oliverdavies.uk/source/_daily_emails/2022-09-03.md

58 lines
2.2 KiB
Markdown
Raw Permalink Normal View History

2024-01-03 20:00:00 +00:00
---
pubDate: 2022-09-03
title: Creating infrastructure with Ansible
permalink: dailys/2022/09/03/creating-infrastructure-with-ansible
2024-01-03 20:00:00 +00:00
tags: ["ansible"]
---
Let's start at the beginning.
If we want to automate our infrastructure then we first need to create it. This could be done manually or we can automate it.
Popular tools for this include Terraform and Pulumi, but Ansible also includes modules to interface with hosting providers such as Amazon Web Services, Microsoft Azure, DigitalOcean, and Linode.
By using one of these tools, you can programatically provision a new, blank server that is ready for you to be configered.
For example, to [create a DigitalOcean droplet](https://docs.ansible.com/ansible/latest/collections/community/digitalocean/digital_ocean_module.htm):
```language-yaml
2024-01-03 20:00:00 +00:00
---
- community.digitalocean.digital_ocean_droplet:
image: ubuntu-20-04-x64
name: mydroplet
oauth_token: "..."
region: sfo3
size: s-1vcpu-1gb
ssh_keys: [ .... ]
state: present
wait_timeout: 500
register: my_droplet
```
Running this playbook will create a new Droplet with the specified name, size, and operating system, and within the specified region.
If you needed to create a separate database server or another server for a new environment, then the file can be updated and re-run.
[Creating an Amazon EC2 instance](https://docs.ansible.com/ansible/latest/collections/amazon/aws/ec2_instance_module.html#ansible-collections-amazon-aws-ec2-instance-module) looks very similar:
```language-yaml
2024-01-03 20:00:00 +00:00
---
- amazon.aws.ec2_instance:
image_id: ami-123456
instance_type: c5.large
key_name: "prod-ssh-key"
name: "public-compute-instance"
network:
assign_public_ip: true
security_group: default
vpc_subnet_id: subnet-5ca1ab1e
```
This doesn't apply just to servers - you can also use Ansible to create security groups and S3 buckets, manage SSH keys, firewalls, and load balancers.
Once we have our infrastructure in place, we can start using Ansible to set and manage its configuration, which we'll do in tomorrow's email.
---
Want to learn more about how I use Ansible? [Register for my upcoming free email course]({{site.url}}/ansible-course).