An Ansible Playbook Tutorial
Use Of handlers to install the packages using Ansible Playbook:
---
- hosts: web
vars:
- package_name: tftp-server
tasks:
- name: Installing {{package_name}}
yum: pkg={{package_name}} state=present
become: yes
become_method: sudo
notify:
- restart {{package_name}}
handlers:
- name: restart {{package_name}}
Looping in Playbooks
The below playbook creates the test directories on the hosts those are defined under the web group of servers inside the /etc/ansible/hosts file -
---
- hosts: web
vars:
- user_name: srohila
tasks:
- name: Create Test Directories
file : path=/home/{{ user_name }}/AnsibleTest/{{ item }}
state=directory
owner={{ user_name }}
group={{user_name }}
mode=0700
with_items:
- src
- bin
- logs
- tmp
- backup
while executing the above playbook, the below output appears on the console -
Using Ansible Playbook to install yum packages-with sudo
Below is the playbook to install packages using yum on the web group of servers-
---
- hosts: web
vars:
- user_name: srohila
tasks:
- name: Installing the Yum Packages
yum: pkg={{item}} state=present
become: yes
become_method: sudo
with_items:
- git
- htop
- unzip
- hosts: web
vars:
- user_name: srohila
tasks:
- name: Installing the Yum Packages
yum: pkg={{item}} state=present
become: yes
become_method: sudo
with_items:
- git
- htop
- unzip
[srohila@localhost ansibleStuff]$ ansible-playbook yumInstall.yml -k --ask-become-pass
Use Of handlers to install the packages using Ansible Playbook:
---
- hosts: web
vars:
- package_name: tftp-server
tasks:
- name: Installing {{package_name}}
yum: pkg={{package_name}} state=present
become: yes
become_method: sudo
notify:
- restart {{package_name}}
handlers:
- name: restart {{package_name}}
Comments
Post a Comment