ansible-workshops

This project is maintained by p-avery

Overview

A playbook is where you list the steps you would like to automate into a repeatable set of plays and tasks. It is recommended that you store playbooks in a source control management (SCM) system so you will have version control and branches assigned.

A playbook can have multiple plays and a play can have one or multiple tasks. The goal of a play is to map a group of hosts. The goal of a task is to implement modules against those hosts.

Section 1: Reviewing the Playbook

Step 1:

We will be using our playbook that includes all the ad-hoc commands we just ran, this will demonstrate what we just did, However; We can see how the individual adhoc jobs are now put into a list of tasks.

---
- name: Playbook Demo
  hosts: web
  tasks:
  - name: ping the hosts
    ping:

  - name: Ensure Apache is installed
    yum:
      name: httpd
      state: present
    become: true

  - name: Ensure Apache is running
    service:
      name: httpd
      state: started
      enabled: true
    become: true
    
  - name: Get a list of all services
    service_facts:

  - name: Read the redhat-release file
    shell: cat /etc/redhat-release
    check_mode: no
    changed_when: false

This is the entire playbook

Section 2: Defining The Play

Breaking the playbook down we can see to start with the play we specify our target audience

---
- name: Playbook Demo
  hosts: web

The hosts is our target and is set to web, this is the same group name we used in the ad-host command, this value could be as global as

hosts: all 

or can be a group or node name and it can include multiple if needed.

hosts: node1,node2,node3

Section 3: Adding Tasks to a Play ====================================

Now that playbook has been defined, We need to add some tasks to get some things done.

  tasks:
  - name: Ping the hosts
    ping:

  - name: Ensure Apache is installed
    yum:
      name: httpd
      state: present
    become: true

  - name: Ensure Apache is running
    service:
      name: httpd
      state: started
      enabled: true
    become: true
    
  - name: Get list of all services on box
    service_facts:

  - name: Read the redhat-release file
    shell: cat /etc/redhat-release
    check_mode: no
    changed_when: false
  - name: Ping the hosts
    ping:
  - name: ensure Apache is installed
    yum:
      name: httpd
      state: present
    become: true
  - name: ensure Apache is running
    service:
      name: httpd
      state: started
      enabled: true
    become: true
  - name: get list of all services on box
    service_facts:
  - name: read the redhat-release file
    shell: cat /etc/redhat-release
    check_mode: no
    changed_when: false

check_mode: no

changed_when: false

Note

Ansible (well, YAML really) can be a bit particular about formatting especially around indentation/spacing. When you get back to the office, read up on this YAML Syntax a bit more and it will save you some headaches later.