Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible ad-hoc output: How to split every node result with new line?

In Ubuntu 20.04 and Ansible version 2.9, using Ansible ad-hoc command like:

ansible all -m shell -a "df -h" 

will print like below

app2 | CHANGED | rc=0 >>
Filesystem           Size  Used Avail Use% Mounted on
devtmpfs             215M     0  215M   0% /dev
tmpfs                233M     0  233M   0% /dev/shm
tmpfs                233M  6.3M  227M   3% /run
tmpfs                233M     0  233M   0% /sys/fs/cgroup
/dev/mapper/cl-root   52G  2.7G   50G   6% /
/dev/sda1           1014M  198M  817M  20% /boot
/dev/mapper/cl-home   26G  213M   26G   1% /home
tmpfs                 47M     0   47M   0% /run/user/1000
app1 | CHANGED | rc=0 >>
Filesystem           Size  Used Avail Use% Mounted on
devtmpfs             215M     0  215M   0% /dev
tmpfs                233M     0  233M   0% /dev/shm
tmpfs                233M  6.3M  227M   3% /run
tmpfs                233M     0  233M   0% /sys/fs/cgroup
/dev/mapper/cl-root   52G  2.7G   50G   6% /
/dev/sda1           1014M  198M  817M  20% /boot
/dev/mapper/cl-home   26G  213M   26G   1% /home
tmpfs                 47M     0   47M   0% /run/user/1000
db1 | CHANGED | rc=0 >>
Filesystem           Size  Used Avail Use% Mounted on
devtmpfs             215M     0  215M   0% /dev
tmpfs                233M     0  233M   0% /dev/shm
tmpfs                233M  6.3M  227M   3% /run
tmpfs                233M     0  233M   0% /sys/fs/cgroup
/dev/mapper/cl-root   52G  3.0G   49G   6% /
/dev/sda1           1014M  198M  817M  20% /boot
/dev/mapper/cl-home   26G  213M   26G   1% /home
tmpfs                 47M     0   47M   0% /run/user/1000

But what I want to have is every node result split like below, node results with new line

app2 | CHANGED | rc=0 >>
Filesystem           Size  Used Avail Use% Mounted on
devtmpfs             215M     0  215M   0% /dev
tmpfs                233M     0  233M   0% /dev/shm
tmpfs                233M  6.3M  227M   3% /run
tmpfs                233M     0  233M   0% /sys/fs/cgroup
/dev/mapper/cl-root   52G  2.7G   50G   6% /
/dev/sda1           1014M  198M  817M  20% /boot
/dev/mapper/cl-home   26G  213M   26G   1% /home
tmpfs                 47M     0   47M   0% /run/user/1000

app1 | CHANGED | rc=0 >>
Filesystem           Size  Used Avail Use% Mounted on
devtmpfs             215M     0  215M   0% /dev
tmpfs                233M     0  233M   0% /dev/shm
tmpfs                233M  6.3M  227M   3% /run
tmpfs                233M     0  233M   0% /sys/fs/cgroup
/dev/mapper/cl-root   52G  2.7G   50G   6% /
/dev/sda1           1014M  198M  817M  20% /boot
/dev/mapper/cl-home   26G  213M   26G   1% /home
tmpfs                 47M     0   47M   0% /run/user/1000

db1 | CHANGED | rc=0 >>
Filesystem           Size  Used Avail Use% Mounted on
devtmpfs             215M     0  215M   0% /dev
tmpfs                233M     0  233M   0% /dev/shm
tmpfs                233M  6.3M  227M   3% /run
tmpfs                233M     0  233M   0% /sys/fs/cgroup
/dev/mapper/cl-root   52G  3.0G   49G   6% /
/dev/sda1           1014M  198M  817M  20% /boot
/dev/mapper/cl-home   26G  213M   26G   1% /home
tmpfs                 47M     0   47M   0% /run/user/1000

While learning Ansible in CentOS 8.4, I've never bothered this output things. But back in Ubuntu I found that if every output result stuck together it is hard to view. Please help me to resolve this 'bother thing'.

My ansible.cfg

[defaults]
inventory = hosts.ini
interpreter_python  = /usr/libexec/platform-python

My inventory file content

# Application servers
[app]
app1
app2

# Database servers
[db]
db1 

# Group 'multi' with all servers
[multi:children]
app
db

# Variables that will be applied to all servers
[multi:vars]
ansible_user = vagrant
ansible_ssh_private_key_file=~/.vagrant.d/insecure_private_key

I hope all this detail can help people to debug and find the right answer for me, many thanks.

like image 821
waterFisher Avatar asked Oct 23 '25 09:10

waterFisher


1 Answers

Since I have a similar requirement depending on the task sometimes, I use the following approach:

ansible test --user ${USER} --ask-pass --module-name shell --args "echo 'Output format test'; echo ' ';"

resulting in an output of

test1.example.com | CHANGED | rc=0 >>
Output format test

test2.example.com | CHANGED | rc=0 >>
Output format test
 

In other words, just add an ; echo ' '; at the end.

Further information regarding output formatting can be found under

  • How to format Ansible output
  • Ansible stdout formatting
  • How can I get the output of Ansible ad-hoc command in JSON, CSV or other format

Similar Q&A

  • Ansible ad-hoc task: How to separate output by host for clarity?
like image 63
U880D Avatar answered Oct 26 '25 05:10

U880D



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!