Introduction to Ansible Loops

In this post we will be seeing how to use Ansible Loops, Sometimes you want to repeat a task multiple times. In computer programming, this is called a loop. Common Ansible loops include changing ownership on several files and/or directories with the file module, creating multiple users with the user module, and repeating a polling step until a certain result is reached.

Let us create multiple user with loop concept, Copy the below code on the file loop.yml

--- # my loops
 hosts: all
 become: yes
 connection: ssh
 tasks:
        - name: add users
          user: name='{{item}}' state=present
          with_items:
                  - adityamalviya
                  - pranaytest
                  - manojtest 

Let us run below command to run the playbook.

ansible-playbook -i inventory loop.yml

Leave a comment