可以使用“column”命令来生成格式化的表格输出,并在Ansible Playbook中调用它。
以下是一个示例:
- name: Run a shell command to get a table output
shell: "mysql -e 'SELECT user, host, authentication_string FROM mysql.user;'"
register: mysql_output
- name: Format the output using column command
shell: "echo '{{ mysql_output.stdout_lines }}' | column -t"
register: formatted_output
- name: Output the formatted output
debug:
var: formatted_output.stdout_lines
在第一个任务中,我们使用shell模块运行mysql查询,并将输出保存在mysql_output变量中。
在第二个任务中,我们使用echo命令将输出传递给column命令,并使用-t选项格式化输出。格式化后的输出将保存在formatted_output变量中。
最后,在第三个任务中,我们使用debug模块输出formatted_output.stdout_lines。
此时,输出将以表格形式呈现。