Ansible Playbook - My First Ansible Playbook | Define Hosts and Tasks in Ansible Playbook

Ansible Playbook - My First Ansible Playbook | Define Hosts and Tasks in Ansible Playbook

ansible playbook tutorial, ansible playbook, ansible playbook examples, ansible tutorial for beginners, ansible tasks, ansible, ansible tutorial, ansible ad hoc commands, ansible modules, ansible example, ansible facts, ansible linux, ansible best practices, ansible modules, ansible best practices, ansible roles, ansible-playbook, how to write a basic ansible playbook
tags: run ansible playbook, ansible playbook inventory, ansible playbook, ansible debug,  ansible tutorial, playbook, ansible, ansible modules, ansible-playbook






Ansible playbooks are simple configuration management and multi-machine deployment system where we can declare tasks/configurations. it's a file that written in yaml format.
Each playbook is composed of one or more ‘pla ys’ in a list and it execute/launch tasks synchronously or asynchronously.


To help or how to use ansible-playbook command,  use -h option
ansible-playbook -h
ansible playbook tutorial, ansible playbook, ansible playbook examples, ansible tutorial for beginners, ansible tasks, ansible, ansible tutorial, ansible ad hoc commands, ansible modules, ansible example, ansible facts, ansible linux, ansible best practices, ansible modules, ansible best practices, ansible roles, ansible-playbook, how to write a basic ansible playbook
Get Ansible Playbook Help

It will show all available option/flags

Now we will write first Ansible Playbook , we can create yaml file under ansible configure directory with any suitable name.
cd /etc/ansible
vi basic.yml
yaml started with "---" then we define group of hosts, variables and tasks.


To define targeted hosts in ansible playbook : following syntax

---
 - hosts: value


In hosts value we can use one hosts, groups or host patterns, separated by colons for example

To run ansble playbook in all hosts of inventory, we will use "all"  keyword, following example

---
 - hosts: all


define single hosts : 127.0.0.1 is ansible hosts

---
 - hosts: 127.0.0.1


define single remote hosts : 172.17.20.98 is ansible remote hosts

---
 - hosts: 172.17.20.98


define multiple hosts in ansible playbook : 127.0.0.1 is localhost & 172.17.20.98 is ansible remote hosts

---
 - hosts: 172.17.20.98,127.0.0.1


OR

---
 - hosts: 172.17.20.98 127.0.0.1

To define group name : localhost is ansible hosts group

---
 - hosts: localhost


To define remote hosts group : remote is ansible hosts group

---
 - hosts: remote

To define multiple group in ansible playbook

---
 - hosts: remote,localhost



Define tasks: following syntax

tasks:
- name: Task name
- module:

We will create a file with the help of file module, following playbook example:

---
 - hosts: all

   tasks:
   - name: Create file
     file:
       state: touch
       path: /tmp/myfirstplaybool.txt



How to add remote hosts in ansible inventory - Click Here….

More Help about modules - Click Here...

To check the syntax of a playbook with the --syntax-check flag.
ansible-playbook --syntax-check basic.yml
To run playbook on all add hosts of inventory
ansible-playbook basic.yml
ansible playbook tutorial, ansible playbook, ansible playbook examples, ansible tutorial for beginners, ansible tasks, ansible, ansible tutorial, ansible ad hoc commands, ansible modules, ansible example, ansible facts, ansible linux, ansible best practices, ansible modules, ansible best practices, ansible roles, ansible-playbook, how to write a basic ansible playbook
Run Ansible Playbook 

Understating of Ansible  Playbook Output 

PLAY  [ all ]  ******************* ( Host/groups/all  Information ) 
TASK  [ Gathering Facts ] ********* ( gather facts  from hosts ) 
TASK  [ Create file ] ************* ( Task name  )
PLAY RECAP ****************** ( Look like summery of execution ) 

To run playbook on specific host/group of inventory : use -l option
ansible-playbook -l <host/group> basic.yml
For example we will run this ansible playbook on single host 192.168.3.104, following command we will use:
ansible-playbook -l 192.168.3.104 basic.yml
ansible playbook tutorial, ansible playbook, ansible playbook examples, ansible tutorial for beginners, ansible tasks, ansible, ansible tutorial, ansible ad hoc commands, ansible modules, ansible example, ansible facts, ansible linux, ansible best practices, ansible modules, ansible best practices, ansible roles, ansible-playbook, how to write a basic ansible playbook
Execute ansible playbook on single hosts of the inventory 


Thanks

End of this ansible playbook ,  we need your support so i request you to please like and share  this post also comment if something missing by me  or  give me  a suggestion to make more easy/useful. 


www.linuxtopic.com
Previous
Next Post »