kujiy
kujiy

Reputation: 6137

Ansible: How to execute multiple commands with `ansible -a`

This works:

  ansible host -a "echo hello"                # hello

But this returned a weird answer

  ansible host -a "echo hello && echo world"      # hello && echo world

Does ansible escape && and ; with ansible -a?

How can I excute it?

Upvotes: 2

Views: 7839

Answers (2)

Konstantin Suvorov
Konstantin Suvorov

Reputation: 68269

By default ansible CLI uses command module, which does not feed its arguments trough shell.

You want to use shell module instead:

ansible host -m shell -a 'echo hello && echo world'

Upvotes: 5

kujiy
kujiy

Reputation: 6137

I remembered that ansible uses just ssh.

# not work
ansible host -a "echo hello && echo world"
# works
ansible host -a "bash -c 'echo hello && echo world'"

The command is excuted just through the ssh command.

Upvotes: 3

Related Questions