How to Install and Use Ansible on Ubuntu 16.04

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 ECSUbuntu 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.

Image for post
Image for post

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:

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:

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:

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:

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:

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:

To check the partition size of the client node, run the following command:

ansible -m shell -a 'df -h' client-node

Output:

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.

Reference:

https://www.alibabacloud.com/blog/how-to-install-and-use-ansible-on-ubuntu-1604_583003?spm=a2c41.11457899.0.0

Written by

Follow me to keep abreast with the latest technology news, industry insights, and developer trends.

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