Ec2 instance using Ansible
I am going to run ec2 instance using ansible. We have to setup ansible so that it can call aws module in playbook. There are basic requirement to setup it:--
- Python > 2.6
- Boto
AMAZON.AWS.EC2 install
ansible-galaxy collection install amazon.aws
To use it in a playbook, specify: amazon.aws.ec2
These are sample playbook to create & terminate instance:--
# Run Ec2 instance from AWS
---
- name: Ec2 Run
hosts: localhost
connection: local
tasks:
- name: Running Instance
amazon.aws.ec2:
key_name: SharadKeyPair
instance_type: t2.micro
image: ami-08e0ca9924195beba
region: ap-south-1
wait: yes
#group: Ansible
count: 1
vpc_subnet_id: subnet-<subnetid>
assign_public_ip: yes
aws_access_key: <AWS access key>
aws_secret_key: <AWS secret key>
Created this file and saved it as ec2run.yaml format.
This is attached screenshot when playbook was running :------
## After running ec2 instance We will write playbook to get public IP & details of instance running on AWS
To get instance details, We need to install community.aws module in ansible server, Command are as below:--
ansible-galaxy collection install community.aws
For this boto3 also required to get instance information
sudo apt install python3-boto3
Comments
Post a Comment