Getting the output of just "stdout_lines" or "stdout" when running an ansible ad-hoc command

Viewed 59

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?

2 Answers

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}'`

In my point of view, the in the original question tried approach to gather service statuses should be avoided since that example seems to be an anti-pattern for Ansible. However, there are options to fulfill the requirement for a report.

A simpler solution is either

ansible test -m systemd -a 'name=cntlm enabled=true'
test.example.com | SUCCESS => {
    "changed": false,
    "enabled": true,
    "name": "cntlm",
    "status": {
<a lot of output>
    }
}

or

ansible test -m shell -a 'systemctl status cntlm'
test.example.com | CHANGED | rc=0 >>
● cntlm.service - CNTLM HTTP Accelerator For NTLM Secured Proxies Authenticator
   Loaded: loaded (/usr/lib/systemd/system/cntlm.service; enabled; vendor preset: disabled)
   Active: active (running) since Thu 2022-09-01 09:00:00 CEST; 1 weeks 4 days ago
 Main PID: 234567 (cntlm)
   CGroup: /system.slice/cntlm.service
           └─234567 /usr/sbin/cntlm -c /etc/cntlm.conf -U cntlm -P /run/cntlm/cntlmd.pid

or almost the requested, after enabling callback plugin for ad hoc commands

ansible test -m shell -a 'systemctl status cntlm'

PLAY [Ansible Ad-Hoc] **************************************************************************************************
Monday 01 September 2022  09:00:00 +0200 (0:00:00.071)       0:00:00.071 ******

TASK [shell] ***********************************************************************************************************
changed: [test.example.com]

PLAY RECAP *************************************************************************************************************
test.example.com  : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Monday 01 September 2022  09:00:00 +0200 (0:00:02.171)       0:00:02.243 ******
===============================================================================
shell ----------------------------------------------------------------------------------------------------------- 2.17s
Playbook run took 0 days, 0 hours, 0 minutes, 2 seconds

Config

[defaults]
bin_ansible_callbacks   = True

Further Q&A


Finally, If you are now are interested in customizing the output more, you'll probably need to start Developing plugins and creating your own Callback plugin. The source of lib/ansible/plugins/callback/default.py can be a good start.

Related