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

时间:2018-10-08 19:26:15

标签: go ansible ansible-2.x ansible-inventory

我正在尝试读取文件内容。可以说我在/etc/hosts中有一个主机文件。我想阅读文件的内容。当我运行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\\n\\n# The following lines are desirable for IPv6 capable hosts\\n::1 ip6-localhost ip6-loopback\\nfe00::0 ip6-localnet\\nff00::0 ip6-mcastprefix\\nff02::1 ip6-allnodes\\nff02::2 ip6-allrouters\\nff02::3 ip6-allhosts\\nHii 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   

问题是,我只想要此输出

{
  "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\\n\\n# 
  The following lines are desirable for IPv6 capable hosts\\n::1 ip6-localhost ip6- 
  loopback\\nfe00::0 ip6-localnet\\nff00::0 ip6-mcastprefix\\nff02::1 ip6- 
  allnodes\\nff02::2 ip6-allrouters\\nff02::3 ip6-allhosts\\nHii 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调用ansible-playbook命令

0 个答案:

没有答案