Ansible-使用Ansible在华为云上创建一台ECS实例:运行Playbook创建一台ECS实例

时间:2023-11-01 16:13:45

运行Playbook创建一台E CS 实例

完成以下操作,通过Playbook创建一台ECS实例:

  1. 创建一个名称为huaweicloud_create_ecs.yml的文件,然后通过VI 编辑器打开。

    vi huaweicloud_create_ecs.yml

  2. 将以下完整的Playbook示例粘贴到huaweicloud_create_ecs.yml文件中。

    - hosts: localhost  collections:    - hwceco.hwcollection  tasks:    - name: create a vpc      hwc_network_vpc:        cidr: "192.168.100.0/24"        name: "ansible_network_vpc_test"      register: vpc    - name: create a subnet      hwc_vpc_subnet:        gateway_ip: "192.168.100.32"        name: "ansible_network_subnet_test"        dhcp_enable: true        vpc_id: "{{ vpc.state.id }}"        filters:          - "name"        cidr: "192.168.100.0/26"      register: subnet    - name: create a disk      hwc_evs_disk:        filters:          - "name"        availability_zone: "cn-north-1a"        name: "ansible_evs_disk_test"        volume_type: "SSD"        size: 10      register: disk    - name: create an instance      hwc_ecs_instance:        data_volumes:          - volume_id: "{{ disk.state.id }}"        name: "ansible_ecs_instance_test"        availability_zone: "cn-north-1a"        nics:          - subnet_id: "{{ subnet.state.id }}"            ip_address:"192.168.100.33"          - subnet_id: "{{ subnet.state.id }}"            ip_address: "192.168.100.34"        server_tags:          my_server: "my_server"        image_id: "8da46d6d-6079-4e31-ad6d-a7167efff892"        flavor_name: "s3.small.1"        filters:          - "name"        vpc_id: "{{ vpc.state.id }}"        root_volume:          volume_type: "SSD"

  3. 保存并退出。
  4. 运行Ansible playbook来创建ECS实例。

    ansible-playbook huaweicloud_create_ecs.yml

support.huaweicloud.com/qs-ansible/ansible_0002.html