4

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.

U880D
  • 8,601
  • 6
  • 24
  • 40
  • Please edit your question and **add the code, logs, output, error messages... in the question body as code blocks**. Using images for this has [numerous disadvantages](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-errors-when-asking-a-question/285557#285557) and is specifically listed as a bad practice in the must read help article [How to ask](/help/how-to-ask). Thanks – Zeitounator Dec 04 '21 at 11:39
  • 1
    thanks for your suggestion. this is my first question in stackoverflow, not familiar writing things. – waterFisher Dec 04 '21 at 14:21
  • There was a similar question here [Ansible: Adhoc task - Separate output by host for clarity](https://stackoverflow.com/questions/68568048/). – U880D Dec 04 '21 at 19:25

1 Answers1

4

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

Similar Q&A

U880D
  • 8,601
  • 6
  • 24
  • 40
  • yes, It works. this manner I have tried yestoday, but not work, and now it works, just so weird. and my manner just trailing ".... && echo ' ' " in it .I don't know what it can display readable in centos8 and not in ubuntu 20.04. maybe some day I can figure out why. many thanks for you help . – waterFisher Dec 05 '21 at 05:12
  • It displays just the same in Ubuntu and CentOS when I tried in fresh docker containers. Double check you did not have some custom callback plugin in your labs machines. – Zeitounator Dec 05 '21 at 08:07
  • very thankfull for your help, I have purged ansible and re-install , but the results still. next time I will update the OS to ubuntu 21 , and hope that problem will be resoled. thanks again. – waterFisher Dec 06 '21 at 10:30