Ashar
Ashar

Reputation: 3245

Ansible shell module behaves differently than the Linux shell

I wish to grab the output of Linux's multiple commands in a Single Line like shown below. As you can see the output of ls and cksum are printed in one line.

# myvar=`ls -ltr test.yml; cksum<test.yml | tr '\n' '\t'`
# echo $myvar

Output: -rw-r--r--. 1 root root 298 May 3 04:46 test.yml 3415682946 298

Now, i wish to do the same in Ansible so below is the ansible command:

# ansible localhost -m shell -a "ls -ltr test.yml; cksum<test.yml | tr '\n' '\t'"

However, as you can see the output of cksum is printed on the second line instead of one.

Output: localhost | CHANGED | rc=0 >>

-rw-r--r--. 1 root root 298 May 3 04:46 test.yml

3415682946 298

Using the command module throws error.

Can you please help me understand how can I tweak the ansible command to get the output in a single line for both ls and cksum ?

Note: you can quickly test this as well on your systems.

Upvotes: 0

Views: 277

Answers (1)

Matt P
Matt P

Reputation: 2615

You are translating newline->tab for the wrong command, and the echo is actually skewing your test results. The command you want is:

ansible localhost -m shell -a "ls -ltr test.yml | tr '\n' '\t'; cksum<test.yml"

If you ran ls -ltr test.yml; cksum<test.yml | tr '\n' '\t' without assigning it to a variable you will see the output split across two lines. The echo is actually stripping the additional white space. To echo your true result (with white space included), enclose the variable in double quotes:

echo "$myvar"

Upvotes: 1

Related Questions