** Ansible ** is a free and open source configuration management system that allows you to deploy and manage your applications.
Alibaba Cloud Tech Share Author, Hitesh Jethva.
Ansible is a free open source configuration management system that allows you to automatically deploy and centrally manage your applications. With Ansible, you can easily manage and control a large number of servers from a central location. Ansible is a great alternative to Chef and Puppet because it has much less overhead to get started. Ansible uses SSH channels to get information from remote systems, so no additional software needs to be installed on the client computer.
This tutorial will show you step by step how to install and use Ansible on Alibaba Cloud ECS (https://www.alibabacloud.com/product/ecs?spm=a2c65.11461447.0.0.530e1dc7SjDKy5) Ubuntu 16.04 server.
--I have two Alibaba Cloud ECS instances with Ubuntu 16.04 installed. --The server node has a static IP address of 192.168.0.103. --The client node has a static IP address of 192.168.0.104. --A root password is set for each node.
First, log in to https://ecs.console.aliyun.com/?spm=a3c0i.o25424en.a3.13.388d499ep38szx"> Alibaba Cloud ECS Console. You will be redirected to the main dashboard. Appropriate You need to select a region and go to ECS Instances (https://www.alibabacloud.com/product/ecs?spm=a2c65.11461447.0.0.530e1dc7SjDKy5). In this tutorial, you are creating an ECS Instance (https://www.alibabacloud.com/product/ecs?spm=a2c65.11461447.0.0.530e1dc7SjDKy5) in the Singapore region.
If you haven't created an instance, check this tutorial (https://www.alibabacloud.com/blog/3-ways-to-set-up-a-linux-server-on-alibaba-cloud_572384?spm=a2c65.11461447.0.0.530e1dc7SjDKy5) or follow the steps in this Quick Start Guide (https://www.alibabacloud.com/help/doc-detail/25422.htm?spm=a2c65.11461447.0.0.530e1dc7SjDKy5). If possible, you should be running two ECS instances (https://www.alibabacloud.com/product/ecs?spm=a2c65.11461447.0.0.530e1dc7SjDKy5) on the same region.
Note that I have the SSH key pair set to ECS Instance (https://www.alibabacloud.com/product/ecs?spm=a2c65.11461447.0.0.530e1dc7SjDKy5) as my credentials. This allows you to connect to your instance using SSH.
By default, Ansible is not available in Ubuntu 16.04 repositories. Therefore, you need to add Ansible's personal repository on the server node. You can add the repository with the following command.
apt-add-repository ppa:ansible/ansible
You should see output similar to the following.
Ansible is a radically simple IT automation platform that makes your applications and systems easier to deploy. Avoid writing scripts or custom code to deploy and update your applications— automate in a language that approaches plain English, using SSH, with no agents to install on remote systems.
http://ansible.com/
More info: https://launchpad.net/~ansible/+archive/ubuntu/ansible
Press [ENTER] to continue or ctrl-c to cancel adding it
gpg: keyring `/tmp/tmpiylu9n1t/secring.gpg' created
gpg: keyring `/tmp/tmpiylu9n1t/pubring.gpg' created
gpg: requesting key 7BB9C367 from hkp server keyserver.ubuntu.com
gpg: /tmp/tmpiylu9n1t/trustdb.gpg: trustdb created
gpg: key 7BB9C367: public key "Launchpad PPA for Ansible, Inc." imported
gpg: Total number processed: 1
gpg: imported: 1 (RSA: 1)
OK
After adding the Ansible repository, you need to update your system so that the repository is added to your system. Run the following command to update your system.
apt-get update -y
After the repository is updated, install Ansible with the following command.
apt-get install ansible -y
Ansible uses the hosts file to keep track of all server and client nodes. Therefore, you need to configure this file before communicating with other nodes.
First, open the file with the following command.
nano /etc/ansible/hosts
You should see all the comment lines. Here you need to add all the client nodes you want to manage as follows:
[servers]
client-node ansible_ssh_host=192.168.0.104
Close the file when you are finished saving.
Ansible uses SSH to communicate with other nodes. Therefore, you need to generate an SSH key on the server node and copy the SSH key to the client node.
On the server node, execute the following command to generate an SSH key.
ssh-keygen -t rsa -b 4096
You should see output similar to the following.
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:3FvzXmI3EhW7idSZy+ITzW7UrefZP/R5AYx2VBu3ytU root@server-node
The key's randomart image is:
+---[RSA 4096]----+
| .+.|
| .. @|
| +. XE|
| . . oo+O *|
| S o +B.Oo|
| o.o*+ |
| . +*+B|
| o+O*|
| ..B|
+----[SHA256]-----+
Next, copy the key created by the following command to the client node.
ssh-copy-id [email protected]
Next, perform ssh key authentication on the client node and check if the authentication is successful.
If set correctly, you will not be prompted for a password.
Once everything is set up correctly, it's time to test Ansible.
First, run the following command to ping all the client nodes specified in the hosts file.
ansible -m ping all
If the client node is up, you should see output similar to the following:
client-node | SUCCESS => {
"changed": false,
"failed": false,
"ping": "pong"
}
Note: "all" means that if you specify multiple hosts in the hosts file, the above command will ping all hosts.
You can also ping a single host or host group with the following command:
ansible -m ping client-node
ansible -m ping servers
If you want to know the memory usage of the client node, execute the following command.
ansible -m shell -a 'free -m' client-node
You should see output similar to the following.
client-node | SUCCESS | rc=0 >>
total used free shared buffers cached
Mem: 3835 3185 649 177 77 929
-/+ buffers/cache: 2179 1656
Swap: 10793 0 10793
To check the partition size of the client node, run the following command.
ansible -m shell -a 'df -h' client-node
Output.
client-node | SUCCESS | rc=0 >>
Filesystem Size Used Avail Use% Mounted on
/dev/sda4 92G 27G 61G 31% /
none 4.0K 0 4.0K 0% /sys/fs/cgroup
udev 1.9G 4.0K 1.9G 1% /dev
tmpfs 384M 1.2M 383M 1% /run
none 5.0M 0 5.0M 0% /run/lock
none 1.9G 1.9M 1.9G 1% /run/shm
none 100M 44K 100M 1% /run/user
/dev/sda5 353G 75G 261G 23% /Data
Congratulations. Succeeded in setting Ansible in Alibaba Cloud ECS Server. Now you can easily manage multiple servers by running a single command from a central location.
Recommended Posts