Syed Humza Shah


Engineering leader. Likes coffee. Loves to travel. Runs on a combination of optimism and pragmatism.

Read more about him here.

Use Ansible to Provision Servers with YAML files

Chef and Puppet are names that are familiar to many RoR developers. These tools help you provision a server i.e. prepare it before you can start deploying your code on to them.

If you’re new to this process and you’ve tried following any tutorial online of the abovementioned tools, you’ll realise that they aren’t very easy to get into. A much friendlier alternative is Ansible.

Ansible provides you with ‘playbooks’ that are filled with ‘roles’. Each role is composed of numerous tasks. You design what each task is going to do e.g. update software, install things, compose files, etc.. Once your playbook is set up, you can execute it and watch all your tasks executed linearly on your servers.

You can specify multiple servers in a hosts-file / inventory-file, and have your servers grouped as particular ‘hosts’.

Each task you define is run on each server in parallel (using SSH), and once the task has been executed on all servers, the next task is run. The downside is obvious - this bottlenecks the speed to the slowest server; but the upside is that the state of the servers is (almost) always in sync.

You can additionally tag tasks to group them. In your run command, you can then choose to include or exclude specific tags, gaining more control over which actions are executed.

Another appealing aspect of Ansible is that it’s all basically YAML. Apart from the file where you specify hosts or the configuration file (unnecessary, in many cases), everything is a YAML file. The files for variables, tasks, playbooks, meta-data, etc. everything is in an easy-to-understand YAML format. Thus you need not be a good Ruby dev to expertly do things in Ansible (unlike Puppet and Chef IMO).

There are many other things I like about Ansible - modules, idempotence-oriented, setting variables through the CLI, people offering their created-roles to be used, the Ansible-Galaxy, specifying role dependencies, and much more. But for me it boils down to its easy learning curve. Beginner RoR devs, in particular, who are fed up of trying to follow the many Chef / Puppet guides will greatly appreciate the simplicity offered by setting up your server using plain old YAML files.

The features mentioned above are just the tip of the iceberg. You can get your hands dirty using the official documentation.

You can also check out my free-to-use Ansible project on GitHub - it will help you easily provision a server for deployments of a basic Rack application.