How to print on screen in Ansible
If you can’t but really want
Sometime you want to print something on the screen in the middle of playbook. You have measly debug
module, and, that’s all.
How about multi-line text with colors, formatting, etc, etc?
Officially you can’t do this. But if you are REAALLY motivated… Nothing can stop you.
The solution
Use /dev/tty
Example
---
- hosts: localhost
gather_facts: false
tasks:
- shell: "echo '{{'='*10}} {{red}}H{{green}}E{{cyan}}L{{red}}L{{green}}O{{cyan}}!{{CSI}}0m {{'='*10}}' > /dev/tty"
vars:
CSI: "\x1B["
red: '{{CSI}}31m'
green: '{{CSI}}32m'
cyan: '{{CSI}}36m'
And, of course, you can jinja as you want to write a lengthy messages, you can print files (cat
), show output for other utilities (echo reg.stdout > /dev/tty
), etc.
One important note: we are talking about localhost /dev/tty
, don’t try this on remote servers.
If example with escape codes is too scary, here the simpler example:
- hosts: localhost
gather_facts: false
tasks:
- shell: head /etc/passwd > /dev/tty
Have fun!