How to use Ansible to Setup an App Server within 5 mins

by Aamir Bhatt

As a software developer, a lot of time is taken up in the process of setting up a server. In spite of knowing all server setup requirements, going through ssh, using vim editors for file creating/editing , installing each package and verifying the installations consumes valuable time.

Ansible is a simple IT automation engine that automates cloud provisioning, configuration management, application deployment, intra-service orchestration, and many other IT needs.

While setting up a server we focus on the  following: 

  • Installing all  global software packages like mysql, git, mysql-server,apache etc.
  • Cloning project from git
  • Installing virtualenv, creating environment  for project and then installing requirements into environment
  • Creating database and importing its fixtures
  • Some file transfer
  • Configuring server  settings

Now we are going to see how we can use Ansible to automate the above process, and use it across multiple servers; essentially you write the Ansible  script one-time but and re-use for all new server setups going forward.

Step 1. Install ANSIBLE:

UBUNTU:

sudo pip install ansible

MAC:

brew install ansible

In addition, the below python packages can also be used to run Ansible

  • paramiko
  • PyYAML
  • jinja2

Step 2. Writing your first Playbook

Ansible uses a very simple language (YAML, in the form of Ansible Playbooks) that allows you to describe your automation jobs in a way that approaches plain English. In a playbook you define what actions Ansible should take to get the server in the state you want. Only what you define gets done. 

Ansible Terminologies:

  • YAML – http://en.wikipedia.org/wiki/YAML
  • HOSTS – Ansible works against multiple systems in your infrastructure at the same time. It does this by selecting portions of systems listed in Ansible inventory file, which defaults to being saved in the location where we declare our host file.

In Host File, we declare the ip of the server. We can also group ips in one tag as:

Untitled drawing (1)

 

For host we can pass parameters like

user: root   # server user

sudo: yes    # is it super user

 

  •  VARIABLES – Variables are declared under var tag and these variables can be used  as per the scope of the variable. Ansible allows you to reference variables in your playbooks using the Jinja2 templating system. For example:

project_name: pa-platform              # variable declaration

{{project_path}}                          # variable Substitution

 

  • LOOPING – To save some typing, repeated tasks can be written in shorthand like,
- name: Install required system packages.
 apt: pkg={{ item }} state=installed update-cache=yes
 with_items:
   - libmysqlclient-dev
   - python-dev
   - gcc
   - python-setuptools
   - mysql-server
   - redis-server
   - git-core
   - mysql-client
   - python-setuptools
   - nginx
   - apache2
  • ACTION – Ansible ships with a number of modules (called the ‘module library’) that can be executed directly on remote hosts or through playbooks. Users can also write their own modules. These modules can control system  resources like services, packages, or files (anything really), or handle executing system commands.

From playbooks, Ansible modules are executed in a very similar way:

- name: install pip
action:  action: easy_install name=pip

Create deploy directory:

I usually add Ansible script in my project folder which I have to deploy. You can add it anywhere you like.

mkdir deploy
cd deploy
touch hosts
hosts file
             
[production]
192.100.100.12

add setup.yaml  file in deploy directory

This is your playbook, we can play it as per the need for server setup and deployment. The code below will help you set up a sample playbook. You can add the script as per your requirements.

touch setup.yaml
gedit setup.yaml
- hosts: all    
   user: root   # server user
   sudo: yes    # is it super user

   vars: # variable can be used throughout the script and it is 
      project_root: /srv/www
      environment_name : env_projectname
      project_name:abc
      repo_name:https://username:password@github.com/abc.git
      environment_path:/root/env_ir


   tasks:

   - name: install packages
     action: apt pkg=apache2 state=latest # this is equivalent to apt-get install apache2
     # we can also write 
     #apt: pkg={{item}}  state=installed update_cache=yes
     #instead of action directly apt
     
   - install other packages and looping through all packages
     action:apt pkg={{item}}  state=installed update_cache=yes
     with_items:
         - python-dev
         - gcc
         - python-setuptools
         - mysql-server
         - redis-server
         - git-core
         - mysql-client
         - python-setuptools
         - nginx
         - apache2
     # action is repeated each time with_items value substituting on item tag
     # apt-get install python-dev,apt-get install gcc etc

  - name: install pip
       action: easy_install name=pip

  - name: install various libraries with pip
       action: pip name={{item}} state=present
       with_items: 
           - virtualenv

     # likewise we can install all packages we need
   - name: clone project from git 
       git: repo={{project_repo}} dest={{project_root}}/{{project_name}} remote=origin version=master

   - name: create virtualenv with mysql-python installed in it
       pip: name=mysql-python virtualenv={{environment_path}}

   - name: change permissions
       shell: chmod 777 {{environment_path}}

   # shell means your command line

   - name: install dependencies into virtualenv
       action: pip requirements={{project_root}}{{project_name}}/requirements.txt virtualenv=/root/env_ir state=present

Running the yaml file:

ansible-playbook deploy/setup_server.yml -i deploy/hosts.example --user root 
--ask-pass
ansible-playbook:
	ansible playbook package

File to be run:
deploy/setup_server.yml
Host file :
deploy/hosts.example
--ask-pass:
	whether to ask password  to connect to the fresh server

Ansible is time efficient and reusable. It does not use any agents nor any additional custom security infrastructure, so it’s easy to deploy.

 

One thought on “How to use Ansible to Setup an App Server within 5 mins”

  1. I am using postgresql i am getting error on
    “- name: create virtualenv with mysql-python installed in it
    pip: name=mysql-python virtualenv={{environment_path}}”

    Please suggest me to create virtualenv

Leave a Reply

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

Tools & Practices

Tools and Technologies we use at Applied

Contact us now

Popular Posts