Getting Started with Ansible 06 – Writing our first Playbook

Ansible is an incredible configuration management and provisioning utility that enables you to automate all the things. In this series, you’ll learn everything you need to know in order to use Ansible for your day-to-day administration duties. In video #6, we get started on writing playbooks, which is how we’ll use Ansible from here on out.

YouTube player

install_apache.yml

 ---
 
 - hosts: all
   become: true
   tasks:
 
   - name: install apache2 package
     apt:
       name: apache2

Run the playbook

 ansible-playbook --ask-become-pass install_apache.yml

install_apache.yml (second version)

 ---
 
 - hosts: all
   become: true
   tasks:
 
   - name: update repository index
     apt:
       update_cache: yes
 
   - name: install apache2 package
     apt:
       name: apache2

install_apache.yml (third version)

 ---
 
 - hosts: all
   become: true
   tasks:
 
   - name: update repository index
     apt:
       update_cache: yes
 
   - name: install apache2 package
     apt:
     name: apache2
 
   - name: add php support for apache
     apt:
       name: libapache2-mod-php

install_apache.yml (fourth version)

 ---
 
 - hosts: all
   become: true
   tasks:
 
   - name: update repository index
     apt:
       update_cache: yes
 
   - name: install apache2 package
     apt:
       name: apache2
       state: latest
 
   - name: add php support for apache
     apt:
       name: libapache2-mod-php
       state: latest

remove_apache.yml

 ---
 
 - hosts: all
   become: true
   tasks:
 
   - name: remove apache2 package
     apt:
       name: apache2
       state: absent
 
   - name: remove php support for apache
     apt:
       name: libapache2-mod-php
       state: absent