Writing your first playbook

Agenda

  • What is a playbook?
  • What are we Automating
  • What makes up a playbook
  • Roles and Sharing Automation

What are you automating?

  • What OS? What kinds of security measures are on the managed node?
  • Ansible will do what you tell it to.

What is a playbook?

A set of instructions to do something on a remote host(s)

What makes up a playbook?

  • Header
  • Tasks
  • Handlers

---
- hosts: webservers
  vars:
    http_port: 80
    max_clients: 200
  remote_user: root
  tasks:
  - name: ensure apache is at the latest version
    yum:
      name: httpd
      state: latest
          

Forming a task

  • Starts with a name
  • The module and specific arguments
  • Your variables can be used in arguments

Conditionals

Yes, you can use them in playbooks

Some of the most popular are:

  • When Statement
  • Register

When Statement


tasks:
  - name: "shut down Debian flavored systems"
    command: /sbin/shutdown -t now
    when: ansible_facts['os_family'] == "Debian"
          

Using Loops

  • Loops can be used to save some typing.
  • Short handed task writing to automate your keystrokes

Loop Example


- name: add several users
  user:
    name: "{{ item }}"
    state: present
    groups: "wheel"
  loop:
     - testuser1
     - testuser2
          

Handlers

  • The quicker restarter, the cleanup crew.
  • Regardless of how many tasks notify a handler, it will run only ONCE.

What a handler looks like


- name: restart apache
      service:
        name: httpd
        state: restarted
        

Notifying a handler


- name: write the apache config file
    template:
      src: /srv/httpd.j2
      dest: /etc/httpd.conf
    notify:
    - restart apache
        

Roles and Sharing Automation

  • What are roles?
  • Playbook vs Role

Ansible Galaxy

  • What is it?
  • How can I use it?
  • https://galaxy.ansible.com/

Thanks all!