What is Ansible?


Ansible is a language to describe infrastructure expectations.

It's human readable.

It has agent-less architecture, it just needs ssh + python interpreter.

It's an automation engine that runs playbooks on the machines specified in the inventory.

You can run it from the command line like:

ansible-playbook -i inventory playbook.yml


File where you declare the list of hosts that you want to match the expectations declared in a playbook.

You can specify meaningful groups of hosts in order to decide what systems you are controlling at what times and for what purpose.

You can specify group variables or host variables that will help to control how ansible interacts with remote hosts and they will be available later in playbooks.

aws01 ansible_ssh_host=





Playbook - Roles, tasks and modules

A playbook is a yml file where you describe the desired state of a host or a group of hosts declared in the inventory.

Ansible ships with a list of modules.

E.g: File module

You can create a task using a module that satisfy you necessity.

E.g: Create a file in a given folder, with specific user and permissions.

- file: path=/etc/foo.conf owner=foo group=foo mode=0644

You can encapsulate a group of meaningfully related tasks in a role.

E.g: Create files and running services for configuring a Weave

You can apply roles to hosts in the playbook.

- hosts: weave_servers
    - weave

Playbook -> role -> task -> module


Ansible provides a mechanisim for overriding variables.

You can go deeply on this but a useful guideline to beging with is:

cli extra-vars -> host_vars/hostname.yml -> group_vars/group_name.yml -> group_vars/all.yml -> role defaults


Templates are a powerful resource for generating files on the hosts

Templates are processed by the Jinja2 templating language

A template will have a common structure and it will be populated with specify variable values at runtime.


WEAVE_PEERS="{{ weave_launch_peers }}"
WEAVEPROXY_ARGS="{{ weave_proxy_args }}"
WEAVE_ROUTER_ARGS="{{ weave_router_args }}"
# Uncomment and make it more secure
# WEAVE_PASSWORD="aVeryLongString"

Conditionals and loops

You can run roles or tasks depending on a conditional statement.

  - name: "shut down Debian flavored systems"
    command: /sbin/shutdown -t now
    when: ansible_os_family == "Debian"
    # note that Ansible facts and vars like ansible_os_family can be used
    # directly in conditionals without double curly braces


Rolling upgrades

You can run ansible in serial and have control on how many servers you want to run it at one time.


Gather useful variables about remote hosts that can be used in playbooks:

ansible all -i inventory -m setup

Check your machines are reachable. This returns pong on successful contact:

ansible all -i inventory -m ping

Run a bash command on the machines remotely:

ansible all -i inventory -a ls

See how your playbook will apply to the your hosts:

ansible-playbook --list-host -i inventory playbook.yml

Print all variables/facts known for a host by adding this into your playbook:

 - hosts: all
   - name: Display all variables/facts known for a host
     debug: var=hostvars

You can also run Ansible in "check mode"

results matching ""

    No results matching ""