Automating Cloud Infrastructure with Ansible: Part 2

Overview of Virtual Private Cloud

Creating a VPC

- name: create vpc
hosts: localhost
connection: local
vars:
alicloud_access_key: "LTAI5tcS3ErsKjIT"
alicloud_secret_key: "rVgR0Xnpk9dQIcvqDJFbL8ZdfRzeu0"
alicloud_region: "ap-southeast-1"
state: present
cidr_block: 192.168.0.0/16
vpc_name: Demo
description: Demo VPC
tasks:
- name: create vpc
ali_vpc:
alicloud_access_key: '{{ alicloud_access_key }}'
alicloud_secret_key: '{{ alicloud_secret_key }}'
alicloud_region: '{{ alicloud_region }}'
state: '{{ state }}'
cidr_block: '{{ cidr_block }}'
vpc_name: '{{ vpc_name }}'
description: '{{ description }}'
register: result
- debug: var=result
[root@ogslab3 Alibaba]# ansible-playbook vpc.yml

Deleting a VPC

- name: deleting vpc
hosts: localhost
connection: local
vars:
alicloud_access_key: "LTAI5tcS3ErsKjIT"
alicloud_secret_key: "rVgR0Xnpk9dQIcvqDJFbL8ZdfRzeu0"
alicloud_region: "ap-southeast-1"
state: absent
vpc_id: "vpc-t4na5wvzit836tgwunyu6"
tasks:
- name: create vpc
ali_vpc:
alicloud_access_key: '{{ alicloud_access_key }}'
alicloud_secret_key: '{{ alicloud_secret_key }}'
alicloud_region: '{{ alicloud_region }}'
state: '{{ state }}'
vpc_id: '{{ vpc_id}}'
register: result
- debug: var=result
[root@ogslab3 Alibaba]# ansible-playbook vpclst.yml

VSwitch

Creating of VSwitch with Ansible

- name: create a vswitch
hosts: localhost
connection: local
vars:
alicloud_access_key: "LTAI5tcS3ErsKjIT"
alicloud_secret_key: "rVgR0Xnpk9dQIcvqDJFbL8ZdfRzeu0"
alicloud_region: "ap-southeast-1"
vpc_id: "vpc-t4nffwrs25fheo1ss1nit"
alicloud_zone: "ap-southeast-1b"
cidr_block: '192.168.0.0/16'
name: 'Demo_VSwitch'
state: present
tasks:
- name: create vswitch
alicloud_vswitch:
alicloud_access_key: "{{ alicloud_access_key }}"
alicloud_secret_key: "{{ alicloud_secret_key }}"
alicloud_region: '{{ alicloud_region }}'
vpc_id: '{{ vpc_id }}'
alicloud_zone: "{{ alicloud_zone }}"
cidr_block: '{{ cidr_block }}'
name: '{{ name }}'
state: '{{ state }}'
register: result
- debug: var=result
[root@ogslab3 Alibaba]# ansible-playbook vswitch.yml

Deleting the VSwitch with Ansible

- name: Delete a vswitch
hosts: localhost
connection: local
vars:
alicloud_access_key: "LTAInSSyrlHBRo7X"
alicloud_secret_key: "EWUCzq8qqfGMtjLJfK7gh8gjFKSZGG"
alicloud_region: "ap-southeast-1"
vpc_id: "vpc-t4na5wvzit836tgwunyu6"
vswitch_id: "vsw-t4n5wsorwbf5dnguvppem"
state: absent
tasks:
- name: Delete vswitch
alicloud_vswitch:
alicloud_access_key: "{{ alicloud_access_key }}"
alicloud_secret_key: "{{ alicloud_secret_key }}"
alicloud_region: '{{ alicloud_region }}'
vpc_id: '{{ vpc_id }}'
vswitch_id: '{{ vswitch_id }}'
state: '{{ state }}'
register: result
- debug: var=result
[root@ogslab3 Alibaba]# ansible-playbook vswitch.yml

Security Group

Creating Security Group with Ansible

- name: create security group
hosts: localhost
connection: local
vars:
alicloud_access_key: "LTAI5tcS3ErsKjIT"
alicloud_secret_key: "rVgR0Xnpk9dQIcvqDJFbL8ZdfRzeu0"
alicloud_region: "ap-southeast-1"
tasks:
- name: create security grp
ali_security_group:
alicloud_access_key: '{{ alicloud_access_key }}'
alicloud_secret_key: '{{ alicloud_secret_key }}'
alicloud_region: '{{ alicloud_region }}'
group_name: 'AliyunSG'
[root@ogslab3 Alibaba]# ansible-playbook sg.yml

Deleting of Security Group with Ansible

- name: delete security group
hosts: localhost
connection: local
vars:
alicloud_access_key: "LTAInSSyrlHBRo7X"
alicloud_secret_key: "EWUCzq8qqfGMtjLJfK7gh8gjFKSZGG"
alicloud_region: "ap-southeast-1"
group_id: sg-t4ngnkqimz75l6rxzh94
state: absent
tasks:
- name: delete security grp
ali_security_group:
alicloud_access_key: '{{ alicloud_access_key }}'
alicloud_secret_key: '{{ alicloud_secret_key }}'
alicloud_region: '{{ alicloud_region }}'
group_id: '{{ group_id }}'
state: '{{ state }}'
register: delete result
- debug: var=delete result
[root@ogslab3 Alibaba]# ansible-playbook sgd.yml

Object Storage Service

Creating Bucket with Ansible

- name: create oss bucket
hosts: localhost
connection: local
vars:
alicloud_access_key: "LTAI5tcS3ErsKjIT"
alicloud_secret_key: "rVgR0Xnpk9dQIcvqDJFbL8ZdfRzeu0"
alicloud_region: "ap-southeast-1"
state: present
bucket: 'bucketogs'
permission: private
tasks:
- name: create oss bucket
alicloud_bucket:
alicloud_access_key: '{{ alicloud_access_key }}'
alicloud_secret_key: '{{ alicloud_secret_key }}'
alicloud_region: '{{ alicloud_region }}'
state: '{{ state }}'
bucket: '{{ bucket }}'
permission: '{{ permission }}'
register: result
- debug: var=result
[root@ogslab3 Alibaba]# ansible-playbook bucket.yml

Deleting a Bucket

- name: Delete oss bucket
hosts: localhost
connection: local
vars:
alicloud_access_key: "LTAI5tcS3ErsKjIT"
alicloud_secret_key: "rVgR0Xnpk9dQIcvqDJFbL8ZdfRzeu0"
alicloud_region: "ap-southeast-1"
state: absent
bucket: 'ogsbucket'
permission: private
tasks:
- name: create oss bucket
alicloud_bucket:
alicloud_access_key: '{{ alicloud_access_key }}'
alicloud_secret_key: '{{ alicloud_secret_key }}'
alicloud_region: '{{ alicloud_region }}'
state: '{{ state }}'
bucket: '{{ bucket }}'
permission: '{{ permission }}'
register: result
- debug: var=result
[root@ogslab3 Alibaba]# ansible-playbook deletebucket.yml

Fetching Bucket Details

- name: Fetch oss bucket
hosts: localhost
connection: local
vars:
alicloud_access_key: "LTAI5tcS3ErsKjIT"
alicloud_secret_key: "rVgR0Xnpk9dQIcvqDJFbL8ZdfRzeu0"
alicloud_region: "ap-southeast-1"
state: absent
bucket: 'bucketogs'
permission: private
tasks:
- name: Fetch oss bucket
alicloud_bucket:
alicloud_access_key: '{{ alicloud_access_key }}'
alicloud_secret_key: '{{ alicloud_secret_key }}'
alicloud_region: '{{ alicloud_region }}'
state: '{{ state }}'
bucket: '{{ bucket }}'
permission: '{{ permission }}'
register: result
- debug: var=result
[root@ogslab3 Alibaba]# ansible-playbook bucketnew.yml

Listing Objects

