Featured post

Quiz: Data PreProcessing

Wednesday, 12 June 2019

Ansible-2


Chapter 7

Play and Playbooks




Playbook

 

It maps module actions to hosts in the inventory, and then it uses the configuration file to define the behavior as it's deploying those actions.
Each playbook holds, individual plays.
Play map hosts to tasks
A play can have multiple tasks
A playbook can have multiple plays


Sample Playbook



·       having 2 plays
·        it’s a YAML file.
·       Whitespace is crucial





Tasks


·       Tasks are executed in order – top down
·       Tasks use modules


Execution of Playbook


#ansible-playbook playbook.yml

·       Above command assume here that the inventory file is already predefined inside of your Ansible configuration file.
·       If a host does fail a task, that host is no longer going to have tasks executed
e.g: Task 4 failed, then no task will execute after that.
·       When any tasks fails for a host, it’s going to added to a retry file.




First Playbook


·       Write a playbook
·       Add play to install web server
·       Add play to install db server
·       Add play to start services
·       Fail a play
·       Retry a failed play

# pwd
/exercise1
# ls
ansible.cfg  inventory 
# cat ansible.cfg
[defaults]
hostfile = inventory

# cat testservers_ansible2.yaml
---
- hosts: testservers

  tasks:
  - name: Ensure that Apache is installed
    yum: name=httpd state=present

  - name: Start Apache Services
    service: name=httpd enable=yes state=started

- hosts: devserver

  tasks:
  - name: Ensure MYSQL is  installed
    yum: name=mariadb-server state=present
  - name: Start MySql
    service: name=mariadb state=started

- hosts: testservers:devserver

  tasks:
  - name: Stop Firewalld
    service: name=firewalld state=stopped

# ansible-playbook testservers_ansible2.yaml

PLAY [testservers] *************************************************************

TASK [setup] *******************************************************************
ok: [ansible2]

TASK [Ensure that Apache is installed] *****************************************
ok: [ansible2]

TASK [Start Apache Services] ***************************************************
changed: [ansible2]

PLAY [devserver] ***************************************************************

TASK [setup] *******************************************************************
ok: [ansible3]

TASK [Ensure MYSQL is  installed] **********************************************
ok: [ansible3]

TASK [Start MySql] *************************************************************
ok: [ansible3]

PLAY [testservers:devserver] ***************************************************

TASK [setup] *******************************************************************
ok: [ansible3]
ok: [ansible2]

TASK [Stop Firewalld] **********************************************************
ok: [ansible3]
ok: [ansible2]

PLAY RECAP *********************************************************************
ansible2                   : ok=5    changed=1    unreachable=0    failed=0
ansible3                   : ok=5    changed=0    unreachable=0    failed=0
















Chapter

Installing LAMP using Ansible




Clone the repository on the linux machine

#

#cd ansible-lamp

Write below script.sh to generate tree structure:
#vi script.sh
#!/bin/bash
# only if you have bash 4 in your CentOS system
shopt -s globstar
for file in **/*
do
    slash=${file//[^\/]}
    case "${#slash}" in
        0) echo "|-- ${file}";;
        1) echo "|   |--  ${file}";;
        2) echo "|   |   |--  ${file}";;
    esac
done

# ./script.sh
|-- hosts
|-- playbook.yml
|-- README.md
|-- roles
|   |--  roles/httpd
|   |   |--  roles/httpd/defaults
|   |   |--  roles/httpd/handlers
|   |   |--  roles/httpd/tasks
|   |   |--  roles/httpd/templates
|   |--  roles/init
|   |   |--  roles/init/handlers
|   |   |--  roles/init/tasks
|   |--  roles/mysql
|   |   |--  roles/mysql/defaults
|   |   |--  roles/mysql/handlers
|   |   |--  roles/mysql/tasks
|   |   |--  roles/mysql/templates
|   |--  roles/php56
|   |   |--  roles/php56/defaults
|   |   |--  roles/php56/handlers
|   |   |--  roles/php56/tasks
|   |--  roles/php70
|   |   |--  roles/php70/defaults
|   |   |--  roles/php70/handlers
|   |   |--  roles/php70/php7.png
|   |   |--  roles/php70/tasks
|-- script.sh


#tree -A
.
├── hosts
├── playbook.yml
├── README.md
├── roles
   ├── httpd
      ├── defaults
         └── main.yml
      ├── handlers
         └── main.yml
      ├── tasks
         ├── configure.yml
         ├── install.yml
         ├── main.yml
         └── secure.yml
      └── templates
          └── default.tpl
   ├── init
      ├── handlers
         └── main.yml
      └── tasks
          └── main.yml
   ├── mysql
      ├── defaults
         └── main.yml
      ├── handlers
         └── main.yml
      ├── tasks
         ├── configure.yml
         ├── install.yml
         ├── main.yml
         └── secure.yml
      └── templates
   ├── php56
      ├── defaults
         └── main.yml
      ├── handlers
         └── main.yml
      └── tasks
          ├── configure.yml
          ├── install.yml
          └── main.yml
   └── php70
       ├── defaults
          └── main.yml
       ├── handlers
          └── main.yml
       ├── php7.png
       └── tasks
           ├── configure.yml
           ├── install.yml
           └── main.yml
└── script.sh


·       Each role has 4 directories – defaults, handlers, tasks, templates.

├── httpd
   ├── defaults
   ├── handlers
   ├── tasks
   └── templates

·       Blank directories can be there, it will not harm the build.

Tasks:
One or more yaml file can be there.
But in case of more than one, main.yaml should be there.
main.yml includes other file names.

e.g:
# file: roles/httpd/tasks/main.yml

- include: install.yml
- include: configure.yml
- include: secure.yml



# file: roles/httpd/tasks/configure.yml

- name: Change default apache vhost
  template: src=default.tpl dest=/etc/httpd/conf.d/000-default.conf

- name: Set global ServerName for apache config
  lineinfile: dest=/etc/httpd/conf/httpd.conf line="ServerName localhost"

- name: SELinux status
  command: sestatus
  ignore_errors: yes
  notify:
    - restart httpd

- name: Ensure Apache running
  service: name=httpd state=started enabled=yes
  ignore_errors: yes

ignore_errors: yes   -> even if task fails, ansible will not stop here.



Defaults:

·       Define variables here

# file: roles/httpd/defaults/main.yml
---

# used in apache vhost configuration files
doc_root: /var/www/html
hostname: vagrantbox

syntax to use variable: {{hostname}}


Handlers:
·       If in tasks, notify is there, it will look for handler


# file: roles/httpd/tasks/configure.yml

- name: SELinux status
  command: sestatus
  notify:
    - restart httpd
# file: roles/httpd/handlers/main.yml
---

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

here “restart httpd” is just text in tasks section to lookout in handlers.




Finally run below command:
#ansible-playbook -i hosts playbook.yml

For test mode:
#ansible-playbook -i hosts playbook.yml -t

No comments:

Post a Comment