Skip to content
Ansible

icon picker
Loops in ansible

#!/root/ansible/myansible/bin/ansible-playbook
- name: Loops in Ansible Playbook Part I
hosts: all
remote_user: ec2-user
become: 'yes'
become_user: root

vars:
alpha: [ 'a', 'b', 'c', 'd' ]
numbers: [ 1, 2, 3, 4 ]

tasks:
# Add Multiple User's in one go
- name: add several users in one go
user:
name: "{{ item }}"
state: present
groups: "games"
with_items:
- testuser1
- testuser2
- testuser3
- testuser4
- testuser5

- name: add several users
user:
name: "{{ item.name }}"
state: present
groups: "{{ item.groups }}"
with_items:
- { name: 'testuser6', groups: 'nobody' }
- { name: 'testuser7', groups: 'nobody' }
- { name: 'testuser8', groups: 'postfix' }
- { name: 'testuser9', groups: 'postfix' }

- name: Loop Over Set of Collection variable
debug:
msg: "{{ item.0 }} and {{ item.1 }}"
with_together:
- "{{ alpha }}"
- "{{ numbers }}"
#!/root/ansible/myansible/bin/ansible-playbook
- name: Loops in Ansible Playbook Part II
hosts: all
remote_user: ec2-user
become: 'yes'
become_user: root

vars:
alpha: [ 'a', 'b', 'c', 'd' ]
numbers: [ 1, 2, 3, 4 ]

tasks:
# Add Multiple User's in one go
- name : Random Looping Example
debug:
msg: "{{ item }}"
with_random_choice:
- "go through the door"
- "drink from the goblet"
- "press the red button"
- "do nothing"
# Looping Over A List With An Index
- name: Looping over a List
debug:
msg: "At array position {{ item.0 }} there is a value {{ item.1 }}"
with_indexed_items:
- "{{ alpha }}"

# Do Until Loop
- name: Ensure Apache is Running
service:
name: httpd
state: started
register: result
until: result.changed == True
retries: 10
delay: 4


Installing and uninstalling multiple packages using loops
#!/root/ansible/myansible/bin/ansible-playbook
- name: Loops in Ansible Playbook Part III
hosts: all
remote_user: ec2-user
become: 'yes'
become_user: root

vars:
packages: [ 'gettext-devel', 'openssl-devel', 'perl-CPAN', 'perl-devel', 'zlib-devel', 'unzip', 'curl', 'wget' ]
tasks:
- name: Install Multiple Packages using Loop
yum:
name: '{{ item }}'
state: present
loop:
- gettext-devel
- openssl-devel
- perl-CPAN
- perl-devel
- zlib-devel
- unzip
- curl
- wget


- name: UnInstall Multiple Packages using Index Loop
yum:
name: '{{ item.1 }}'
state: absent
with_indexed_items:
- "{{ packages }}"


- name: Install Multiple Packages using Index Loop
yum:
name: '{{ item.0 }}'
state: present
with_together:
- "{{ packages }}"
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.