# Run Command On Each Host

**URL:** <https://forum.ansible.com/t/run-command-on-each-host/33285>\
**Category:** Ansible Project\
**Created:** [June 30, 2020, 2:04pm UTC](https://forum.ansible.com/t/run-command-on-each-host/33285 "2020-06-30T14:04:13Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jitender\_J](https://avatars.discourse-cdn.com/v4/letter/j/2bfe46/32.png) [@Jitender\_J](https://forum.ansible.com/u/Jitender_J)\
**Post date:** [June 30, 2020, 2:04pm UTC](https://forum.ansible.com/t/run-command-on-each-host/33285/1 "2020-06-30T14:04:13Z")

</div>

Hi Team,

I want to run multiple commands in shell module, but i want to run 1st command on each hosts than move to next command on each host than third and so on, so that i can get output of same command together from all host at single place, tried with loop (with\_item)  
but no luck, need help

Thanks  
jitender

---

<div class="post-metadata">

**Author:** ![Luca\_remix\_tj\_Lorenz](https://avatars.discourse-cdn.com/v4/letter/l/ea666f/32.png) [@Luca\_remix\_tj\_Lorenz](https://forum.ansible.com/u/Luca_remix_tj_Lorenz)\
**Post date:** [June 30, 2020, 2:14pm UTC](https://forum.ansible.com/t/run-command-on-each-host/33285/2 "2020-06-30T14:14:08Z")

</div>

Hello,

ansible by default runs a task on all hosts and then waits for result  
from everyone before going to the next step. If your task has a loop,  
there's no way to run the loop synchronously between all nodes,  
because is a single task with multiple options.

You can split the commands in several shell tasks, instead one with a loop.

Luca

---

<div class="post-metadata">

**Author:** ![Jitender\_J](https://avatars.discourse-cdn.com/v4/letter/j/2bfe46/32.png) [@Jitender\_J](https://forum.ansible.com/u/Jitender_J)\
**Post date:** [June 30, 2020, 2:17pm UTC](https://forum.ansible.com/t/run-command-on-each-host/33285/3 "2020-06-30T14:17:44Z")

</div>

Thanks for the update, i’'ll try using multiple shell tasks, also can you please let me know how to make ansible not to print commands only print output of command

---

<div class="post-metadata">

**Author:** ![Luca\_remix\_tj\_Lorenz](https://avatars.discourse-cdn.com/v4/letter/l/ea666f/32.png) [@Luca\_remix\_tj\_Lorenz](https://forum.ansible.com/u/Luca_remix_tj_Lorenz)\
**Post date:** [June 30, 2020, 2:20pm UTC](https://forum.ansible.com/t/run-command-on-each-host/33285/4 "2020-06-30T14:20:33Z")

</div>

Hello,

try this for printing command output. I don't know if you can hide the  
other parts of output:

[https://stackoverflow.com/a/24834231](https://stackoverflow.com/a/24834231)

---

<div class="post-metadata">

**Author:** ![Jitender\_J](https://avatars.discourse-cdn.com/v4/letter/j/2bfe46/32.png) [@Jitender\_J](https://forum.ansible.com/u/Jitender_J)\
**Post date:** [July 1, 2020, 10:13am UTC](https://forum.ansible.com/t/run-command-on-each-host/33285/5 "2020-07-01T10:13:54Z")

</div>

Hi how can i access array in shell module trying to access as below but not getting any output

tasks:

- name: verify that it shows the new cluster ID for this company in active list and old cluster in inactive list  
shell:  
spaceorg=`echo "{{ commaorg }}"|sed 's/,/ /g'`|for i in ${spaceorg[@]} ; do echo $i ; /sc/bin/smmgr -s cid=$i -s getsminfo=company\_ssn 2\>/dev/null|grep $i|grep -v Changing ; done  
args:  
executable: /bin/bash

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [July 1, 2020, 1:11pm UTC](https://forum.ansible.com/t/run-command-on-each-host/33285/6 "2020-07-01T13:11:03Z")

</div>

> Hi how can i access array in shell module trying to access as below but not getting any output
> 
> tasks:
> 
> ```
> \- name: verify that it shows the new cluster ID for this company in active list and old cluster in inactive list
> shell:
> spaceorg=\`echo &quot;\{\{ commaorg \}\}&quot;|sed &#39;s/,/ /g&#39;\`|for i in $\{spaceorg\[@\]\} ; do echo $i ; /sc/bin/smmgr \-s cid=$i \-s
> 
> ```
> 
> getsminfo=company\_ssn 2\>/dev/null|grep $i|grep -v Changing ; done  
> args:  
> executable: /bin/bash

Do yourself a favour and use a shell script on the target host.

You can either create it with copy or template module.

Your task above is hard to read and escaping all the special characters is a difficult and superfluous venture.

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Jitender\_J](https://avatars.discourse-cdn.com/v4/letter/j/2bfe46/32.png) [@Jitender\_J](https://forum.ansible.com/u/Jitender_J)\
**Post date:** [July 2, 2020, 1:47pm UTC](https://forum.ansible.com/t/run-command-on-each-host/33285/7 "2020-07-02T13:47:01Z")

</div>

Dont want ansible to print command as below only need output , is it possible ?

changed: [zsn-ams2-clr-1b2]

cmd: spaceorg=`echo "25528960,28115152,28863788"|sed 's/,/ /g'`

bin/smmgr -s nameid=smid2name \>\> /sc/build/smid2namePostOUTPUT-32145.txt

for i in ${spaceorg[@]}; do echo $i && grep -w $i /sc/build/smid2namePostOUTPUT-32145.txt | wc -l; done

---

<div class="post-metadata">

**Author:** ![Jitender\_J](https://avatars.discourse-cdn.com/v4/letter/j/2bfe46/32.png) [@Jitender\_J](https://forum.ansible.com/u/Jitender_J)\
**Post date:** [July 3, 2020, 11:11am UTC](https://forum.ansible.com/t/run-command-on-each-host/33285/8 "2020-07-03T11:11:18Z")

</div>

Anything Guys any option here with i can hide command execute on target hosts

---

<div class="post-metadata">

**Author:** ![Werner\_Flamme](https://avatars.discourse-cdn.com/v4/letter/w/c68b51/32.png) [@Werner\_Flamme](https://forum.ansible.com/u/Werner_Flamme)\
**Post date:** [July 3, 2020, 12:16pm UTC](https://forum.ansible.com/t/run-command-on-each-host/33285/9 "2020-07-03T12:16:52Z")

</div>

Maybe you can register a variable that captures the "cmd" output and  
look at yourvariable.output afterwards?
