By Kalapriya Kolappan, Alibaba Cloud Tech Share Author. Tech Share is Alibaba Cloud's incentive program to encourage the sharing of technical knowledge and best practices within the cloud community.
"You're either the one that creates the automation or you're getting automated," Tom Preston-Werner
For the past few years, we have experienced many disruptions in business trends. This has led to build a constructive way for megatrends associated with emerging technologies and one such thing is 'AUTOMATION'. On this note, welcome to the decade of automation. We want everything to be done on time. Despite doing all jobs manually, we want our daily routines to get automated. Hence, we want to sharpen our knowledge to avoid doing tasks in the nick of time. This could be achieved by Ansible efficient code which replaces all our day to day activities. Perhaps, we need to have a solution in our local environment to make our life easier. But where to begin?
In this article, we will articulate about the concepts of automation, the benefits of Ansible, and how Ansible influences automation in cloud computing, using Alibaba Cloud Elastic Compute Service (ECS).
The world is moving towards automation. Automation with Ansible makes organizational operations easier while managing company-wide infrastructure. In this epoch of technology, we'll mainly focus on a bunch of reasons for why we think automation is a critical part in all IT enhancements.
When it comes to automation there are many options, we need to map to the look around and work accordingly. Everything has its own pros and cons, so strive hard to yield the best out of it.
Ansible is a simple and powerful automation language. It helps to automate, accelerate, collaborate and integrate technologies that we already use. Ansible is used to perform the same set of tasks across different servers from the centralized server where Ansible is installed. One advantage of Ansible is completely agentless. It implies no agent needs to be installed on client systems where you want the automation to be done rather provided only SSH communication between client and server need to be established. Here we have some terms for better understanding. All this automation will be easier when there are hundreds of instance in the specified region.
Here are some common terms used when describing Ansible:
Control node: Machine where Ansible is installed and responsible for servers you are managing
Inventory: File that contains information about the servers
Playbook: The Main gateway for automation is defined in terms of tasks through YAML.
Ansible needs to be installed in your host environment.
Installation of Alibaba Cloud Modules
Alibaba cloud modules need to be installed in the control node. (i.e.) where ansible is installed.
We need the Alibaba cloud modules to be installed in ansible before using ansible with Alibaba. There are two ways to install alicloud provider ensure that ansible is already installed in the server.
sudo pip install ansible_alicloud
sudo pip install ansible_alicloud_module_utils
Modules will get installed in
Configuration file locations:
/etc/ansible ---- ansible.cfg
To ensure check the ansible version with the following command, it points to the currently used configuration file
[*****@***** ansible]# ansible --version
ansible 2.3.1.0
config file = /etc/ansible/ansible.cfg
configured module search path = Default w/o overrides
python version = 2.7.5 (default, Sep 5 2016, 02:30:38) [GCC 4.8.5 20150623 (Red Hat 4.8.5-9)]
Let's make our work much more interesting with a technical guide to get started with the following set of activities.
Ansible playbooks are more efficient when automating the same process for a large number of Virtual machines instead of repeating the tasks individually.
The first step is to create the playbook for starting the instance. The yaml code is as follows:
Note: Change the alicloud access key and a secret key of your cloud account
The yaml code is as follows:
- name: starting the instance example
hosts: localhost
vars:
alicloud_access_key: "xxxxxxxxx"
alicloud_secret_key:"xxxxxxxx"
alicloud_region: "ap-southeast-1"
instance_ids: "i-t4nelg6lkj5ycajgcaf2"
force: True
tasks:
- name: starting instance
alicloud_instance:
alicloud_access_key: "{{ alicloud_access_key }}"
alicloud_secret_key: "{{ alicloud_secret_key }}"
alicloud_region: "{{ alicloud_region }}"
instance_ids: "{{ instance_ids }}"
state: 'running'
The output of the code after running the playbook is as follows:
Before running the code, status of ECS is given below:
Executing the playbook is as follows:
[root@ogslab3 Alibaba]# ansible-playbook starting.yml
After running the playbook, the status of the ECS is as follows:
An ECS instance in Singapore region is in running state. The state of the ECS can be stopped using Ansible playbook as follows,
The yaml code for playbook is as follows:
- name: basic provisioning example
hosts: localhost
vars:
alicloud_access_key: "xxxxxxx"
alicloud_secret_key: "xxxxx"
alicloud_region: "ap-southeast-1"
instance_ids: "i-t4nelg6lkj5ycajgcaf2"
force: True
tasks:
- name: stopping the instance
alicloud_instance:
alicloud_access_key: "{{ alicloud_access_key }}"
alicloud_secret_key: "{{ alicloud_secret_key }}"
alicloud_region: "{{ alicloud_region }}"
instance_ids: "{{ instance_ids }}"
state: 'stopped'
Before executing the code:
Ansible output is as follows:
After running the playbook, the status of the instance in ecs console is as follows:
The yaml code is as follows (restart playbook):
- name: Restarting an instance
hosts: localhost
vars:
alicloud_access_key: "XXXXXXXXX"
alicloud_secret_key: "XXXXXXXXX"
alicloud_region: "ap-southeast-1"
instance_ids: "i-t4nelg6lkj5ycajgcaf2"
force: True
tasks:
- name: Restart
alicloud_instance:
alicloud_access_key: "{{ alicloud_access_key }}"
alicloud_secret_key: "{{ alicloud_secret_key }}"
alicloud_region: "{{ alicloud_region }}"
instance_ids: "{{ instance_ids }}"
state: 'restarted'
force: '{{ force }}'
Compiling the playbook make changes to the ECS instance in the console:
The ecs instance which is up and running will again get restarted in the console.
Playbook for terminating instance in ecs console is given below,
- name: Terminating the instance example
hosts: localhost
vars:
alicloud_access_key: "XXXXXX"
alicloud_secret_key: "xxxxx"
alicloud_region: "ap-southeast-1"
instance_ids: "i-t4n65f71bg34vbhz09if"
tasks:
- name: deleting instance
alicloud_instance:
alicloud_access_key: "{{ alicloud_access_key }}"
alicloud_secret_key: "{{ alicloud_secret_key }}"
alicloud_region: "{{ alicloud_region }}"
instance_ids: "{{ instance_ids }}"
force: True
state: 'absent'
register: Deleteresult
- debug: var=deleteresult
While executing this playbook we get the following changes.
After releasing the instance, it will not be reflected in the console.
When we are confused to know about the instance details, you can even fetch information about the instance using the below playbook.
instancefacts.yml
- name: fetch instance types example
hosts: localhost
vars:
alicloud_access_key: "XXXXX"
alicloud_secret_key: "XXXXXXX"
alicloud_region: "ap-southeast-1"
alicloud_zone: "ap-southeast-1a"
tasks:
- name: Find all instance types in the specified region
alicloud_instance_type_facts:
alicloud_access_key: '{{ alicloud_access_key }}'
alicloud_secret_key: '{{ alicloud_secret_key }}'
alicloud_region: '{{ alicloud_region }}'
register: all_instance_types
- debug: var=all_instance_types
- name: Find all instance types based on the specified zone
alicloud_instance_type_facts:
alicloud_access_key: '{{ alicloud_access_key }}'
alicloud_secret_key: '{{ alicloud_secret_key }}'
alicloud_region: '{{ alicloud_region }}'
alicloud_zone: '{{ alicloud_zone }}'
register: instance_types_by_zone
- debug: var=instance_types_by_zone
- name: Find all instance types based on the specified family names
alicloud_instance_type_facts:
alicloud_access_key: '{{ alicloud_access_key }}'
alicloud_secret_key: '{{ alicloud_secret_key }}'
alicloud_region: '{{ alicloud_region }}'
instance_type_families:
- "ecs.t5"
register: instance_types_by_families
- debug: var=instance_types_by_families
- name: Find all instance types based on the specified ids
alicloud_instance_type_facts:
alicloud_access_key: '{{ alicloud_access_key }}'
alicloud_secret_key: '{{ alicloud_secret_key }}'
alicloud_region: '{{ alicloud_region }}'
instance_type_ids:
- "ecs.t5-lc2m1.nano"
register: instance_types_by_ids
- debug: var=instance_type_ids
From the above playbook, it fetches the instance type as follows,217 types of instances are available.
"instance_types": [
{
"cpu_core_count": 1,
"id": "ecs.t1.xsmall",
"instance_type_family": "ecs.t1",
"memory_size": 0.5
},
{
"cpu_core_count": 1,
"id": "ecs.t1.small",
"instance_type_family": "ecs.t1",
"memory_size": 1.0
}…….
{
"cpu_core_count": 4,
"id": "ecs.sn2ne.xlarge",
"instance_type_family": "ecs.sn2ne",
"memory_size": 16.0
},
The second module of the playbook fetch the types of instances available in the specified zone.
135 types of instance with their specifications are listed below:
"instance_type_ids": [
"ecs.sn2ne.large", "ecs.sn2ne.xlarge",
"ecs.sn2ne.2xlarge", "ecs.sn2ne.4xlarge"….. "ecs.n4.8xlarge"
The third module of the playbook fetch the different types of instances available in the specified family.
Say here ecs.t5 is taken as input, based on the CPU core count 16 varieties of the instance are there in the specified family.
{
"cpu_core_count": 1,
"id": "ecs.t5-lc1m2.small",
"instance_type_family": "ecs.t5",
"memory_size": 2.0
},
{
"cpu_core_count": 2,
"id": "ecs.t5-lc1m2.large",
"instance_type_family": "ecs.t5",
"memory_size": 4.0
},
{
"cpu_core_count": 2,
"id": "ecs.t5-lc1m4.large",
"instance_type_family": "ecs.t5",
"memory_size": 8.0
}
The fourth module of the playbook fetch the different types of instances based on the instance type ids.
When additional storage needs to be attached to the instance created, we can create a disk through Ansible.
The below playbook depicts the way for creating a disk in the specified region.
createdisk.yml
- name: create disk
hosts: localhost
connection: local
vars:
alicloud_access_key: "XXXXXXXX"
alicloud_secret_key: "XXXXXXXXX"
alicloud_region: "ap-southeast-1"
alicloud_zone: "ap-southeast-1b"
disk_name: "Testing"
disk_category: "cloud_efficiency"
size: 50
disk_tags:
- tag_key: create_test_1
tag_value: '0.01'
state: present
tasks:
- name: create disk
alicloud_disk:
alicloud_access_key: '{{ alicloud_access_key }}'
alicloud_secret_key: '{{ alicloud_secret_key }}'
alicloud_region: '{{ alicloud_region }}'
alicloud_zone: '{{ alicloud_zone }}'
disk_name: '{{ disk_name }}'
disk_category: '{{ disk_category }}'
size: '{{ size }}'
disk_tags: '{{ disk_tags }}'
state: '{{ state }}'
register: creatediskresult
- debug: var=creatediskresult
When executing the playbook for creating disk,
The disk named testing have been created is reflected in the console.
This playbook will explain how to attach the disk to the specified instance.
- name: Adding a disk to an ECS instance
hosts: localhost
vars:
state: present
alicloud_access_key: "XXXXXXXXXX"
alicloud_secret_key: "XXXXXXXXX"
alicloud_region: "ap-southeast-1"
alicloud_zone: "ap-southeast-1b"
instance_id: "i-t4nh8623mgcdfbcacqi2"
disk_id: "d-t4n3s6w46jbd5gyxeqwk"
delete_with_instance: no
tasks:
- name: Attaching disk
alicloud_disk:
state: '{{ state }}'
alicloud_access_key: '{{ alicloud_access_key }}'
alicloud_secret_key: '{{ alicloud_secret_key }}'
alicloud_region: '{{ alicloud_region }}'
alicloud_zone: '{{ alicloud_zone }}'
instance_id: '{{ instance_id }}'
disk_id: '{{ disk_id }}'
delete_with_instance: '{{ delete_with_instance }}'
register: attachingdiskresult
- debug: var=attachingdiskresult
The screenshots depicts the output for executing this playbook and getting reflected in the console.
This playbook describes the details to be gathered about a disk.
- name: fetch disk details example
hosts: localhost
vars:
alicloud_access_key: "XXXXXXXX"
alicloud_secret_key: "XXXXXXXXXX"
alicloud_region: "ap-southeast-1"
alicloud_zone: "ap-southeast-1b"
tasks:
- name: Find all disks in the specified region
alicloud_disk_facts:
alicloud_access_key: '{{ alicloud_access_key }}'
alicloud_secret_key: '{{ alicloud_secret_key }}'
alicloud_zone: "{{ alicloud_zone }}"
alicloud_region: "{{ alicloud_region }}"
register: all_disks
- debug: var=all_disks
- name: Find all disks based on the specified ids
alicloud_disk_facts:
alicloud_access_key: '{{ alicloud_access_key }}'
alicloud_secret_key: '{{ alicloud_secret_key }}'
alicloud_zone: "{{ alicloud_zone }}"
alicloud_region: "{{ alicloud_region }}"
disk_ids:
- "d-t4n3s6w46jbd5gyxeqwk"
- "d-t4n9gyct1a1r21xx4s6v"
register: disks_by_ids
- debug: var=disks_by_ids
- name: Find all disks based on the specified names/name-prefixes
alicloud_disk_facts:
alicloud_access_key: '{{ alicloud_access_key }}'
alicloud_secret_key: '{{ alicloud_secret_key }}'
alicloud_zone: "{{ alicloud_zone }}"
alicloud_region: "{{ alicloud_region }}"
disk_ids:
- "d-t4n3s6w46jbd5gyxeqwk"
- "d-t4n6311wylqzlp9kjr5x"
disk_names:
- "Testing"
register: disks_by_names
- debug: var=disks_by_names
Thus, we have explored about automation with cloud infrastructure focused mainly on Alibaba Cloud Elastic Compute Service (ECS). We have seen about the following starting, stopping, deleting and restarting instance in the specified region, fetching and gathering facts about the instance in the specified region and creating disk and attaching the disk to a specific instance. For further details about automation cloud refer to the upcoming articles.
Building Very Fast App Backends with Falcon Web Framework on PyPy
November Announcement of Alibaba Cloud Community Builder Program
2,599 posts | 763 followers
FollowAlibaba Clouder - April 19, 2019
Alibaba Clouder - September 7, 2020
iilness - March 17, 2020
Alibaba Cloud Community - June 12, 2023
Alibaba Clouder - August 31, 2020
Alibaba Clouder - October 12, 2019
2,599 posts | 763 followers
FollowElastic and secure virtual cloud servers to cater all your cloud hosting needs.
Learn MoreAlibaba Cloud Function Compute is a fully-managed event-driven compute service. It allows you to focus on writing and uploading code without the need to manage infrastructure such as servers.
Learn MoreCloud-based and lightweight servers that are easy to set up and manage
Learn MoreMore Posts by Alibaba Clouder
Raja_KT March 20, 2019 at 4:44 pm
Nice sharing. Examples are good and we can copy and paste the codes.