Use Ansible to create an ECS instance on Alibaba Cloud
You can use Ansible playbooks to automate the creation, configuration, and deployment of Alibaba Cloud resources. This tutorial shows how to use Ansible to create an ECS instance on Alibaba Cloud.
Tutorial overview
This tutorial splits the ECS instance creation and configuration into different Ansible playbooks to help you understand how to declare configurations in YAML format. You can use the complete example to run the playbook and create an ECS instance. For more information, see ECS example.
Prerequisites
Install and configure Ansible for use with Alibaba Cloud. For more information, see Install and configure Ansible.
Create a VPC
Before you create an ECS instance, you must create a virtual private cloud (VPC).
The following Ansible playbook shows how to create a VPC.
---
- name: Create a new VPC
hosts: localhost
connection: local
tasks:
- name: Create a new alicloud VPC resource
ali_vpc:
alicloud_region: 'cn-qingdao'
cidr_block: '192.168.0.0/16'
vpc_name: 'vpc_test'
register: vpcCreate a vSwitch
You must specify a vSwitch when you create an ECS instance.
You can use the following Ansible playbook to create a vSwitch in a VPC.
---
- name: Create a new Alibaba Cloud VSwitch resource
hosts: localhost
connection: local
tasks:
- name: Create a new Alibaba Cloud VSwitch resource
ali_vswitch:
alicloud_region: 'cn-qingdao'
alicloud_zone: 'cn-qingdao-b'
state: 'present'
cidr_block: '192.168.0.0/24'
vswitch_name: 'vswitch_test'
description: 'Test creating a vSwitch with Ansible'
vpc_id: 'vpc-m5e08bfrh8288********'
register: vswitch
~ Create a security group
The following Ansible playbook creates a security group and associates it with a VPC.
---
- name: Create a security group
hosts: localhost
connection: local
tasks:
- name: Create a security group
ali_security_group:
alicloud_region: 'cn-qingdao'
state: 'present'
name: 'group_test'
description: 'Test creating a security group with Ansible'
vpc_id: 'vpc-m5e08bfrh8288********'
register: groupCreate an ECS instance
The following Ansible playbook creates an ECS instance.
---
- name: Create an ECS instance
hosts: localhost
connection: local
tasks:
- name: Create an ECS instance
ali_instance:
alicloud_region: 'cn-qingdao'
alicloud_zone: 'cn-qingdao-b'
image_id: 'aliyun_3_x64_20G_alibase_20240528.vhd'
instance_type: 'ecs.g6.large'
internet_charge_type: 'PayByTraffic'
instance_name: 'ecs_ansible_test'
description: 'Test creating an ECS instance with Ansible'
system_disk_category: 'cloud_efficiency'
system_disk_size: '40'
password: 'ansible@1234'
vswitch_id: 'vsw-m5ey1ybjn0yh0********'
security_groups: ['sg-m5e2fjcye6yb********']
host_name: 'myhost'
count: 1
instance_charge_type: 'PostPaid'
register: ecsRun the playbook to create an ECS instance
You can create an ECS instance using a playbook.
Create a file named alicloud_create_ecs.yml and open it in the VI editor.
vi alicloud_create_ecs.ymlIn edit mode, paste the following playbook example into the alicloud_create_ecs.yml file.
NoteModify the ECS configuration in the example as needed.
--- - name: Create a new VPC hosts: localhost connection: local vars: vpc_cidr: "172.16.0.0/12" vpc_name: "VPC_From_Ansible" vpc_description: "Create a new VPC resource via Ansible example alicloud-ecs-vpc." alicloud_region: cn-qingdao alicloud_zone: cn-qingdao-b vswitch_cidr: "172.16.1.0/24" vswitch_name: "VSwitch_From_Ansible" vswitch_description: "Create a new VSwitch resource via Ansible example alicloud-ecs-vpc." group_name: "Security_Group_From_Ansible" group_description: "Create a new security group resource via Ansible example alicloud-ecs-vpc." group_inboundRules: - ip_protocol: tcp port_range: 22/22 source_cidr_ip: 0.0.0.0/0 dest_cidr_ip: 47.89.23.33/32 priority: 2 image: centos_6_8_64_40G_base_20170222.vhd type: ecs.n4.small instance_name: newtests2 description: travis-ansible-instance2 host_name: myhost count: 1 allocate_public_ip: True internet_charge_type: PayByBandwidth max_bandwidth_in: 200 max_bandwidth_out: 10 key_name: ECS_KEY tags: role: frontend tasks: - name: Create a new alicloud VPC resource ali_vpc: alicloud_region: '{{ alicloud_region }}' cidr_block: '{{ vpc_cidr }}' vpc_name: '{{ vpc_name }}' register: vpc - name: Create a new alicloud VSwitch resource ali_vswitch: alicloud_region: '{{ alicloud_region }}' alicloud_zone: '{{ alicloud_zone }}' state: 'present' cidr_block: '{{ vswitch_cidr }}' vswitch_name: '{{ vswitch_name }}' description: '{{ vswitch_description }}' vpc_id: '{{vpc.vpc.id}}' register: vswitch # - name: Output the VPC information # debug: # msg: "The created VPC is {{ vpc }}." # - name: Output the vSwitch information # debug: # msg: "The created vSwitch is {{ vswitch }}." # - name: Get the existing VPC # ali_vpc_facts: # region: '{{alicloud_region}}' # vpc_name: '{{vpc_name}}' # register: vpcs - name: Creating security group ali_security_group: alicloud_region: '{{ alicloud_region }}' state: 'present' name: '{{ group_name }}' description: '{{ group_description }}' vpc_id: '{{vpc.vpc.id}}' rules: '{{ group_inboundRules }}' register: group - name: Creating an ECS instance ali_instance: alicloud_region: '{{ alicloud_region }}' alicloud_zone: '{{ alicloud_zone }}' image_id: '{{ image }}' instance_type: '{{ type }}' instance_name: '{{ instance_name }}' description: '{{ description }}' host_name: '{{ host_name }}' vswitch_id: '{{vswitch.vswitch.id}}' security_groups: '{{group.group.id}}' count: '{{count}}' allocate_public_ip: '{{ allocate_public_ip }}' internet_charge_type: '{{ internet_charge_type }}' max_bandwidth_in: '{{ max_bandwidth_in }}' max_bandwidth_out: '{{ max_bandwidth_out }}' tags: '{{tags}}' register: ecs - name: Output the VM information debug: msg: "The created VM is {{ ecs }}."Save the file and exit edit mode.
Run the Ansible playbook to create the ECS instance.
ansible-playbook alicloud_create_ecs.yml