简体   繁体   English

如何在 json 中获取本地机器的 ansible 事实?

[英]How can I get the ansible facts of my local machine in a json?

I'm trying to gather the facts from my local machine using ansible_runner :我正在尝试使用ansible_runner从我的本地机器收集事实:

import ansible_runner, json


res = ansible_runnner.run(
    module='setup',
    host_pattern='localhost',
)
json.loads(res.stdout.read())

But the json breaks because the data is malformed.但是json由于数据格式错误而中断。 I tried doing it with command line ansible: ansible -m setup localhost > bla and then changing the file and then trying to json.load it but still got stuck.我尝试使用命令行 ansible 执行此操作: ansible -m setup localhost > bla然后更改文件,然后尝试json.load它但仍然卡住了。

Is there an ansible built-in for this?有内置的 ansible 吗?

The output from Ansible isn't really meant to be machine-parseable.来自 Ansible 的 output 并不是真正意义上的机器可解析的。 For example, the content produced by res.stdout.read() in your example includes ANSI color codes, which are nice for display but render the data invalid even if it were otherwise valid JSON.例如,在您的示例中由res.stdout.read()生成的内容包括 ANSI 颜色代码,这些代码很适合显示,但即使数据在其他方面有效,也会使数据无效 JSON。

You can access the result of the setup module in structured form (that is, already parsed into Python data structures) via the events attribute of your res variable.您可以通过res变量的events属性以结构化形式访问setup模块的结果(即,已经解析为 Python 数据结构)。

For example:例如:

>>> import ansible_runner
>>> res = ansible_runner.run(module='setup', host_pattern='localhost')
>>> setup_results= next(x for x in res.events if  x['event'] == 'runner_on_ok' and x['event_data']['task'] == 'setup')
>>> facts = setup_results['event_data']['res']['ansible_facts']
>>> print(facts['ansible_processor_vcpus'])
8

暂无
暂无

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

相关问题 如何使用Ansible在未知计算机上收集所有事实? - How to gather all facts on an unknown machine with Ansible? 如何在库存插件中获取 Ansible 事实 - How to get Ansible facts within inventory plugin Django Rest框架:我希望我的GET响应是指向本地计算机上媒体目录中所有文件链接的JSON对象。 我该怎么做呢? - Django Rest framework: I want my GET response to be a JSON object of links to all files in my media directory on my local machine. How do i do this? 如何将我抓取的文本格式的 json 数据集保存到本地机器中,以及如何将文件读入 Pandas DataFrame? - How can i save my scraped json dataset that is a text format into my local machine and also how can i read the file into Pandas DataFrame? 如何在仍然使用本地文件作为我的游戏的同时以编程方式运行 ansible play? - How can I run an ansible play programmatically while still using a local file as my play? 如何将某些数据从Google App脚本发布到本地计算机上的python脚本中? - How can I post some data from google app script to my python script on the local machine? 我怎么知道用户是否通过python脚本中的ssh连接到本地计算机? - How can I know if the user is connected to the local machine via ssh in my python script? 如何将部署的 ML model 下载/导出到我的本地计算机? - How can I download/export the deployed ML model to my local machine? 如何将 .parquet 文件从本地计算机上传到 Azure Storage Data Lake Gen2? - How can I upload a .parquet file from my local machine to Azure Storage Data Lake Gen2? 在python中如何获取本地服务器的日期和时间? - In python how can I get the date and time of my local server?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM