-1

I have a file listing 1 or more server's hostnames hostname_list that I generate using grep and awk. Now I need to execute salt commands against the hostnames. I am trying, as a test, to run test.ping to ping each server in the list.

I have tried doing it as a for loop but I can't get it to work.

I've tried numerous variations of:

for i in hostname_list
do 
    salt $i test.ping
done
1
  • Is hostname_list the name of a text file containing the names? Or a command that generates such a text file to standard output? Or something else? Aug 3, 2020 at 17:50

1 Answer 1

2

Under assumption that hostname_list is a text file containing 1 hostname per line, you can use this:

while read -r line
do
    [[ -n "$line" ]] && salt $line test.ping
done < hostname_list

The [[ -n "$line" ]] statement checks that the line is not empty before executing the following command.

1
  • That's what I was looking for. Thank you!
    – Sean
    Aug 3, 2020 at 18:02

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