I have a text file that contains some IPs. I want to copy the contents of this text file into /etc/ansible/hosts without showing the output on the terminal (as shown in example 2).

Note: root user is disabled.

If I use the following:

  1. sudo cat myfile.txt >> /etc/ansible/host

It will not work, since sudo cat didn't affect redirections (expected).

  1. cat myfile.txt | sudo tee --append /etc/ansible/hosts

It will show the output in the terminal then copy them to /etc/ansible/hosts A.A.A.A B.B.B.B C.C.C.C

  1. Adding /dev/null will interrupt the result (nothing will be copied to /etc/ansible/hosts).
sudo tee -a /etc/ansible/hosts <myfile.txt >/dev/null

Or, if you want to use cat:

cat myfile.txt | sudo tee -a /etc/ansible/hosts >/dev/null

Either of these should work. It is unclear how you "added" /dev/null when you tried, but this redirects the standard output of tee to /dev/null.

  • Thanks, I found the problem with my code, after reading cat methods from your answer, it was an extra character :sweat_smile: that's why when I add /dev/null to my second command did not work. – Ahmad Abuhasna Apr 9 '18 at 13:18

Try to use bash with "-c" switch. For example:

sudo bash -c "cat /etc/hosts >> /etc/ansible/hosts" >> /dev/null

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for?Browse other questions tagged or ask your own question.