问题描述
我在 centos 中使用 ansible 2.4,尝试在远程服务器中运行以下脚本并获取输出.这里的问题是 yum info 输出也以 json 格式显示.但我只需要显示输出.如何去除json格式.
I am using ansible 2.4 in centos, trying to run the below script in remote servers and getting the output. Here the problem is yum info output is showing with json format also. But i need to display only the output. How to remove the json format.
---
- hosts: GeneralServer
tasks:
- name: Checking the service status
shell: systemctl status {{ item }}
with_items:
- httpd
- crond
- postfix
- sshd
register: service
- debug: var=service
- name: Checking the package info
shell : yum info {{ item }}
with_items:
- httpd
- postfix
register: info
- debug: var=info
- name: Executing the mysql running scripts in mysql
shell: mysql -u username --password mysql -Ns -e 'show databases;'
register: databases
- debug: var=databases
我也是回调模块的新手.请帮我解决这个问题.
Also i am new in callback Module. Please help me to resolve this issue.
推荐答案
你可以尝试使用不同的回调插件来改变你的输出,例如:
You can try to play with different callback plugins to alter your output, e.g.:
$ ANSIBLE_STDOUT_CALLBACK=oneline ansible-playbook myplaybook.yml
$ ANSIBLE_STDOUT_CALLBACK=minimal ansible-playbook myplaybook.yml
但通常您不会避免使用 JSON,因为它是 Ansible 解释数据的方式.
But generally you would not avoid JSON, as it's how Ansible interprets data.
为了减少信息量,您可以使用不同的技术.例如 json_query 过滤器.
To reduce amount of info, you can use different technics. For example json_query filter.
像这样:
- debug:
msg: "{{ info.results | json_query('[].stdout_lines[]') }}"
这篇关于ansible 输出打印不需要的东西.如何格式化和仅显示特定数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!