Ansible - 4
Ansible Playbook, Variables, Handlers & Loops
------------------------------------------------------
-> Playbook in ansible are written in YAML format.
-> It is human readable data serialization language.
It is commonly used for configuration files.
-> Playbook is like a file where you write codes consist of vars, handlers, files, templates and roles.
-> Each playbook is composed of one or more 'module' in a list module is a collection of configuration files.
->Playbooks are divided into many sections like -
a) Target Section - Define the host against which playbooks task has to be executed.
b) Variable Section - Define variable.
c) Task section - list of all modules that we need to run in an order.
YAML (Yet another markup language)
-----------------------------------
-> for ansible, nearly every YAML files starts with a list.
-> Each item in the list is a list of key-value pairs commonly called a dictionary.
-> All YAML files have to began with "---" and end with "..."
imp point-> All members of a list lines must begin with same indentation level starting with "---"
for eg -> --- #A list of fruits
Fruits:
-Mango
-Strawberry
-Banana
-Grapes
-Apple
...
Key Values
Name: Bhupender
A dictionary is represented in a simple key: value from.
For eg *
---#Details of customer
Customer:
name: Rajput
Job: Trainer
Skills: Ansible
Exp: 8 years
--> Extension for playback files is yml.
Note: There should be space between : and value.
========================================================================================
if you are doing on jump servers and creating playbook then remove two entries user and become as you are working as a root user. so you do not require any specific parameter such as user and become.
-> Go to ansible server.
-> Now create one playbook.
[ansible@ip]$ vi target.yml
---#Target playbook
- hosts: demo
user: ansible
become: yes
connection: ssh
gather-facts: yes
esc --> :wq!
Now, to execute this playbook
[ansible@ip]$ansible-playbook target.yml
server A : ok=0 changed=0 unreachable=1 failed=0 skipped=0 rescued=0 ignored=0
Server B : ok=0 changed=0 unreachable=1 failed=0 skipped=0 rescued=0 ignored=0
=============================================================================================
Now create one more playbook in ansible server
[ansible@ip] vi task.yml
--- # Target and Task Playbook
- hosts: demo
user: ansible
become: yes
connection: ssh
tasks:
- name: install httpd on linux
action: yum name=httpd state=installed
esc --> wq!
Now execute this playbook
[ansible@ip]$ansible-playbook task.yml
====================================================================================================
Variables:
----------
-> Ansible uses variables which are defined previously to enable more flexibility in playbooks and roles. They can be used to loop through a set of given values, access various information like the host name of a system and replace certain strings in templates with specific values.
-> Put variable section above tasks so that we define it first and use it later.
Now go to ansible server and create one playbook.
we can remove user and become parameter if we are using jsethi3 as root user.
[ansible@ip]$ vi vars.yml
--- # My variable playbook
- hosts: demo
user: ansible
become: yes
connection: ssh
vars:
pkgname: httpd
tasks:
- name: install httpd server
action: yum name='{{pkgname}}' state=installed
esc->:wq!
Now execute playbook.
[ansible@ip]$ ansible-playbook vars.
=====================================================================================================
Handlers Section
-----------------
-> A handler is exactly the same as a task, but it will run when called by another task.
or
-> Handlers are just like regular tasks in an ansible playbook, but are only run if the task contains a notify directive and also indicates that it changed something. ------
Go to ansible server
[ansible@ip]$ vi handlers.yml
--- # handlers playbook
- hosts: demo
user: ansible
become: yes
connection: ssh
tasks:
- name: install httpd server
action: yum name=httpd state=installed
notify: restart HTTPD
handlers:
- name: restart HTTPD
action: service name=httpd state=restarted
esc -> wq!
Now execute this playbook
[ansible@ip]$ ansible-playbook handlers.yml
=====================================================
DRYRUN (it will show like a preview before printing)
--------
Check whether the playbook is formatted correctly.
ansible-playbook handlers.yml --check
=====================================================
Loops
-----
-> sometimes you want to repeat a task multiple times. In computer programming, this is called loops common ansible loops include changing ownership on several files and/or directories with the file module, creating multiple users with the user module, and repeating a polling step until certain result is reacted.
Now go to ansible server
Note - never use space between name like bhupinder rajput , always use single name
[ansible@ip] $ vi loops.yml
--- # My loops playbook
- hosts: demo
user: ansible
become: yes
connection: ssh
tasks:
- name: add a list of users
user: name='{{item}}' state=present
with_items:
- Bhupinder
- Zeeshan
- Kishori
- Rajpal
esc ---->:wq!
[ansible@ip]$ ansible-playbook loops.yml
To verify, go inside node1
cat /etc/passwd
Comments
Post a Comment