dokaspar
dokaspar

Reputation: 8624

Why does Ansible fail to execute this simple shell script?

I came across a very strange problem with Ansible (1.8.2) that boils down to executing this simple command in a shell script:

#!/bin/sh

# transform a String into lowercase chars:
echo "TeSt" | tr [:upper:] [:lower:]

When I log into the remote Solaris machine, this script seems to work no matter in which shell I am (e.g., /bin/sh, /bin/bash):

# ./test.sh 
test

Also when I execute this script using a remote ssh command, it works:

# ssh root@<remote-host> '/tmp/test.sh'
test

However, when I execute the same script with the Ansible command or shell modules, I get a "Bad String" error no matter what shell I specify:

- shell: executable=/bin/sh /tmp/test.sh      [FATAL stderr: Bad string]
- shell: executable=/bin/bash /tmp/test.sh    [FATAL stderr: Bad string]
- command: /tmp/test.sh                       [FATAL stderr: Bad string]

It took me ages to figure out that it works with the raw module:

- raw: executable=/bin/sh /tmp/test.sh        [OK]

Does anyone have a clue why the shell and command modules produce this error?

Some more info about the remote host on which the script fails:

The locale differs! When I log in or execute a remote ssh command, the locale looks like this:

LANG=
LC_CTYPE="C"
LC_NUMERIC="C"
LC_TIME="C"
LC_COLLATE="C"
LC_MONETARY="C"
LC_MESSAGES="C"
LC_ALL=

However, with Ansible, I get this:

LANG=en_US.UTF-8
LC_CTYPE=en_US.UTF-8
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_ALL=

Upvotes: 1

Views: 1921

Answers (2)

larsks
larsks

Reputation: 311605

Okay. So, despite what Jens says, that script is not "broken" in most environments. I tested it as written under bash, bash --posix, dash, busybox sh, and ksh from the pdksh package, and in all cases it works.

So I went searching for that specific error message (Bad string), and found:

http://sourceforge.net/p/wrapper/bugs/229/

Which appears to exactly describe your problem. It's not a bug in the script; it's a bug in tr on Solaris.

Upvotes: 3

Jens
Jens

Reputation: 72649

Whatever other problem you have, there's certainly a problem with your tr command,

tr [:upper:] [:lower:]

because [] is a character range specification expanded by the shell. If by chance you have a file named :, u, p, e, r, l, o, or w this will get expanded before tr sees it as an argument:

$ touch u
$ echo [:upper:]
u

Fix: use quotes, as in

tr '[:upper:]' '[:lower:]'

Upvotes: 0

Related Questions