Run Command On Each Host

49 views
Skip to first unread message

Jitender J

unread,
Jun 30, 2020, 10:04:13 AM6/30/20
to Ansible Project
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

Luca 'remix_tj' Lorenzetto

unread,
Jun 30, 2020, 10:14:39 AM6/30/20
to ansible...@googlegroups.com
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
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/efe49a15-80e6-4d37-aab6-44793d1d4c58n%40googlegroups.com.



--
"E' assurdo impiegare gli uomini di intelligenza eccellente per fare
calcoli che potrebbero essere affidati a chiunque se si usassero delle
macchine"
Gottfried Wilhelm von Leibnitz, Filosofo e Matematico (1646-1716)

"Internet è la più grande biblioteca del mondo.
Ma il problema è che i libri sono tutti sparsi sul pavimento"
John Allen Paulos, Matematico (1945-vivente)

Luca 'remix_tj' Lorenzetto, http://www.remixtj.net , <lorenze...@gmail.com>

Jitender J

unread,
Jun 30, 2020, 10:18:09 AM6/30/20
to ansible...@googlegroups.com
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




--
Regards,
Jitender

Luca 'remix_tj' Lorenzetto

unread,
Jun 30, 2020, 10:21:10 AM6/30/20
to ansible...@googlegroups.com
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

On Tue, Jun 30, 2020 at 4:18 PM 'Jitender J' via Ansible Project
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CAMO4JZtPxnaoGxJ6NTTr-EuLpQV9jwWt7ME75t-0Z%3DcjXq%2BKkA%40mail.gmail.com.

Jitender J

unread,
Jul 1, 2020, 6:13:55 AM7/1/20
to Ansible Project

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




Stefan Hornburg (Racke)

unread,
Jul 1, 2020, 9:12:06 AM7/1/20
to ansible...@googlegroups.com
On 7/1/20 12:13 PM, 'Jitender J' via Ansible Project wrote:
>
> 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
>

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
Racke
> ansible-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/0319af45-a40c-4bb1-812a-d59635e871aen%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/0319af45-a40c-4bb1-812a-d59635e871aen%40googlegroups.com?utm_medium=email&utm_source=footer>.


--
Ecommerce and Linux consulting + Perl and web application programming.
Debian and Sympa administration. Provisioning with Ansible.

signature.asc

Jitender J

unread,
Jul 2, 2020, 9:47:01 AM7/2/20
to Ansible Project
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

Jitender J

unread,
Jul 3, 2020, 7:11:18 AM7/3/20
to Ansible Project
Anything Guys any option here with i can hide command execute on target hosts

Werner Flamme

unread,
Jul 3, 2020, 8:17:10 AM7/3/20
to ansible...@googlegroups.com
Maybe you can register a variable that captures the "cmd" output and
look at yourvariable.output afterwards?

'Jitender J' via Ansible Project schrieb am 02.07.20 um 15:47:
--


Reply all
Reply to author
Forward
0 new messages