Does anybody know how to solve this problem?

ansible -m shell -i test.txt -a "sudo find / -regex '.*\(tar\|zip\)$' -type f 2>/dev/null | while read i; do echo $i; done"

The problem is the "while" part. It doesn't show any result. When I run it directly in Bash, then all is OK.

(I need this "while" part, because I do various operations with files found by the find, like unzip and grep)

Thank you

2 Answers 2


Since $i occurs in double-quotes, it'll be expanded (to nothing) before being passed to ansible as part of the command string. You need to escape it: ... do echo \$i; ....

BTW, you should technically do the same with the backslashes and $ that're part of the regular expression, like '.*\\(tar\\|zip\\)\$'. You don't really need to, since the characters after them happen to be ones that can't be interpreted as escape sequences or $ expansions, so the shell will leave them alone, but using syntax that "just happens to work" always makes me nervous.

  • Thank you very much for your quick answer. It's working now.
    – Nicco
    Feb 8, 2022 at 9:06

In respect to the first answer, escaping $i to \$i let it run correctly.

ansible test --user ${ACCOUNT} --ask-pass --become --module-name shell --args "find /home/{{ ansible_user }} -regex '.*\(tar\|zip\)$' -type f 2>/dev/null | while read i; do echo \$i; done"

Instead of using sudo within the shell arguments, --become might be a better approach.


When I run it directly in Bash, then all is OK.

as far as I understand the Ansible shell module, it is doing something like /bin/sh -c. If a Bash is necessary you may use it in example via --args "/bin/bash -c '...'".

Thanks to

  • Thanks for your improvements of my Ansible command. Much appreciated.
    – Nicco
    Feb 8, 2022 at 9:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .