以正确的可读格式(如JSON)获取ansible任务的输出[重复]

这个问题已经在这里有了答案:

  • Ansible:如何通知python脚本有关任务的结果? 2个答案。 ansible输出打印不需要的东西。 如何格式化和仅显示特定数据的答案1 我正在尝试读取文件的内容。 可以说我有一个主机文件 /etc/hosts.我想要阅读文件的内容。当我运行ansible playbook它包含几个日志包含任务名称和符号。 ansible剧本文件:
    - hosts: all
      remote_user: "{{ ansible_user }}"
      vars:
        ansible_become_pass: "{{ ansible_ssh_pass }}"
    
      tasks:
        - name: Read file
          command: cat /etc/hosts
          register: response
    
        - debug: msg="Content of file is {{ response }}"
    

    ansible发出的日志:

    $ ansible-playbook -i inventory.yml playbook.yml 
    
    PLAY [all] *************************************************************************************************************************************************************************************************
    
    TASK [Gathering Facts] *************************************************************************************************************************************************************************************
    ok: [18.222.139.140]
    
    TASK [Read file] *******************************************************************************************************************************************************************************************
    changed: [18.222.139.140]
    
    TASK [debug] ***********************************************************************************************************************************************************************************************
    ok: [18.222.139.140] => {
        "msg": "Content of file is {'stderr_lines': [], u'changed': True, u'end': u'2018-10-08 19:21:52.968466', 'failed': False, u'stdout': u'127.0.0.1 localhost\
    \
    # The following lines are desirable for IPv6 capable hosts\
    ::1 ip6-localhost ip6-loopback\
    fe00::0 ip6-localnet\
    ff00::0 ip6-mcastprefix\
    ff02::1 ip6-allnodes\
    ff02::2 ip6-allrouters\
    ff02::3 ip6-allhosts\
    Hii just testing', u'cmd': [u'cat', u'/etc/hosts'], u'rc': 0, u'start': u'2018-10-08 19:21:52.966509', u'stderr': u'', u'delta': u'0:00:00.001957', 'stdout_lines': [u'127.0.0.1 localhost', u'', u'# The following lines are desirable for IPv6 capable hosts', u'::1 ip6-localhost ip6-loopback', u'fe00::0 ip6-localnet', u'ff00::0 ip6-mcastprefix', u'ff02::1 ip6-allnodes', u'ff02::2 ip6-allrouters', u'ff02::3 ip6-allhosts', u'Hii just testing']}"
    }
    
    PLAY RECAP *************************************************************************************************************************************************************************************************
    18.222.139.140             : ok=3    changed=1    unreachable=0    failed=0   
    

    The issue is, I just want this output

    {
      "msg": "Content of file is {'stderr_lines': [], u'changed': True, u'end': u'2018- 
      10-08 19:21:52.968466', 'failed': False, u'stdout': u'127.0.0.1 localhost\
    \
    # 
      The following lines are desirable for IPv6 capable hosts\
    ::1 ip6-localhost ip6- 
      loopback\
    fe00::0 ip6-localnet\
    ff00::0 ip6-mcastprefix\
    ff02::1 ip6- 
      allnodes\
    ff02::2 ip6-allrouters\
    ff02::3 ip6-allhosts\
    Hii just testing', 
      u'cmd': [u'cat', u'/etc/hosts'], u'rc': 0, u'start': u'2018-10-08 19:21:52.966509', 
      u'stderr': u'', u'delta': u'0:00:00.001957', 'stdout_lines': [u'127.0.0.1 
      localhost', u'', u'# The following lines are desirable for IPv6 capable hosts', 
      u'::1 ip6-localhost ip6-loopback', u'fe00::0 ip6-localnet', u'ff00::0 ip6- 
      mcastprefix', u'ff02::1 ip6-allnodes', u'ff02::2 ip6-allrouters', u'ff02::3 ip6- 
      allhosts', u'Hii just testing']}"
    }
    
    或可以通过程序解析的任何其他输出。 任何帮助/建议,将不胜感激。 我正在使用go-lang进行调用

    命令