This project uses Ansible to automate the provisioning of an AWS EC2 instance and the setup of a security group. It demonstrates the use of Ansible playbooks, roles, and Ansible Vault for secure management of sensitive variables.
- AWS Account
- Ansible installed on your machine
- AWS CLI installed and configured
- SSH key pair created in AWS (referred to as
ansible-keypair
in this guide)
site.yml
: The main playbook that orchestrates the provisioning process.inventory/hosts.yml
: Inventory file specifying the target host.group_vars/all.yml
: Contains non-sensitive variable definitions.secret.yml
: Encrypted file containing sensitive variables likeec2_vpc_id
andec2_subnet_id
.roles/ec2-setup
: Role containing tasks to create a security group and provision EC2 instances.
-
Clone the repository:
git clone <repository-url> cd ansible-ec2-tutorial
-
Configure AWS CLI:
Make sure you have AWS CLI installed and configured:
aws configure
-
Create an Ansible Vault:
To securely store sensitive information like VPC and subnet IDs:
ansible-vault create secret.yml
Enter the sensitive data:
ec2_vpc_id: vpc-xxxxxxx ec2_subnet_id: subnet-xxxxxxx
-
Update
group_vars/all.yml
:Remove sensitive data and ensure it only contains non-sensitive information:
ec2_keypair: ansible-keypair ec2_instance_type: t2.micro ec2_ami: ami-051f8a213df8bc089 ec2_region: us-east-1 ec2_security_group_name: ansible-tutorial-sg http_port: 80
-
Running the Playbook:
Execute the main playbook:
ansible-playbook -i inventory/hosts.yml site.yml --ask-vault-pass
Enter the vault password when prompted.
- The
site.yml
playbook references theec2-setup
role, which includes tasks to create a security group and provision an EC2 instance using variables defined ingroup_vars/all.yml
andsecret.yml
. - The
ec2_group
task creates a security group allowing all inbound traffic on specified ports. - The
ec2_instance
task provisions an EC2 instance with the specified configuration and tags.
This project provides a foundational understanding of using Ansible for AWS resource management while emphasizing best practices for secure variable management with Ansible Vault. For detailed Ansible documentation, visit Ansible Documentation.