DevOps/Ansible

From Wiki

ansible (science fiction) A hypothetical device that enables users to communicate instantaneously across great distances; that is, a faster-than-light communication device. -- Wikitionary


Ansible basically runs commands on remote machines using SSH. Hence, doesn't need an agent running on the remote machine (like Chef or Puppet).

Ansible is based on a declarative syntax. Tasks are specified in terms of the end results that you expect in contrast to shell scripting where you specify how to get there. Ansible tasks are also idempotent by default. They can thus be executed any number of times with the same effect.

A task in Ansible might looks like this:

  
- apt: 
  name: "apache2"
  state: present

Programming Languages

Ansible basically uses a combination of two templating languages which also double up as primitive programming languages with conditionals, lists and loops. They are YAML and Jinja2. There are some best practices on what feature to use from which language. There are multiple ways of doing things in the same language as well. For example, YAML has two ways of representing lists. Sometimes, warnings will be thrown expecting the user to use one form over the other.

    • Note:** Use a good text editor or the YAML indentation errors will drive you nuts. The one that you use for Python or Ruby should be good.

Playbooks

Playbooks: Playbooks are Ansible’s configuration, deployment, and orchestration language. Sometimes a playbook can be a single //yml //file. It has a bunch of key-value pairs in it. Tasks are also specified similarly. The most basic task is a shell command.

Tasks can be included into playbooks using include_task or entire roles can be included using include_role. Since it's all key-value pairs in YAML, seperating out sections into files helps with readability. Writing a role and creating a playbook out of it seems to be the most organized approach.

Roles

A role has the following directory structure:

 
roles/
   common/
   tasks/
   handlers/
   files/
   templates/
   vars/
   defaults/
   meta/

Roles can be included into playbooks and run. They are a better way of organizing your Ansible project. Configuration with a proper convention I suppose.

Ansible expects things to be in certain directories by convention. For example, a main.yml file is expected in the tasks directory which contains the tasks.

main.yml is necessary in each directory. Other files can be referred from main.yml

Terminology

  • Inventory
  • Play