From a8cee9437aa66aa842ece67ad4779b940a0e3276 Mon Sep 17 00:00:00 2001 From: lixue <1120722567@qq.com> Date: Wed, 26 Feb 2020 21:09:30 +0800 Subject: [PATCH] deploy petstore on ECS --- examples/deploy-ecs/LICENSE.md | 4 + examples/deploy-ecs/README.md | 22 ++++++ examples/deploy-ecs/create.yml | 10 +++ examples/deploy-ecs/delete.yml | 78 +++++++++++++++++++ examples/deploy-ecs/deploy.yml | 57 ++++++++++++++ examples/deploy-ecs/group_vars/all | 60 ++++++++++++++ .../deploy-ecs/roles/instance/tasks/main.yml | 68 ++++++++++++++++ .../roles/security_group/tasks/main.yml | 14 ++++ examples/deploy-ecs/roles/vpc/tasks/main.yml | 12 +++ .../deploy-ecs/roles/vswitch/tasks/main.yml | 32 ++++++++ 10 files changed, 357 insertions(+) create mode 100644 examples/deploy-ecs/LICENSE.md create mode 100644 examples/deploy-ecs/README.md create mode 100644 examples/deploy-ecs/create.yml create mode 100644 examples/deploy-ecs/delete.yml create mode 100644 examples/deploy-ecs/deploy.yml create mode 100644 examples/deploy-ecs/group_vars/all create mode 100644 examples/deploy-ecs/roles/instance/tasks/main.yml create mode 100644 examples/deploy-ecs/roles/security_group/tasks/main.yml create mode 100644 examples/deploy-ecs/roles/vpc/tasks/main.yml create mode 100644 examples/deploy-ecs/roles/vswitch/tasks/main.yml diff --git a/examples/deploy-ecs/LICENSE.md b/examples/deploy-ecs/LICENSE.md new file mode 100644 index 00000000..bf7c12c3 --- /dev/null +++ b/examples/deploy-ecs/LICENSE.md @@ -0,0 +1,4 @@ +Copyright (C) 2017-present Alibaba Cloud, Inc. + +This work is licensed under the Creative Commons Attribution 3.0 Unported License. +To view a copy of this license, visit http://creativecommons.org/licenses/by/3.0/deed.en_US. diff --git a/examples/deploy-ecs/README.md b/examples/deploy-ecs/README.md new file mode 100644 index 00000000..390fec01 --- /dev/null +++ b/examples/deploy-ecs/README.md @@ -0,0 +1,22 @@ +## Add or Remove tags for resources + +- Requires Ansible 2.9 or newer +- Requires Ansible-Alicloud 1.17.0 or newer +- Requires footmark 1.18.0 or newer + + +These playbooks Create ECS and Deploy product on them. + +These playbooks' hosts default to `localhost`. To use, make the file and edit the `hosts` inventory file to include the names or IPs of the servers +you want to deploy. + +Then run the playbook, like this: + + ansible-playbook -i hosts deploy.yml + +When the run is complete, you can login in the Alicloud console to check them. + + +### Ideas for Improvement +We would love to see contributions and improvements, so please fork this +repository on GitHub and send us your changes via pull requests. diff --git a/examples/deploy-ecs/create.yml b/examples/deploy-ecs/create.yml new file mode 100644 index 00000000..13633b85 --- /dev/null +++ b/examples/deploy-ecs/create.yml @@ -0,0 +1,10 @@ +--- +- name: Create ECS + hosts: localhost + remote_user: root + + roles: + - vpc + - vswitch + - security_group + - instance diff --git a/examples/deploy-ecs/delete.yml b/examples/deploy-ecs/delete.yml new file mode 100644 index 00000000..5e1a0b63 --- /dev/null +++ b/examples/deploy-ecs/delete.yml @@ -0,0 +1,78 @@ +--- +- name: Delete resource + hosts: localhost + remote_user: root + + tasks: + - name: Filter instance using name_regex + ali_instance_info: + name_prefix: '{{ instance_name }}' + register: instances + + - name: Changed. Deleting instances + ali_instance: + instance_ids: '{{instances.ids}}' + force: True + state: absent + + + - name: Filter instance using name_regex + ali_instance_info: + name_prefix: '{{ instance_name_a }}' + register: instances + + - name: Changed. Deleting instances with another vswitch + ali_instance: + instance_ids: '{{instances.ids}}' + force: True + state: absent + + + - name: Filter security group using name_regex + ali_security_group_info: + name_prefix: '{{ security_group_name }}' + register: sgs + + - name: Changed. Deleting the security groups + ali_security_group: + name: '{{ item.group_name }}' + vpc_id: '{{item.vpc_id}}' + state: absent + with_items: '{{sgs.groups}}' + + + - name: Get the existing vswitch + ali_vswitch_info: + name_prefix: '{{ vswitch_name }}' + register: vswitches + + - name: Changed. Deleting vswitches + ali_vswitch: + vpc_id: '{{ item.vpc_id}}' + cidr_block: '{{ item.cidr_block}}' + state: absent + with_items: '{{vswitches.vswitches}}' + + - name: Get another vswitch + ali_vswitch_info: + name_prefix: '{{ vswitch_name_a }}' + register: vswitches + + - name: Changed. Deleting vswitches + ali_vswitch: + vpc_id: '{{ item.vpc_id}}' + cidr_block: '{{ item.cidr_block}}' + state: absent + with_items: '{{vswitches.vswitches}}' + + - name: Filter vpc using name_regex + ali_vpc_info: + name_prefix: '{{ vpc_name }}' + register: vpcs + + - name: Changed. Deleting vpcs + ali_vpc: + cidr_block: '{{ item.cidr_block }}' + name: '{{ item.vpc_name }}' + state: absent + with_items: '{{vpcs.vpcs}}' diff --git a/examples/deploy-ecs/deploy.yml b/examples/deploy-ecs/deploy.yml new file mode 100644 index 00000000..e57d71dd --- /dev/null +++ b/examples/deploy-ecs/deploy.yml @@ -0,0 +1,57 @@ +--- +- name: Deploy petstore on ECS + hosts: alicloud + remote_user: root + + tasks: + - name: create property file + shell: "{{item}}" + with_items: + - echo "petstore.loggingLevel = warn" >> /root/antx.properties + - echo "petstore.loggingRoot = /root/pet/logs" >> /root/antx.properties + - echo "petstore.upload = /root/pet/upload" >> /root/antx.properties + - echo "petstore.work = /root/pet" >> /root/antx.properties + + - name: remove java11 + apt: + pkg: + - openjdk-11-jre-headless + - openjdk-11-jdk + state: absent + force_apt_get: True + + - name: build enviroment + apt: + pkg: + - apache2 + - openjdk-8-jre-headless + - openjdk-8-jdk + - git + - maven + force_apt_get: True + + - name: clone project + command: "{{item}}" + with_items: + - git clone https://github.com/webx/citrus-sample.git + + - name: mvn install + command: "{{item}}" + args: + chdir: /root/citrus-sample/petstore + with_items: + - mvn clean install + ignore_errors: True + async: 60 + poll: 10 + + - name: run project + shell: + cmd: nohup mvn jetty:run-war & + chdir: /root/citrus-sample/petstore/web + register: petstore + + - wait_for: port=8081 state=started + + - debug: + msg: http://{{hostvars[inventory_hostname]['public_ip_address']}}:8081?home diff --git a/examples/deploy-ecs/group_vars/all b/examples/deploy-ecs/group_vars/all new file mode 100644 index 00000000..2cfb81c5 --- /dev/null +++ b/examples/deploy-ecs/group_vars/all @@ -0,0 +1,60 @@ +--- +# ecs instance filter parameters +instance_name: 'Instance_From_Ansible_Petstore' +instance_name_a: 'a-Instance_From_Ansible_Petstore' +instance_tags: {For: PetStore, Created: Ansible, From: example/deply} +image_id: "ubuntu_18_04_64_20G_alibase_20190624.vhd" +instance_type: ecs.g5.large +instance_description: "Create a new ECS instance resource via Ansible example deploy-ecs." +host_name: "my-instance-from-ansible" +password: Test12345 + +allocate_public_ip: True +internet_charge_type: "PayByTraffic" +max_bandwidth_in: 200 +max_bandwidth_out: 50 + +system_disk_category: "cloud_ssd" +system_disk_size: 50 +number_of_instances: 2 + +# security group filter parameters +security_group_name: 'Security_Group_From_Ansible' +security_group_tags: {For: PetStore, Created: Ansible, From: example/deply} + +group_description: "Create a new security group resource via Ansible example deploy-ecs." +group_inboundRules: + - ip_protocol: tcp + port_range: 22/22 + source_cidr_ip: 0.0.0.0/0 + priority: 1 + + - ip_protocol: tcp + port_range: 80/80 + source_cidr_ip: 0.0.0.0/0 + priority: 1 + + - ip_protocol: tcp + port_range: 8081/8081 + source_cidr_ip: 0.0.0.0/0 + priority: 1 + +group_outboundRules: + - ip_protocol: tcp + port_range: 80/80 + dest_cidr_ip: 192.168.0.54/32 + priority: 1 + +# vpc filter parameters +vpc_name: 'Vpc_From_Ansible' +vpc_tags: {For: PetStore, Created: Ansible, From: example/deply} +vpc_cidr: "172.16.0.0/12" +vpc_description: "Create a new VPC resource via Ansible example alicloud-ecs-vpc." + +# vswitch parameters +vswitch_cidr: "172.16.1.0/24" +vswitch_cidr_a: "172.22.0.0/16" +vswitch_description: "Create a new VSwitch resource via Ansible example-deploy-ecs." +vswitch_name: 'Vswitch_From_Ansible' +vswitch_name_a: 'a-Vswitch_From_Ansible' +availability_zone: cn-beijing-h diff --git a/examples/deploy-ecs/roles/instance/tasks/main.yml b/examples/deploy-ecs/roles/instance/tasks/main.yml new file mode 100644 index 00000000..d249b7d4 --- /dev/null +++ b/examples/deploy-ecs/roles/instance/tasks/main.yml @@ -0,0 +1,68 @@ +--- +- name: Get the existing ECS instances + ali_instance_info: + name_prefix: '{{ instance_name }}' + filters: + vpc_id: '{{ vpcs.vpcs.0.id }}' + register: instances + +- name: Get the existing ECS instances + ali_instance_info: + name_prefix: '{{ instance_name_a }}' + filters: + vpc_id: '{{ vpcs.vpcs.0.id }}' + register: instances_a + +- name: Creating two ECS instances + ali_instance: + image: '{{ image_id }}' + type: '{{ instance_type }}' + instance_name: '{{ instance_name }}' + description: '{{ instance_description }}' + host_name: '{{ host_name }}' + password: '{{ password }}' + + allocate_public_ip: '{{ allocate_public_ip }}' + internet_charge_type: '{{ internet_charge_type }}' + max_bandwidth_in: '{{ max_bandwidth_in }}' + max_bandwidth_out: '{{ max_bandwidth_out }}' + + security_groups: ['{{ sgs.groups.0.id }}'] + vswitch_id: '{{ vswitches.vswitches.0.id }}' + + system_disk_category: '{{ system_disk_category }}' + system_disk_size: '{{ system_disk_size }}' + count: '{{ number_of_instances }}' + tags: '{{ instance_tags }}' + when: not instances.instances + +- name: Creating two ECS instances with another vswitch. + ali_instance: + image: '{{ image_id }}' + type: '{{ instance_type }}' + instance_name: '{{ instance_name_a }}' + description: '{{ instance_description }}' + host_name: '{{ host_name }}' + password: '{{ password }}' + + allocate_public_ip: '{{ allocate_public_ip }}' + internet_charge_type: '{{ internet_charge_type }}' + max_bandwidth_in: '{{ max_bandwidth_in }}' + max_bandwidth_out: '{{ max_bandwidth_out }}' + + security_groups: ['{{ sgs.groups.0.id }}'] + vswitch_id: '{{ vswitches_a.vswitches.0.id }}' + + system_disk_category: '{{ system_disk_category }}' + system_disk_size: '{{ system_disk_size }}' + count: '{{ number_of_instances }}' + when: not instances_a.instances + +- name: Get the existing ECS instances + ali_instance_info: + name_prefix: '{{ instance_name }}' + register: instances + +- name: Get the existing ECS instances + ali_instance_info: + name_prefix: '{{ instance_name_a }}' diff --git a/examples/deploy-ecs/roles/security_group/tasks/main.yml b/examples/deploy-ecs/roles/security_group/tasks/main.yml new file mode 100644 index 00000000..296fa1ae --- /dev/null +++ b/examples/deploy-ecs/roles/security_group/tasks/main.yml @@ -0,0 +1,14 @@ +--- +- name: Create a VPC security group + ali_security_group: + name: '{{ security_group_name }}' + description: '{{ group_description }}' + vpc_id: '{{vpc.vpc.id}}' + rules: '{{ group_inboundRules }}' + rules_egress: '{{ group_outboundRules }}' + register: group + +- name: Get the existing groups + ali_security_group_info: + name_prefix: '{{ security_group_name }}' + register: sgs diff --git a/examples/deploy-ecs/roles/vpc/tasks/main.yml b/examples/deploy-ecs/roles/vpc/tasks/main.yml new file mode 100644 index 00000000..ae6fa3a3 --- /dev/null +++ b/examples/deploy-ecs/roles/vpc/tasks/main.yml @@ -0,0 +1,12 @@ +--- +- name: Create a new alicloud VPC resource + ali_vpc: + cidr_block: '{{ vpc_cidr }}' + vpc_name: '{{ vpc_name }}' + description: '{{ vpc_description }}' + register: vpc + +- name: Get the existing vpc + ali_vpc_info: + name_prefix: '{{vpc_name}}' + register: vpcs \ No newline at end of file diff --git a/examples/deploy-ecs/roles/vswitch/tasks/main.yml b/examples/deploy-ecs/roles/vswitch/tasks/main.yml new file mode 100644 index 00000000..7bfa9b3f --- /dev/null +++ b/examples/deploy-ecs/roles/vswitch/tasks/main.yml @@ -0,0 +1,32 @@ +--- +- name: Create a new alicloud VSwitch resource + ali_vswitch: + alicloud_zone: '{{ availability_zone }}' + cidr_block: '{{ vswitch_cidr }}' + vswitch_name: '{{ vswitch_name }}' + description: '{{ vswitch_description }}' + vpc_id: '{{vpc.vpc.id}}' + register: vswitch + +- name: Create another alicloud VSwitch resource + ali_vswitch: + alicloud_zone: '{{ availability_zone }}' + cidr_block: '{{ vswitch_cidr_a }}' + vswitch_name: '{{ vswitch_name_a }}' + description: '{{ vswitch_description }}' + vpc_id: '{{vpc.vpc.id}}' + register: vswitch_a + +- name: Get the existing vswitch + ali_vswitch_info: + name_prefix: '{{ vswitch_name }}' + filters: + vpc_id: '{{vpc.vpc.id}}' + register: vswitches + +- name: Get the existing vswitch + ali_vswitch_info: + name_prefix: '{{ vswitch_name_a }}' + filters: + vpc_id: '{{vpc.vpc.id}}' + register: vswitches_a \ No newline at end of file