# how to run a shell command within a playbook repeatedly

**URL:** <https://forum.ansible.com/t/how-to-run-a-shell-command-within-a-playbook-repeatedly/35901>\
**Category:** Ansible Project\
**Created:** [October 20, 2021, 7:02am UTC](https://forum.ansible.com/t/how-to-run-a-shell-command-within-a-playbook-repeatedly/35901 "2021-10-20T07:02:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hareesh\_Vn](https://avatars.discourse-cdn.com/v4/letter/h/f14d63/32.png) [@Hareesh\_Vn](https://forum.ansible.com/u/Hareesh_Vn)\
**Post date:** [October 20, 2021, 7:02am UTC](https://forum.ansible.com/t/how-to-run-a-shell-command-within-a-playbook-repeatedly/35901/1 "2021-10-20T07:02:20Z")

</div>

Hi Team

Below is problem description on which i need help.

I am adding single mongo db shards by running below shell command – this is working fine to install on one server

- name: “Creating the database”  
command: “sh /tmp/add\_shard.sh {{item}} {{mongo\_user}} {{mongo\_password}}”  
register: mongo\_db\_shard\_add  
ignore\_errors: true

with\_items: “{{P\_NODE}}”  
when: ansible\_default\_ipv4\_address ==== “{{item}}”

here my Question :

Now i want to run add mutiple shards on multiple servers (example: add 3 shards on each servers). So how to change above code?

Basically , i want to run below command repeatedly in loop for "n’ number of time where n=number of shards to be added.  
Also i should pass ‘n’ as input while running playbook

command: “sh /tmp/add\_shard.sh {{item}} {{mongo\_user}} {{mongo\_password}} {{n}}”  
where “n” is number of shards to be created"

Thanks  
Hareesh

---

<div class="post-metadata">

**Author:** ![Jorge\_Rua1](https://avatars.discourse-cdn.com/v4/letter/j/a87d85/32.png) [@Jorge\_Rua1](https://forum.ansible.com/u/Jorge_Rua1)\
**Post date:** [October 20, 2021, 9:24am UTC](https://forum.ansible.com/t/how-to-run-a-shell-command-within-a-playbook-repeatedly/35901/2 "2021-10-20T09:24:45Z")

</div>

You can define a data structure, a dict for example containing number of shards per server an individual shard settings etc. Then just use with\_sequence to iterate from 1 to length of the aforementioned shards list.

---

<div class="post-metadata">

**Author:** ![Hareesh\_Vn](https://avatars.discourse-cdn.com/v4/letter/h/f14d63/32.png) [@Hareesh\_Vn](https://forum.ansible.com/u/Hareesh_Vn)\
**Post date:** [October 20, 2021, 9:38am UTC](https://forum.ansible.com/t/how-to-run-a-shell-command-within-a-playbook-repeatedly/35901/3 "2021-10-20T09:38:02Z")

</div>

can you please help me changing below code as per your suggestion of with\_sequence?

- name: “Creating the database”  
command: “sh /tmp/add\_shard.sh {{item}} {{mongo\_user}} {{mongo\_password}}”  
register: mongo\_db\_shard\_add  
ignore\_errors: true

with\_items: “{{P\_NODE}}”  
when: ansible\_default\_ipv4\_address ==== “{{item}}”

---

<div class="post-metadata">

**Author:** ![Jorge\_Rua1](https://avatars.discourse-cdn.com/v4/letter/j/a87d85/32.png) [@Jorge\_Rua1](https://forum.ansible.com/u/Jorge_Rua1)\
**Post date:** [October 21, 2021, 12:07am UTC](https://forum.ansible.com/t/how-to-run-a-shell-command-within-a-playbook-repeatedly/35901/4 "2021-10-21T00:07:18Z")

</div>

I would not run a shell script with the command module. You should use the script module instead. But ideally if you have the option use the mongodb\_shard module.

This piece of code actually uses with\_sequence to illustrate how to generate a sequence of numbers that will then used to populate port numbers dynamically:
