Simple loops :-
this will iterate from a list of items for a task.
backend concept:-
It uses with_items as loop starter and data is stored in item
Demo 1:
-bash-4.3$ cat loops/simple.yml
--- - hosts: localhost
become: true
tasks:
- name: starting service
service:
name: "{{ item }}"
state: restarted
enabled: yes
with_items:
- httpd
- sshd
Using internal variables :
Demo 2:
-bash-4.3$ cat loops/simple1.yml ---
- hosts: localhost
become: true
vars:
x:
- httpd
- sshd
tasks:
- name: starting service
service:
name: "{{ item }}"
state: restarted
enabled: yes
with_items: "{{ x }}"
Using external variables :
demo 3:
-bash-4.3$ cat loops/simple2.yml ---
- hosts: localhost
become: true
tasks:
- name: including variable
include_vars: vars/var.yml
- name: starting service
service:
name: "{{ item }}"
state: restarted
enabled: yes
with_items: "{{ x }}"
-bash-4.3$ cat loops/vars/var.yml x:
- httpd
- sshd
Hash oriented or Key value based simple loop:
Demo :
-bash-4.3$ cat loops/keyval.yml
---
- hosts: localhost
become: true
tasks:
- name: adding users and assigining them in a group
user:
name: "{{ item.name }}"
state: present
groups: "{{ item.groups }}"
with_items:
- {name: 'raj', groups: 'root'}
- {name: 'roahn', groups: 'wheel'}
- {name: 'rajesh', groups: 'bin'}
Time for nested loops
-bash-4.3$ cat loops/nested.yml ---
- hosts: localhost
become: true
vars:
x:
- key1
- key2
- key3
tasks:
- name: adding in line
lineinfile: dest=/tmp/{{ item[0] }}/a.txt line={{ item[1] }} state=present
with_nested:
- ['user1','user2','user3']
- "{{ x }}"
Here :
all 3 keys are added to each user file
Demo 2:
-bash-4.3$ cat loops/nestedbug.yml ---
- name: Demo of with_nested
hosts: localhost
become: true
vars:
list1: [1, 2, 3]
list2: [a, b, c, d]
tasks:
- name: Printing message
debug: msg="Values are {{item[0]}} and {{item[1]}}"
with_nested:
- list1
- list2
ANSIble tasks with condition :
As in normal programming language we are using we are using when it comes to project with ansible it also check the same thing :
Demo:
-bash-4.3$ cat condition/when1.yml ---
- hosts: localhost
become: true
vars:
x: true
tasks:
- name: pring message
debug: msg="hello world this is ansible"
when: x
Thanks !! very helpful post!
ReplyDeleteAlso see my Website
Technology
Its really worth information
ReplyDeletedevops course in Marathahalli
best devops training in Marathahalli
Devops certification training in Marathahalli
devops training in Marathahalli
devops training institute in marathahalli
Great Article !! Thanks for sharing..
ReplyDeleteAzure DevOps Online Training
Microsoft Azure DevOps Training Courses
Microsoft Azure DevOps online Training in Hyderabad
Microsoft Azure DevOps Training
Azure DevOps Online Training in Hyderabad
Thank you for your great post!
ReplyDeleteDocker Training in Hyderabad
Docker and Kubernetes Online Training
Docker Training
Docker Online Training
Kubernetes Online Training
Kubernetes Training in Hyderabad
Best Docker and kubernetes training in ameerpet
Docker and Kubernetes Training in Hyderabad
very informative. Devops Training
ReplyDeleteDevops online Training
Devops Training in Hyderabad
It's Really A Great Post.
ReplyDeleteDevOps Course Videos
Great Article !! Thanks for sharing..
ReplyDeleteDocker Online Training
Docker Training in Hyderabad
Hey thanks for this amazing post! Thank you so much for sharing the good post, I appreciate your hard work.Keep blogging.
ReplyDeleteDevOps Training in Electronic City
Very Informative! To know more on devops engineer certification.
ReplyDeleteThis information really helped me a lot. It was very informative.
ReplyDeleteDevops Services
Thanks for sharing such a great post. It is very useful and informative. Valuable information you have shared. Also, check out
ReplyDeleteDevops Services