Beginning Ansible in 5 Minutes

Push VS Pull

So let me show you how easy it is to get started with Ansible.

Install

Let’s assume you’ll use pip to get this done :

$ sudo easy_install pip
$ sudo pip install ansible

Make sure it installed by running ansible --version.

Concepts

You’ll often hear that Ansible is agent-less and uses a push approach (as opposed to pull).

In a nutshell, Chef or Puppet work by installing an agent on the hosts they manage. This agent is pulling changes from a

master host, using their own channel (usually not SSH).

Push VS Pull

Ansible on the other hand is simply using SSH to push changes from wherever it runs (a server or your own laptop).

Conceptually, it’s as if instead of connecting to your machines with SSH and running commands manually, you could

script the whole thing and run it automatically.

Ansible VS SSH

We’ll get familiar below with more of Ansible’s concepts: Inventories, Playbooks, Roles and Tasks.

Adding a host with an inventory

The first thing for us to do once Ansible is installed is to specify which hosts we want to manage.

  1. Add a new machine; Fedora, Ubuntu or CentOS will do
  2. Create a folder where you’ll keep the Ansible related code for this example. In this folder, add a file named hosts  following content:
     $ web ansible_ssh_host=198.199.115.67
    
  3. That’s it. Let’s just make sure this works. Run the following command:
     $ ansible -i hosts all -m ping -u root -k -v
    

    You should get something like that:

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

This file is called an inventory, it lists the hosts that you will be managing with Ansible.

Installing NGINX with roles

Go to the folder where you created your inventory in, create a roles/ subfolder and then run command:

$ ansible-galaxy install jdauphant.nginx -p roles

It will install the NGINX role in the roles subfolder, making it available to Ansible when ran from this folder.

Now, we’re gonna create our first playbook: playbook is a key concept in Ansible. It defines what needs to be configured and executed on your hosts.

Add a file named deploy.yml in the same folder as your inventory with the following content:

---
- hosts: all
  roles:
    - role: jdauphant.nginx
      nginx_http_params:
        - sendfile on
        - access_log /var/log/nginx/access.log
      nginx_sites:
         abc.com:
           - listen 80
           - server_name _
           - root /var/www/abc.com/_site
           - location / { try_files $uri $uri/ /index.html; }

We’re now ready to apply this to our host. Just run the following:

$ ansible-playbook deploy.yml -i hosts -u root -k -v

Ansible will spurt out logs while running your playbook and should finally display that all tasks were run successfully (“ok”):

PLAY RECAP **************************************************************
web                  : ok=15   changed=10   unreachable=0    failed=0  

Now, if you point your browser at your URL, you’ll get a 404 error from NGINX since we haven’t deployed our site yet. This however means that NGINX is indeed up and running (great success!).

Leave a Reply

Your email address will not be published. Required fields are marked *