How to Provision Alibaba Cloud Resources with Ansible

Introduction

Comparison of Provisioning Methods

What Is Ansible and How Does It Support IaaS and the Cloud?

Setting Up the Test Environment

Install Ansible

yum -y install \ https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
yum -y install ansible

Install Alicloud Ansible module

yum -y install python2-pip
pip install ansible_alicloud

Go through a Deployment Example

Define the Playbook

Run the Results

ansible-playbook $FILENAME.yml

Command Error during Playbook Execution

NTP Time Lag

TASK [make sure ingress port 22 and 80 are allowed] ******************************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"changed": false, "msg": "Error in get_all_security_groups: ECSResponseError: , HTTP Status: 400 Error:IllegalTimestamp The input parameter \"Timestamp\" that is mandatory for processing this request is not supplied. RequestID: 9FAC5E19-46E3-4A72-A879-422A691585BC,\nNone"}
to retry, use: --limit @/root/test.retry

Appendix

Example Playbook

- name: basic provisioning example
hosts: localhost
vars:
alicloud_access_key: YOUR_ACCESS_KEY_HERE # replace it with your Alicloud Access Key
alicloud_secret_key: YOUR_SECRET_KEY_HERE # replace it with your Alicloud Secret Key
alicloud_region: cn-hongkong # replace it with the region you plan to use
image: centos_7_02_64_20G_alibase_20170818.vhd # replace it with the image to use
instance_type: ecs.n4.small # replace it with the flavor type
vswitch_id: vsw-YOUR_VSWITCH_ID_HERE # replace it with the Vswitch ID
assign_public_ip: True # set to false in case you dont need public ip
max_bandwidth_out: 10
host_name: PREFERRED_HOSTNAME # hostname
system_disk_category: cloud_efficiency
system_disk_size: 50
internet_charge_type: PayByTraffic
group_id: sg-YOUR_SG_ID_HERE # replace with the security-group-id
sg_action: join
key_name: SSH_KEY_NAME # replace with the ssh key
count: 2 # number of instances to create
user_data: |
#!/bin/sh
yum -y install httpd
systemctl start httpd
echo "Server Up at $(date -R)!" | tee /var/www/html/index.html
tasks:
- name: make sure ingress port 22 and 80 are allowed
alicloud_security_group:
alicloud_access_key: '{{ alicloud_access_key }}'
alicloud_secret_key: '{{ alicloud_secret_key }}'
group_id: '{{ group_id }}'
alicloud_region: '{{ alicloud_region }}'
rules:
- ip_protocol: tcp
port_range: 22/22
source_cidr_ip: '0.0.0.0/0'
policy: accept
- ip_protocol: tcp
port_range: 80/80
source_cidr_ip: '0.0.0.0/0'
policy: accept
- name: launch ECS instance in VPC network
alicloud_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 }}'
vswitch_id: '{{ vswitch_id }}'
assign_public_ip: '{{ assign_public_ip }}'
internet_charge_type: '{{ internet_charge_type }}'
max_bandwidth_out: '{{ max_bandwidth_out }}'
group_id: '{{ group_id }}'
key_name: '{{ key_name }}'
host_name: '{{ host_name }}'
#password: '{{ password }}'
user_data: '{{ user_data }}'
count: '{{ count }}'
instance_tags:
Name: created_through_ansible
register: status
- name: Poll instance information
alicloud_instance_facts:
alicloud_access_key: '{{ alicloud_access_key }}'
alicloud_secret_key: '{{ alicloud_secret_key }}'
alicloud_region: '{{ alicloud_region }}'
instance_ids: '{{ status.instance_ids }}'
register: all_instances
- set_fact:
instances_info: []
- set_fact:
instances_info: "{{ instances_info + [ { 'id': item.id, 'host_name': item.host_name, 'public_ip': item.public_ip }] }}"
with_items: "{{ all_instances.instances }}"
no_log: true

- name: print instance information
debug:
msg: "ECS instance {{ item.host_name }} created, id: {{ item.id }}. IP: {{ item.public_ip }} . URL http://{{ item.public_ip }}/"
with_items: "{{ instances_info }}

Example Task Definition to Create VPC

- name: create vpc
hosts: localhost
connection: local
vars:
alicloud_access_key: YOUR_ACCESS_KEY_HERE # replace it with your Alicloud Access Key
alicloud_secret_key: YOUR_SECRET_KEY_HERE # replace it with your Alicloud Secret Key
alicloud_region: cn-hongkong
state: present
cidr_block: 192.168.0.0/16
vpc_name: Demo_VPC
description: Demo VPC
tasks:
- name: create vpc
alicloud_vpc:
alicloud_region: '{{ alicloud_region }}'
state: '{{ state }}'
cidr_block: '{{ cidr_block }}'
vpc_name: '{{ vpc_name }}'
description: '{{ description }}'

Example Task Definition to Create RDS

- name: create rds instance
hosts: localhost
vars:
alicloud_access_key: <your-alicloud-access-key-id>
alicloud_secret_key: <your-alicloud-access-secret-key>
alicloud_region: cn-beijing
tasks:
- name: create instance
alicloud_rds_instance:
alicloud_access_key: '{{ alicloud_access_key }}'
alicloud_secret_key: '{{ alicloud_secret_key }}'
alicloud_region: '{{ alicloud_region }}'
state: present
engine: MySQL
engine_version: 5.6
instance_class: rds.mysql.t1.small
instance_storage: 30
instance_net_type: Internet
security_ips: 10.23.12.24/24
instance_charge_type: Postpaid

Example Task Definition to Create SLB

- name: create server load balancer
hosts: localhost
connection: local
vars:
alicloud_region: cn-beijing
alicloud_access_key: <your-alicloud-access-key-id>
alicloud_secret_key: <your-alicloud-access-secret-key>
load_balancer_name: demo_slb
internet_charge_type: paybytraffic
state: present
tasks:
- name: create server load balancer
alicloud_slb_lb:
alicloud_access_key: '{{ alicloud_access_key }}'
alicloud_secret_key: '{{ alicloud_secret_key }}'
alicloud_region: '{{ alicloud_region }}'
load_balancer_name: '{{ load_balancer_name }}'
internet_charge_type: '{{ internet_charge_type }}'
state: '{{ state }}'

Example Skeleton to Create VPC, RDS and ECS

- name: create server stack
hosts: localhost
connection: local
vars:
alicloud_region: cn-beijing
alicloud_access_key: <your-alicloud-access-key-id>
alicloud_secret_key: <your-alicloud-access-secret-key>
.. line skipped ..
tasks:
- name: create VPC
alicloud_vpc:
.. line skipped ..
- name: create RDS
alicloud_rds_instance:
.. line skipped ..
- name: create ECS
alicloud_instances:
.. line skipped ..

--

--

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