Skip to content

Instantly share code, notes, and snippets.

@tbielawa
Created February 29, 2012 19:26
Show Gist options
  • Save tbielawa/1943782 to your computer and use it in GitHub Desktop.
Save tbielawa/1943782 to your computer and use it in GitHub Desktop.

ansible-modules(5)

NAME

ansible-playbook - format and function of an ansible playbook file

DESCRIPTION

Ansible ships with ansible-playbook, a tool for running playbooks. Playbooks can represent frequent tasks, desired system configurations, or deployment processes.

FORMAT

Example of the YAML format

- pattern: '*'
  hosts: '/etc/ansible/hosts'
  tasks:
  - name: configure template & module variables for future template calls
    action: setup http_port=80 max_clients=200
  - name: write the apache config file
    action: template src=/srv/httpd.j2 dest=/etc/httpd/conf
    notify:
    - restart apache
  - name: ensure apache is running
    action: service name=httpd state=started
  handlers:
    - name: restart apache
    - action: service name=httpd state=restarted

WHAT THE EXAMPLE MEANS

Here’s what the above example will do.

For all hosts in /etc/ansible/hosts (one host per line) that are named webserver-anything, first write a JSON file into /etc/ansible/setup on each remote system with the values max_clients and http_port.

Next, use a Jinja2 template locally residing at /srv/templates/httpd.j2 to write the Apache config file on each host to the path /etc/httpd.conf, using the previous values.

We’ll ensure that apache is running if stopped.

If and only if the config file changed, note that we need to restart apache at the end of the run, otherwise, don’t bother because we already know it is running.

HIGH LEVEL EXPLANATION

Playbooks are executed top down and can contain multiple references to patterns. For instance, a playbook could do something to all webservers, then do something to all database servers, then do something different to all webservers again.

For each pattern, the tasks in the tasks list are executed in order for all hosts in the host file matching the pattern.

For each task, a name/action pair describes what the task is and what ansible module to use to accomplish the task, along with any arguments. Additional fields like comment: can be added and will be ignored, so feel free to take notes in the file.

Most modules accept key=value format arguments.

Handlers are like tasks, but are conditionally executed. If a module reports a change, it can notify one or more handler by name. If notified, it will run only for hosts that changed.

ERROR HANDLING

If a host has a failure, the host will be ignored for the remainder of the playbook execution.

AUTHOR

Ansible was originally written by Michael DeHaan. See the AUTHORS file for a complete list of contributors.

SEE ALSO

ansible(1)

ansible-playbook(5) - pending

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment