使用Ansible在阿里云上創建一臺ECS實例
更新時間:
您可以通過Ansible playbooks自動完成阿里云資源的創建、配置和部署。本教程指引您如何使用Ansible在阿里云上創建一臺ECS實例。
教程概覽
本教程將創建和配置ECS實例的配置拆分成不同的Ansible playbooks,方便您了解如何通過YAML格式聲明配置。您可以參考提供的完整示例,運行Playbook創建一臺ECS實例。更多信息,請參見ECS示例。
前提條件
確保您已經安裝并配置了阿里云Ansible模塊。詳情信息,請參見安裝和配置Ansible。
創建專有網絡
在創建ECS實例時,您首先需要創建一個專有網絡(VPC)。
以下Ansible playbook展示了如何創建一個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: vpc
創建交換機
在創建ECS實例時,您必須要指定ECS實例所屬的交換機。
以下Ansible playbook展示了如何在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: '測試ansible創建交換機'
vpc_id: 'vpc-m5e08bfrh8288********'
register: vswitch
~
創建安全組
以下Ansible playbook展示了如何創建一個安全組并將安全組和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: '測試ansible創建安全組'
vpc_id: 'vpc-m5e08bfrh8288********'
register: group
創建ECS實例
以下Ansible playbook展示了如何創建一臺ECS實例。
---
- name: Create a new VPC
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: '測試ansible創建ECS'
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: ecs
運行Playbook創建一臺ECS實例
完成以下操作,通過Playbook創建一臺ECS實例:
創建一個名稱為alicloud_create_ecs.yml的文件,然后通過VI 編輯器打開。
vi alicloud_create_ecs.yml
在編輯模式下,將以下完整的Playbook示例粘貼到alicloud_create_ecs.yml文件中。
說明請您根據實際需要,參考以下示例更改ECS的配置信息。
--- - 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 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: '{{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 information of the vm debug: msg: "The created vm is {{ ecs }}."
保存后,退出編輯模式。
運行Ansible playbook創建ECS實例。
ansible-playbook alicloud_create_ecs.yml
文檔內容是否對您有幫助?