1. Playbooks

  • It is the core of ansible

  • It contain instructions to configure the nodes

  • They are written in YAML, a language used to describe data


Structure

1. Name

We have the name of play like play1 and play 2

2. Host

It is the target of the play

3. Tasks

Each play has a list of tasks which contain following sections

name

Each element in the list of tasks is given a name like "install apache" and "start apache"

set of instructions

The name is followed by instructions to execute the task


Other Params

become

used to escalate privleges to specific user to execute tasks. become_user parameter is required to specify the user. remote_user is another way to escalate privleges

vars

used to define variables to be used later on in playbook

notfiy

used to refer handlers

gather_facts

used to gather files or keys which can be used in different playbook


Sample Playbook 1

- name: install apache, php & mysql
  hosts: test-servers
  become: true
  become_user: root
  gather_facts: true
  tasks:
  - name: "Install Apache2"
    #present is used since we are installing this package for first time
    package: name=apache2 state=present

  - name: "Install PHP Cli"
    package: name=php-cli state=present

  - name: "Install PHP Mysql"
    package: name=php-mysql state=present

Sample Playbook 2

- hosts: all
  remote_user: root

  tasks:
    - name: "Install PIP"
      apt: name=python-pip state=present

    - name: "Install Python MySQL module"
      pip: name=MySQL-python

    - name: "Create db user noman"
      mysql_user: user=noman password=aziz priv=*.*:ALL state=present

    - name: "Create db ansible"
      mysql_db: db=ansible state=present

    - name: "Create table reg"
      command: mysql -u noman -p aziz -e 'CREATE TABLE reg (name varchar(30), email varchar(30));' ansible

Sample Playbook 3

- name: copy
  hosts: test-servers
  become: true
  become_true: root
  gather_facts: true
  tasks:
    - name: "copy file"
      copy: src=/home/noman/index.html dest=/var/www/html/index.html

Sample Playbook 4

In Ansible, handlers are typically used to start, reload, restart, and stop services

- hosts: webservers
  tasks:
    - name: install apache2
	  apt: name=apache2 update_cache=yes latest=yes

	  notify:
	    - restart apache2

  handlers:
    - name: restart apache2
	  service: name=apache2 state=restarted

Last updated