February 14, 2015

Ansible: Playbooks

Use Ansible playbooks to run idempotent tasks on your servers.

Use Ansible playbooks to run idempotent tasks on your servers.We installed Ansible and then installed nginx on three servers, using Ansible's "apt" module.

The first thign we do here is run the same command again to install Nginx:

ansible all -m apt -a "pkg=nginx state=latest update_cache=true" \
        -u root --private-key=~/.ssh/id_ansible

This time our output shows us that nothing was changed, as our desired state has already been reached!

Playbooks

We'll use Playbooks to allow some more orchestration and follow a more configuration steps.

mkdir ansible
cd ansible

# Create a playbook named "nginx.yml"
vim nginx.yml

The nginx.yml file:

---
 - hosts: web
   sudo: yes
   user: root
   tasks:
    - name: Add Nginx Repository
      apt_repository: repo='ppa:nginx/stable' state=present
      register: ppainstalled

    - name: Install Nginx
      apt: pkg=nginx state=latest update_cache=true
      when: ppainstalled|success
      notify:
       - Start Nginx

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

Once that's saved, we can run this via the ansible-playbook command:

ansible-playbook --private-key=~/.ssh/id_ansible nginx.yml

This will connect to the servers, gather facts about it, and change what needs to be changed to accomplished the tasks defined.

All Topics