使用Ansible在阿里云上创建一台ECS实例
更新时间:2019-08-06 21:24:36
您可以通过Ansible playbooks自动完成阿里云资源的创建、配置和部署。本教程指引您如何使用Ansible在阿里云上创建一台ECS实例。 本教程将创建和配置ECS实例的配置拆分成不同的Ansible playbooks,方便您了解如何通过YAML格式声明配置。您可以参考提供的完整示例,运行Playbook创建一台ECS实例。 确保您已经安装并配置了阿里云Ansible模块。详情信息,请参见安装和配置Ansible。
在创建ECS实例时,您首先需要创建一个专有网络(VPC)。 以下Ansible playbook展示了如何创建一个VPC。 在创建ECS实例时,您必须要指定ECS实例所属的交换机。 以下Ansible playbook展示了如何在VPC中创建一个交换机。 以下Ansible playbook展示了如何创建一个安全组并将安全组和VPC关联。 以下Ansible playbook展示了如何创建一台ECS实例。 完成以下操作,通过Playbook创建一台ECS实例:
说明 请您根据实际需要,参考以下示例更改ECS的配置信息。
教程概览
前提条件
创建专有网络
- name: Create a new AlibabaCloud VPC resource
ali_vpc:
alicloud_region: '{{ alicloud_region }}'
cidr_block: '{{ vpc_cidr }}'
vpc_name: '{{ vpc_name }}'
when: not vpcs.vpcs
register: vpc
创建交换机
- name: Create a new Alibaba Cloud 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: '{{vpcs.vpcs.0.id}}'
register: vswitch
创建安全组
- name: Create a security group
ali_security_group:
alicloud_region: '{{ alicloud_region }}'
state: 'present'
name: '{{ group_name }}'
description: '{{ group_description }}'
vpc_id: '{{vpcs.vpcs.0.id}}'
rules: '{{ group_inboundRules }}'
rules_egress: '{{ group_outboundRules }}'
register: group
创建ECS实例
- name: Create 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 }}'
key_name: '{{ key_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 information of the vm
debug:
msg: "The created vm is {{ ecs }}."
运行Playbook创建一台ECS实例
vi alicloud_create_ecs.yml
---
- 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-hangzhou
alicloud_zone: cn-hangzhou-e
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
t_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: 3
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: Get the existing vpc
ali_vpc_facts:
region: '{{alicloud_region}}'
vpc_name: '{{vpc_name}}'
register: vpcs
- name: Create a new alicloud VPC resource
ali_vpc:
alicloud_region: '{{ alicloud_region }}'
cidr_block: '{{ vpc_cidr }}'
vpc_name: '{{ vpc_name }}'
when: not vpcs.vpcs
register: vpc
# - name: output information of the vpc
# debug:
# msg: "The created vpc is {{ 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: '{{vpcs.vpcs.0.id}}'
register: vswitch
# - name: output information of the vpc
# debug:
# msg: "The created vpc is {{ vpc }}."
# - name: output information of the vSwitch
# debug:
# msg: "The created vpc 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: '{{vpcs.vpcs.0.id}}'
rules: '{{ group_inboundRules }}'
rules_egress: '{{ group_outboundRules }}'
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 }}'
key_name: '{{key_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 information of the vm
debug:
msg: "The created vm is {{ ecs }}."
ansible-playbook alicloud_create_ecs.yml