简体   繁体   English

获取 Python Ansible_Runner 模块 STDOUT 键值

[英]Get Python Ansible_Runner module STDOUT Key Value

I'm using ansbile_runner Python module as bellow:我正在使用ansbile_runner Python 模块,如下所示:

import ansible_runner
r = ansible_runner.run(private_data_dir='/tmp/demo', playbook='test.yml')

When I execute the above code, it will show the output without printing in Python.当我执行上面的代码时,它会显示 output 没有在 Python 中打印。 What I want is to save the stdout content into a Python variable for further text processing.我想要的是将标准输出内容保存到 Python 变量中以进行进一步的文本处理。

Did you read the manual under https://ansible-runner.readthedocs.io/en/stable/python_interface/ ?您是否阅读了https://ansible-runner.readthedocs.io/en/stable/python_interface/下的手册? There is an example where you add another parameter, which is called output_fd and that could be a file handler instead of sys.stdout .有一个示例,您可以添加另一个参数,称为output_fd ,它可以是文件处理程序而不是sys.stdout

Sadly, this is a parameter of the run_command function and the documentation is not very good.可悲的是,这是run_command function 的参数,文档不是很好。 A look into the source code at https://github.com/ansible/ansible-runner/blob/devel/ansible_runner/interface.py could help you.查看https://github.com/ansible/ansible-runner/blob/devel/ansible_runner/interface.py的源代码可以帮助您。

According to the implementation details in https://github.com/ansible/ansible-runner/blob/devel/ansible_runner/runner.py it looks like, the run() function always prints to stdout.根据https://github.com/ansible/ansible-runner/blob/devel/ansible_runner/runner.py中的实现细节, run() function 总是打印到标准输出。

According to the interface, there is a boolean flag in run(json_mode=TRUE) that stores the response in JSON (I expect in r instead of stdout) and there is another boolean flag quiet . According to the interface, there is a boolean flag in run(json_mode=TRUE) that stores the response in JSON (I expect in r instead of stdout) and there is another boolean flag quiet .

I played around a little bit.我玩了一下。 The relevant option to avoid output to stdout is quiet=True as run() attribute.避免 output 到 stdout 的相关选项是quiet=True作为 run() 属性。

Ansible_Runner catches the output and writes it to a file in the artifacts directory. Ansible_Runner 捕获 output 并将其写入工件目录中的文件。 Every run() command produces that directory as described in https://ansible-runner.readthedocs.io/en/stable/intro/#runner-artifacts-directory-hierarchy .每个 run() 命令都会生成该目录,如https://ansible-runner.readthedocs.io/en/stable/intro/#runner-artifacts-directory-hierarchy中所述。 So there is a file called stdout in the artifact directory.所以在工件目录中有一个名为 stdout 的文件。 It contains the details.它包含详细信息。 You can read it as JSON.您可以将其读作 JSON。

But also the returned object contains already some relevant data.而且返回的 object 也已经包含一些相关数据。 Here is my example这是我的例子

playbook = 'playbook.yml'
private_data_dir = 'data/' # existing folder with inventory etc
work_dir = 'playbooks/' # contains playbook and roles

try:
  logging.debug('Running ansible playbook {} with private data dir {} in project dir {}'.format(playbook, private_data_dir, work_dir))
  runner = ansible_runner.run(
    private_data_dir=private_data_dir, 
    project_dir=work_dir, 
    playbook=playbook,
    quiet=True,
    json_mode=True
  )

  processed = runner.stats.get('processed')
  failed = runner.stats.get('failures')
  
  # TODO inform backend
  for host in processed:
    if host in failed:
      logging.error('Host {} failed'.format(host))
    else:
      logging.debug('Host {} backupd'.format(host))

  logging.error('Playbook runs into status {} on inventory {}'.format(runner.status, inventory.get('name')))

  if runner.rc != 0:
    # we have an overall failure
  else:
    # success message
except BaseException as err:
  logging.error('Could not process ansible playbook {}\n{}'.format(inventory.get('name'),err))

So this outputs all processed hosts and informs about failures per host.因此,这会输出所有已处理的主机并通知每个主机的故障。 Concrete more output can be found in the stdout file in artifact directory.具体更多output可以在artifact目录下的stdout文件中找到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM