Ansible

From Code Self Study Wiki
Jump to: navigation, search

Notes about Ansible:

See also DigitalOcean.

Handlers

Handlers call a task after another task has completed. In the following example, the "Start Nginx" handler runs after the "Install Nginx" task runs.

---
- hosts: all

  tasks: 
    - name: Install Nginx
      apt: pkg=nginx state=installed update_cache=true
      notify:
        - Start Nginx

  handlers:
    - name: Start Nginx
      service: name=nginx state=started

From the docs:

"Handlers are just like regular tasks in an Ansible playbook (see Tasks), but are only run if the Task contains a notify directive and also indicates that it changed something. For example, if a config file is changed then the task referencing the config file templating operation may notify a service restart handler. This means services can be bounced only if they need to be restarted. Handlers can be used for things other than service restarts, but service restarts are the most common usage."

Modules

File Module

The File module manages files. Example:

- hosts: all
  vars: 
    - website_dir: /var/www/mysite.dev/web

  tasks:
    - name: Create website directory
      file: dest={{ website_dir }} mode=775 owner=www-data group=www-data