Ansible is awesome!

Personally, I'm a big fan of Ansible. Where other deployment technologies works on a pull model, ansible pushes out deployments to the targets in question.

In this Guide, I will show you how to setup Ansible and will also be demonstrating how to deploy 5 LAMP Servers using Ansible.

Dependencies:

$ yum update -y
$ yum git python-devel python-setuptools -y
$ easy_install pip
$ pip install ansible

More about Ansible:

Ansible uses Playbooks that is written in YAML language, which makes it easy to compile.

Ansible uses SSH for the deployment, so with that being said, we need to make sure we can SSH to our nodes without specifying authentication.

Ansible uses a inventory file which is located at /etc/ansible/hosts which contains information of the targeted systems.

In our example we will setup a group WEB-SERVERS and assign 5 hosts to the group:

Prepare Host Inventory:

$ mv /etc/ansible/hosts{,.bak}

$ cat > /etc/ansible/hosts << EOF
[WEB-SERVERS]
192.168.1.11
192.168.1.12
192.168.1.13
192.168.1.14
192.168.1.15
EOF

Ensure Passwordless SSH:

You can follow this guide to get that sorted.

Create index.html:

We will create a index.html document which will be hosted on our Ansible server and when the deployment gets executed, this index document will be copied to the target nodes.

cat > /etc/ansible/content/lamp/index.html << EOF
<!DOCTYPE html>
<html>
<body>
<h1>Deployed with Ansible</h1>
</body>
</html>
EOF

Playbook:

Now to create our playbook, the playbook is like a descriptive or instruction for Ansible.

$ mkdir -p /etc/ansible/playbooks

cat > /etc/ansible/playbooks/deploy-lamp.yml << EOF
---
# Setup LAMP Stack
- hosts: WEB-SERVERS
  tasks:
    - name: install lamp stack
      sudo: yes
      yum: name={{ item }} state=present
      with_items:
        - httpd
        - httpd-devel
        - mysql
        - mysql-server
        - mysql-devel
        - php
        - php-devel
        - php-mysql

    - name: start services
      service: name={{ item }} state=running enabled=yes
      sudo: yes
      with_items:
        - httpd
        - mysqld

    - name: deploy index.html
      sudo: yes
      copy: src=/etc/ansible/content/lamp/index.html dest=/var/www/html/index.html
EOF

Deployment Time!

$ ansible-playbook /etc/ansible/playbooks/deploy-lamp.yml

Output:

PLAY [web-servers] *************************************************************** 

GATHERING FACTS *************************************************************** 
ok: [web-servers]

TASK: [install lamp stack] **************************************************** 
changed: [ec2-prep] => (item=httpd,httpd-devel,mysql,mysql-server,mysql-devel,php,php-devel,php-mysql)

TASK: [start services] ******************************************************** 
changed: [web-servers] => (item=httpd)
changed: [web-servers] => (item=mariadb)

TASK: [deploy index.html] ***************************************************** 
changed: [web-servers]

PLAY RECAP ******************************************************************** 
web-servers                   : ok=5    changed=5    unreachable=0    failed=0   

And you're done! Have a look at their Github page for a couple of example playbooks.