- name: listing bucket objects
hosts: localhost
connection: local
vars:
alicloud_access_key: "LTAInSSyrlHBRo7X"
alicloud_secret_key: "EWUCzq8qqfGMtjLJfK7gh8gjFKSZGG"
alicloud_region: "ap-southeast-1"
mode: list
bucket: bucketogs
tasks:
- name: listing bucket objects
alicloud_bucket_object:
alicloud_access_key: '{{ alicloud_access_key }}'
alicloud_secret_key: '{{ alicloud_secret_key }}'
alicloud_region: '{{ alicloud_region }}'
mode: '{{ mode }}'
bucket: '{{ bucket }}'
register: result
- debug: var=result
[ansible@ogslab3 Alibaba]# ansible-playbook oss.yml

Deleting Objects

- name: Deleting bucket objects
hosts: localhost
connection: local
vars:
alicloud_access_key: "LTAInSSyrlHBRo7X"
alicloud_secret_key: "EWUCzq8qqfGMtjLJfK7gh8gjFKSZGG"
alicloud_region: "ap-southeast-1"
mode: delete
bucket: bucketogs
object: 'NEW ACESS KEY.txt'
tasks:
- name: Deleting bucket objects
alicloud_bucket_object:
alicloud_access_key: '{{ alicloud_access_key }}'
alicloud_secret_key: '{{ alicloud_secret_key }}'
alicloud_region: '{{ alicloud_region }}'
mode: '{{ mode }}'
bucket: '{{ bucket }}'
object: '{{ object }}'
register: result
- debug: var=result
[ansible@ogslab3 Alibaba]# ansible-playbook oss.yml

Elastic Compute Service

Instance Provisioning

- name: basic provisioning example
hosts: localhost
vars:
alicloud_access_key: LTAIzpw8uK7VVWmM
alicloud_secret_key: jPuJzWHKP3QZewpXEe0NA3TDVisSSR
alicloud_region: ap-southeast-1
image: ubuntu_16_0402_64_20G_alibase_20180409.vhd
instance_type: ecs.t5-lc2m1.nano
assign_public_ip: True
max_bandwidth_out: 10
host_name: myhost
password: MyPassword@10
system_disk_category: cloud_efficiency
system_disk_size: 100
internet_charge_type: PayByTraffic
security_groups: ["sg-t4n50q981ove2azth8ys"]
force: True
tasks:
- name: launch ECS instance in VPC network
ali_instance:
alicloud_access_key: '{{ alicloud_access_key }}'
alicloud_secret_key: '{{ alicloud_secret_key }}'
alicloud_region: '{{ alicloud_region }}'
image: '{{ image }}'
system_disk_category: '{{ system_disk_category }}'
system_disk_size: '{{ system_disk_size }}'
instance_type: '{{ instance_type }}
assign_public_ip: '{{ assign_public_ip }}'
security_groups: '{{ security_groups }}'
internet_charge_type: '{{ internet_charge_type }}'
max_bandwidth_out: '{{ max_bandwidth_out }}'
instance_tags:
Name: created_one
host_name: '{{ host_name }}'
password: '{{ password }}'
ignore_errors: yes

--

--

--

Follow me to keep abreast with the latest technology news, industry insights, and developer trends. Alibaba Cloud website:https://www.alibabacloud.com

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Papa The Man The Myth The Legend Shirt Design 8

Free claps for everyone! — a medium-style applause button

Building a Kubernetes Application with Amazon EKS

Service layer method inject using Autofac in a .NET Console Application

Google is a Leader at the Gartner® Magic Quadrant™

Introduction to Disjoint Sets

How to access Jupyter Notebooks running in your local server with ngrok (and an intro to GNU…

MongoDB vs. MySQL

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Alibaba Cloud

Alibaba Cloud

Follow me to keep abreast with the latest technology news, industry insights, and developer trends. Alibaba Cloud website:https://www.alibabacloud.com

More from Medium

Recipe OSS: How to process images? (Alibaba Cloud)

Patching of Windows and Linux EC2 Instances using AWS SSM

Terraform in Action