#Devops - Ansible Playbook - Ansible Tutorial for Beginners Part-3
#Ansibleplaybook
Now will discuss playbooks...
ansible playbook tutorial,ansible playbook examples,ansible playbook yaml scripting,
yaml playbook ansible,
ansible loop,ansible loops tutorial,ansible loop vs with_items,ansible loop variable,
ansible loop more tasks,ansible loop tutorial,ansible loop in loop,ansible loops example,
ansible when condition,ansible variable,ansible variable tutorial,ansible variables best practices,
ansible gather_facts,ansible gather facts
Playbook syntax starts with --- and ends with ...(this is not mandatory)
to execute playbook file
ansible-playbook filename.yml
Now will write our first playbook.
vi example.yml
--- # This is My first Playbook
hosts: demo
become: yes #giving root previliges
tasks:
name: Install httpd on server
action: yum pkg=httpd state=installed
to execute this playbook
ansible-playbook example.yml
it is giving "You need to be root to perform this command"
to overcome this error we need to be root...
will check on both nodes...
HTTPD Intalled on both nodes. Now remove that install again...
done for first playbook....
run the target section...
create another playbook file test.yml and add the below
--- # This is My target Playbook
hosts: demo
user: ansible
become: yes #giving root previliges
connection: ssh
gather_facts: yes
Will discuss tasks section:
edit vi test.yml
--- # This is My tasks Playbook
hosts: demo
user: ansible
become: yes #giving root previliges
connection: ssh
gather_facts: yes
tasks:
name: install httpd on centos 7
action: yum name=httpd state=installed
name: install mysql on centos 7
action: yum name=mysql state=installed
Now will move to variable section
will include variable.
--- # This is My variable Playbook
hosts: demo
user: ansible
become: yes #giving root previliges
connection: ssh
vars:
pkgname: httpd #pkgname is the variable #for this variable we are giving httpd
tasks:
name: Install HTTPD on centos 7
action: yum name='{{ pkgname}}' state=installed
Now handlers section:
--- # This is My variable Playbook
hosts: demo
user: ansible
become: yes #giving root previliges
connection: ssh
tasks:
name: Install HTTPD on centos 7
action: yum name=httpd state=installed
notify: restart HTTPD #it will notify if the httpd successfully installed...
name: restart HTTPD #notify and name must be same
action: service name=httpd state=restarted #whenever notify takes information it will execute.
Now will see dry run:
ansible-playbook test.yml --check
it will check the code and gives the output only...it won't install anything
just it will check the code and gives us the output.....like testing...
Loops:
create loop.yml
--- # This is My loops Playbook
hosts: demo
user: ansible
become: yes #giving root previliges
connection: ssh
tasks:
name: add a list of users
user: name='{{ item }}' state=present
with_items:
prabha
chinnu
execute it ansible-playbook loops.yml
check the users in nodes cat /etc/passwd
Now will see when conditionals playbook:
--- # This is My when Playbook
hosts: demo
user: ansible
become: yes #giving root previliges
connection: ssh
tasks:
name: Install apache for debian
command: apt-get -y install apache2 #This will execute when condition is success
when: ansible_os_family == "debian" #it will check the os_family is debian or not...if debian then command will execute.
name: Install apache for redhat
command: yum -y install httpd #This will execute when condition is success
when: ansible_os_family == "Redhat" #it will check the os_family is redhat or not...if redhat then command will execute.
it skipped both execution because....those package conditions and both are not ansible_os_family...
Ansible Gather Facts - Ansible Variable,Loops,When Condition | Ansible Tutorial for Beginners Part-3 HD
#Devops - Ansible Playbook - Ansible Tutorial for Beginners Part-3
#AnsibleTutorial
• #Devops - Ansible Playbook - Ansible ...
Ansible Ad hoc Commands - Ansible All Modules | Ansible Package Modules | Ansible Tutorial Part - 2
#Devops #Ansible Ad hoc Commands - Ansible Tutorial Part - 2
#AnsibleTutorial
• #Devops #Ansible Ad hoc Commands - An...
How to Install Ansible - Give Sudo Privileges | SSH Connection to Nodes - Ansible Tutorial part - 1
#Devops - Ansible Playbook - Ansible Tutorial for Beginners Part-1
#AnsibleTutorial
• Ansible Tutorial - Give Sudo Privileg...
Let's subscribe for more videos like this
share,comment and like....