Today I learned

Get started with Ansible in 2 minutes

This post has been written in 2013.

Ansible is a fantastic tool for provisioning servers. I personally prefer it over Chef, Puppet and Salt. Here's how to get an Ansible project started.

Install Ansible

Ansible is officially available via pip.

brew install ansible            # OSX
[sudo] pip install ansible      # elsewhere

Start your project

Make the directory. Put this under version control, preferrably.

~$ mkdir setup
~$ cd setup

Create an inventory file

This is a list of hosts you want to manage, grouped into groups. (Hint: try using to deploy to your local machine)

# ~/setup/hosts


Create your first Playbook

A playbook is just a YAML file.

# ~/setup/playbook.yml

- hosts:
  user: root
    - name: install nginx
      apt: pkg=nginx state=present

    - name: start nginx every bootup
      service: name=nginx state=started enabled=yes

    - name: do something in the shell
      shell: echo hello > /tmp/abc.txt

    - name: install bundler
      gem: name=bundler state=latest

Run it

Use the ansible-playbook command.

~/setup$ ls
~/setup$ ansible-playbook -i hosts playbook.yml
PLAY [all] ********************************************************************

GATHERING FACTS ***************************************************************
ok: []

TASK: [install nginx] *********************************************************
ok: []

TASK: [start nginx every bootup] **********************************************
ok: []

Further reading

Ansible's source is available via GitHub: ansible/ansible.

You have just read Get started with Ansible in 2 minutes, written on November 27, 2013. This is Today I Learned, a collection of random tidbits I've learned through my day-to-day web development work. I'm Rico Sta. Cruz, @rstacruz on GitHub (and Twitter!).

← More articles