ansible输出打印不需要的东西。如何格式化和仅显示特定数据

ansible output printing unwanted things. how to format and display only specific data's

我在 centos 中使用 ansible 2.4,尝试 运行 远程服务器中的以下脚本并获取输出。这里的问题是 yum 信息输出也以 json 格式显示。但我只需要显示输出。如何删除 json 格式。

---

- 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 

我也是回调模块的新手。请帮我解决这个问题。

是否可以仅显示 stdout_lines 个值。

您可以尝试使用不同的回调插件来改变您的输出,例如:

$ ANSIBLE_STDOUT_CALLBACK=oneline ansible-playbook myplaybook.yml
$ ANSIBLE_STDOUT_CALLBACK=minimal ansible-playbook myplaybook.yml

但通常您不会避免 JSON,因为它是 Ansible 解释数据的方式。

要减少信息量,您可以使用不同的技术。例如json_query过滤器。

像这样:

- debug:
    msg: "{{ info.results | json_query('[].stdout_lines[]') }}"