×
Community Blog How to Install and Use Ansible on Ubuntu 16.04

How to Install and Use Ansible on Ubuntu 16.04

Ansible is a free and open source configuration management system allowing you to deploy and manage your applications.

By Hitesh Jethva, Alibaba Cloud Tech Share Author

Ansible is a free and open source configuration management system that allows you to automatically deploy and centrally manage your applications. You can easily manage and control large numbers of servers from central location using Ansible. Ansible is a great alternative solution of Chef and Puppet because it has a much smaller overhead to get started. Ansible uses SSH channels to retrieve information from remote system, so you does not require any additional software to be installed on the client computers.

This tutorial will walk you through the step by step instruction of how to install and use Ansible on an Alibaba Cloud ECS Ubuntu 16.04 server.

Prerequisites

  • Two Alibaba Cloud ECS instance with Ubuntu 16.04 installed.
  • A static IP address 192.168.0.103 is configured on server node.
  • A static IP address 192.168.0.104 is configured on client node.
  • Root password is configured on each node.

Launch Alibaba Cloud ECS instance

First, Login to your https://ecs.console.aliyun.com/?spm=a3c0i.o25424en.a3.13.388d499ep38szx">Alibaba Cloud ECS Console. You will be redirected to the main dashboard. You will need to navigate to your ECS instance by selecting the appropriate region. For this tutorial, I have created my ECS instances in the Singapore region.

1

If you haven't created an instance, you can check out this tutorial or follow the steps on this quick start guide. You will need to have two running ECS instances, preferably on the same region.

Note that I have set up my ECS instances with SSH key pairs as credential. This allows me to connect to the instances using SSH.

Install Ansible

By default, Ansible is not available in Ubuntu 16.04 repository. So you will need to add Ansible personal repository to on the server node. You can add the repository using the following command:

apt-add-repository ppa:ansible/ansible

You should see the following output:

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 will need to update the system so that repository will be added to the system. Run the following command to update the system:

apt-get update -y

Once the repository is updated, install Ansible with the following command:

apt-get install ansible -y

Configure Ansible Host

Ansible keeps track of all of the server nodes and client nodes using a hosts file. So you will need to configure this file before communicate with our other nodes.

First, open the file with the following command:

nano /etc/ansible/hosts

You should see all the commented lines. Here, you will need to add all the client nodes which you want to manage as shown below:

[servers]
client-node ansible_ssh_host=192.168.0.104

Save and close the file when you are finished.

Setup SSH Keys for Remote Hosts

Ansible uses SSH to communicate with other nodes. So you will need to generate an SSH key on server node and copy the ssh keys to the client node.

On the server node, run the following command to create an SSH key:

ssh-keygen -t rsa -b 4096

You should see the following output:

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 created key to the client node with the following command:

ssh-copy-id root@192.168.0.104

Next, perform an ssh key authentication on client node to check whether authentication working or not.

ssh root@192.168.0.104

You should not be prompted for a password if you have set this up correctly.

Test Ansible

Once everything is configured properly, it's time to test Ansible.

First, run the following command to ping all the client node which you have specified in hosts file:

ansible –m ping all

If your client node is up, then you should see the following output:

client-node | SUCCESS => {
    "changed": false, 
    "failed": false, 
    "ping": "pong"
}

Note: "all" means if you have specify multiple host in hosts file then the above command execute ping command on all the hosts.

You can also execute ping command on single host or group of hosts with the following command:

ansible -m ping client-node
ansible -m ping servers

If you want to find out the memory usage on client node, run the following command:

ansible -m shell -a 'free -m' client-node

You should see the following output:

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

Conclusion

Congratulations! You have successfully configured Ansible on your Alibaba Cloud ECS server. You can now easily manage multiple server by executing single command from central location.

0 0 0
Share on

Alibaba Clouder

2,605 posts | 747 followers

You may also like

Comments

Alibaba Clouder

2,605 posts | 747 followers

Related Products

  • Cloud Config

    A configuration audit service that provides configuration history of enterprise resources in Alibaba Cloud and audits the compliance of resource configurations.

    Learn More
  • Cloud Migration Hub

    Cloud Migration Hub (CMH) provides automatic and intelligent system surveys, cloud adoption planning, and migration management for you to perform migration to Alibaba Cloud.

    Learn More
  • Organizational Data Mid-End Solution

    This comprehensive one-stop solution helps you unify data assets, create, and manage data intelligence within your organization to empower innovation.

    Learn More
  • ChatAPP

    Reach global users more accurately and efficiently via IM Channel

    Learn More