How to manage Linux servers with Ansible - ClickIT

How to manage Linux Servers with Ansible

Little introduction to Ansible

Ansible is a simple way to automate apps and IT infrastructure. Do you need to deploy or create an environment several times? Ansible can be your best friend in these cases.

Ansible can automate cloud provisioning, configuration management, application deployment and many other IT needs.

A lot of modern companies are using ansible due to the fact that it requires minimum configuration from the server side, you just need SSH and you’re good to go. No agents or third party software are needed and also you can save a ton of work and hours thanks to the simplicity of it.

Unlike Puppet or Chef it doesn’t use an agent on the remote host. Instead Ansible uses SSH which is assumed to be installed on all the systems you want to manage. This means that you don’t have to setup a client server environment before using Ansible, you can just run it from any of your machines and from the client's point of view there is no knowledge of any Ansible server

Let's mention some case scenarios.

  • You wish to have multiple development Linux server (one per developer) in Amazon Web Services (AWS). With Ansible's help you can create one playbook that creates the Linux server in AWS, attach it to a VPC, install a LAMP stack (Linux Server, Apache, MySQL, PHP) and the latest version of your WordPress

NOTE: The following example was tested on Ubuntu 14.x

Installing Ansible

Install the latest version of Ansible on Ubuntu running the following commands:

$ sudo apt-add-repository -y ppa:ansible/ansible
$ sudo apt-get update
$ sudo apt-get install ansible

Hurray! Now Ansible is installed on your server. Simple, right? Now let’s start with configuring Ansible and do some basic testing.

How does Ansible work?

Ansible communicates over SSH to the remotes clients in order to gather information of the remote machines, run commands, copy or delete files and some other functions. Compared to Puppet and Cheff, Ansible can be very easy to use and compatible with almost all servers (as long as they have accessible SSH service for the Ansible host)

Ansible use Playbooks that are configurations, deployments and orchestration language. They are easy to read and easy to create which is a really cool feature for Ansible.

They can describe a policy you want your remote systems to enforce, or a set of steps for your remote or local server.

Getting Started!

Once Ansible is installed, the directory /etc/ansible will be created.This directory contains the mains files for Ansible’s configurations and playbooks.  We strongly recommend to have all the Ansible related files, configurations and playbooks inside this directory.

Once inside that directory you’ll see a file called hosts, in that file you can define host groups. For example, you can have 1 host group for web servers and another host group for databases.

Example of /etc/ansible/hosts file

[webservers]
alpha.example.org
beta.example.org
192.168.1.100
192.168.1.110
[dbservers]
db01.intranet.mydomain.net
db02.intranet.mydomain.net
10.25.1.56
10.25.1.57

You can also create aliases in the host groups like this

[tutorial]
clickit ansible_ssh_host=10.0.20.46

This host file allows you to run certain commands or playbooks to different servers’ group, which can be really handy at the time of having many Playbooks and functions.

On the remote Linux server we’re using a user called ‘ec2-user’. By default ansible will try to connect with the ‘root’ user, but you can modify that using flags in the command itself, or the vars in the playbooks

We will also define the private key with the flag --private-key to specify the pem file

$ ansible -m ping turial -u ec2-user --private-key=ansiblekey.pem

 

Ping output

clickit | success >> {
    "changed": false,
    "ping": "pong"
}

Seeing the ping output means that the connections is being successfully established.

Now we will be able to execute remote commands on the servers we are connecting. For example, we can use df -h command to check the disk usage

$ ansible -m shell -a 'df -h' tutorial -u ec2-user --private-key=ansiblekey.pem

 

Shell output

clickit | success | rc=0 >>
Filesystem      Size  Used Avail Use% Mounted on
/dev/xvda1      7.8G  1.5G  6.2G  20% /
devtmpfs        993M   56K  993M   1% /dev
tmpfs          1002M     0 1002M   0% /dev/shm

Now you should have the basic knowledge of Ansible and its functionalities connecting your Ansible server with your remote servers using commands.
But wait.. Ansible has much more to offer, and we haven’t used the most useful feature, which are the playbooks. We will talk about them more in the next post.

Here at ClickIT, we are experts providing Managed services with DevOps solutions such as Ansible, Chef and AWS Opswork. Our Agile Team also includes leading IT experts in application migration in the cloud, web security, web development, IT automation, clustering, scalability and near-shore support"

 

Tagged under: