Home > Back-end >  Getting the output of just "stdout_lines" or "stdout" when running an ansible ad
Getting the output of just "stdout_lines" or "stdout" when running an ansible ad

Time:09-13

I'm trying to run the below ansible ad-hoc command that runs the "status.sh" script:

ansible host -m script -a '/path/status.sh' -u root -i inventory

The script simply gets the status of a service on the target host as shown below:

service_1=$(ls /etc/systemd/system | grep -e jboss | awk -F ' ' '{print $1}')

if [ ! -z "$service_1" ] //if service exists
then
      systemctl status $service_1
else
      echo "There is No $Service_1 Here !"
fi

I'm getting too much output when running the ad-hoc command, I just want to limit the output to stdout_lines or stdout, is there a way to do so without creating a particular playbook with debug or any other modules just by adding an option or piping the output to a grep?

CodePudding user response:

There is no inbuilt option for ansible adoc commands to filter output unless supported by module itself(eg setup module).

If you want to compress output in single line you can use "-o" option.

Only option seems to be work with shell to filter out output. I have come up with below command you can give it a try

ansible test_node1 -m script -a "/root/python/test.sh" | grep "\"stdout\":" | tr -d  ','  | echo -e `awk '{print $2}'`
  • Related