- Ansible 2 Cloud Automation Cookbook
- Aditya Patawari Vikas Aggarwal
- 543字
- 2021-06-24 18:43:38
How to do it...
We can create an EC2 instance using an Ansible module, which will take an instance type, AMI, subnet ID of VPC, tags, the exact count of instances, and region as input. Let's define tasks:
- To log into the created instance, we need to create an EC2 keypair. There are two ways to create a keypair. One is that we create a keypair on the AWS console and download a private key; the other is to import an existing keypair into AWS. We will import a keypair into EC2:
- name: Create EC2 Key Pair
ec2_key:
name: my_first_key
aws_access_key: "{{ access_key }}"
aws_secret_key: "{{ secret_key }}"
key_material: "{{ key }}"
region: "{{ aws_region }}"
state: present
Every AWS EC2 region maintains its own set of keypairs for instances to be launched in respective regions. We have passed aws_region, where we will be booting our instance and another variable that is key, which contains the content of our public key. This variable will be defined under ec2/vars/main.yml. After this task, our EC2 keypair will be generated and we can use the name defined for this keypair as the input in our next task to create an EC2 instance.
- We need to add the following things to our variables:
#Key Pair Content
key: 'ssh-rsa AAAAB3NzaC1yc2EAAAAD……………………….'
- After we have our keypair in place, we are ready to create our EC2 instances in the subnets we created in VPC tasks. We have passed the following inputs to define our instance:
-
- Instance configuration using the variable instance_type
- Private subnet of the VPC we created using the registered variable; that is my_private.subnet.id in VPC task
- Instance tags to refer our instance as key, value pair that is Name: Private Instance
- Count of instances we want to keep in AWS EC2 region with specific tags using variable private_instance_count
The argument count_tag with exact_count determine how many nodes of an instance with the specific tag should be running. If the count of instances exceeds the parameters exact_count then extra instances would be terminated. In this example, we are keeping exact count for the instance tagged with Name: Private Instance.
- name: Create EC2 Instance in private subnet
ec2:
key_name: my_first_key
instance_type: "{{ instance_type }}"
image: "{{ ami_id }}"
wait: yes
group: my_first_sg
vpc_subnet_id: "{{ my_private_subnet.subnet.id }}"
aws_access_key: "{{ access_key }}"
aws_secret_key: "{{ secret_key }}"
region: "{{ aws_region }}"
count_tag:
Name: Private Instance
exact_count: "{{ private_instance_count }}"
instance_tags:
Name: Private Instance
- We need to define the following variables for the preceding tasks:
#EC2 Instance Info
instance_type: 't2.micro'
ami_id: 'ami-46c1b650'
private_instance_count: 1
public_instance_count: 1
- Similarly, we can create an instance in the public subnet as well:
- name: Create EC2 Instance in public subnet
ec2:
key_name: my_first_key
instance_type: "{{ instance_type }}"
image: "{{ ami_id }}"
wait: yes
group: my_first_sg
vpc_subnet_id: "{{ my_public_subnet.subnet.id }}"
assign_public_ip: yes
aws_access_key: "{{ access_key }}"
aws_secret_key: "{{ secret_key }}"
region: "{{ aws_region }}"
count_tag:
Name: Public Instance
exact_count: "{{ public_instance_count }}"
instance_tags:
Name: Public Instance
register: ec2_public_instance
This task is identical to the previous task. The only difference is the different tags and subnet ID. We have created two instances in the preceding tasks, in each of the subnets we created in VPC tasks.
- 大數(shù)據(jù)管理系統(tǒng)
- Div+CSS 3.0網(wǎng)頁布局案例精粹
- Hands-On Neural Networks with Keras
- Maya 2012從入門到精通
- OpenStack Cloud Computing Cookbook(Second Edition)
- STM32嵌入式微控制器快速上手
- 單片機技術一學就會
- MCGS嵌入版組態(tài)軟件應用教程
- 啊哈C!思考快你一步
- Linux系統(tǒng)下C程序開發(fā)詳解
- Mastering OpenStack(Second Edition)
- 手把手教你學Photoshop CS3
- 計算機組裝與維修實訓
- 菜鳥起飛五筆打字高手
- 實戰(zhàn)突擊