使用Ansible从Json输出中提取数据
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用Ansible从Json输出中提取数据相关的知识,希望对你有一定的参考价值。
我写了一个Ansible playbook来从Cloudflare平台检索DNS条目。在将条目导出到CSV文件之前,我正在尝试捕获总记录数。但无论我如何尝试,它总是显示VARIABLE IS NOT DEFINED
错误。
以下是我的剧本:
- hosts: 127.0.0.1
connection: local
tasks:
- name: Listing the cloudflare DNS records...
uri:
url: https://api.cloudflare.com/client/v4/zones/zoneid/dns_records?page=1&per_page=1&type=A
method: GET
headers:
X-Auth-Email: "myemail@gmail.com"
X-Auth-Key: "jvnnf345mnbhfgj5"
Content-Type: "application/json"
register: cloudflare_records
- name: Displaying the records...
debug:
var: cloudflare_records
上面的playbook正在按预期工作并显示以下输出:
ok: [127.0.0.1] =>
"cloudflare_records":
"changed": false,
"msg": "All items completed",
"results": [
"_ansible_ignore_errors": null,
"_ansible_item_label": 1,
"_ansible_item_result": true,
"_ansible_no_log": false,
"_ansible_parsed": true,
"cache_control": "no-store, no-cache, must-revalidate, post-check=0, pre-check=0",
"cf_ray": "4b64c01132-SIN",
"changed": false,
"connection": "close",
"content_type": "application/json",
"cookies":
"__cf_bm": "3852871f6e8f0ff02f410491f-1552383037-1800-Acq+ObfnSR8WRCVLvsYmprM61p3DayCFn9W12LhqPFpzKqVCUvi9oCxaYUhN9XfNgdEvZc3RTa79NxufrMv8aoU=",
"__cfduid": "dc8d780e98d213b61f1f1552383037"
,
"cookies_string": "__cf_bm=3852871f6e8f02f410491f-1552383037-1800-Acq+ObfnSR8WRCVLvsYmprM61p3DayCFn9W12LhqPFpzKqVCUvi9oCxaYUhN9XfNgdEvZc3RTa79NxufrMv8aoU=; __cfduid=dc8d787704f9af702c80e98d213b61f1f1552383037",
"date": "Tue, 12 Mar 2019 09:30:37 GMT",
"expect_ct": "max-age=604800, report-uri=\"https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct\"",
"expires": "Sun, 25 Jan 1981 05:00:00 GMT",
"failed": false,
"invocation":
"module_args":
"attributes": null,
"backup": null,
"body": null,
"body_format": "raw",
"client_cert": null,
"client_key": null,
"content": null,
"creates": null,
"delimiter": null,
"dest": null,
"directory_mode": null,
"follow": false,
"follow_redirects": "safe",
"force": false,
"force_basic_auth": false,
"group": null,
"headers":
"Content-Type": "application/json",
"X-Auth-Email": "myemail@gmail.com",
"X-Auth-Key": "jvnnf345mnbhfgj5"
,
"http_agent": "ansible-httpget",
"method": "GET",
"mode": null,
"owner": null,
"regexp": null,
"remote_src": null,
"removes": null,
"return_content": false,
"selevel": null,
"serole": null,
"setype": null,
"seuser": null,
"src": null,
"status_code": [
200
],
"timeout": 30,
"unsafe_writes": null,
"url": "https://api.cloudflare.com/client/v4/zones/myzoneid/dns_records?page=1&per_page=1&type=A",
"url_password": null,
"url_username": null,
"use_proxy": true,
"validate_certs": true
,
"item": 1,
"json":
"errors": [],
"messages": [],
"result": [
"content": "x.x.x.x",
"created_on": "2018-01-22T05:00:45.020352Z",
"id": "f5bc1d3ce029b0ffd6e21b617c1140a3",
"locked": false,
"meta":
"auto_added": false,
"managed_by_apps": false,
"managed_by_argo_tunnel": false
,
"modified_on": "2018-01-22T05:00:45.020352Z",
"name": "a.example.com",
"proxiable": true,
"proxied": false,
"ttl": 1,
"type": "A",
"zone_id": "6c5f85f74db5f8af73a",
"zone_name": "example.com"
],
"result_info":
"count": 1,
"page": 1,
"per_page": 1,
"total_count": 138,
"total_pages": 138
,
"success": true
,
"msg": "OK (unknown bytes)",
"pragma": "no-cache",
"redirected": false,
"served_in_seconds": "0.053",
"server": "cloudflare",
"set_cookie": "__cfduid=dc8d787704f1f1552383037; expires=Wed, 11-Mar-20 09:30:37 GMT; path=/; domain=.cloudflare.com; HttpOnly, __cf_bm=3852871f6e8f0ff5ed65d01db10ee402f410491f-137-1800-Acq+ObfnSR8WRCVLvsYmprM61p3DayCFn9W12LhqPFpzKqVCUvi9o12-Mar-19 10:00:37 GMT; domain=.cloudflare.com; HttpOnly",
"status": 200,
"strict_transport_security": "max-age=15780000; includeSubDomains",
"transfer_encoding": "chunked",
"url": "https://api.cloudflare.com/client/v4/zones/myzoneid/dns_records?page=1&per_page=1&type=A",
"vary": "Accept-Encoding",
"x_content_type_options": "nosniff",
"x_frame_options": "SAMEORIGIN",
"x_ssl_protocol": "TLSv1.2"
]
现在使用上面的json输出,我试图捕获total_pages
值。以下是我的安全任务:
- name: Displaying the gathered records...
debug:
var: cloudflare_records.results.result_info.total_pages
但收到以下错误:
VARIABLE IS NOT DEFINED!
我尝试使用json_query
方法,但对我不起作用。请指教。
答案
results
是一个数组,所以你需要获得它的第一个记录,即results[0]
。请试试
- name: Displaying the gathered records...
debug:
var: cloudflare_records.results.0.json.result_info.total_pages
或者也许是cloudflare_records.results.0.result_info.total_pages
在解析json时,我更喜欢使用debug-msg而不是debug-var:
- debug:
msg: " cloudflare_records.results.0.json.result_info.total_pages "
如果您有多个结果:
- debug:
msg: " item.json.result_info.total_pages "
with_items: cloudflare_records.results
以上是关于使用Ansible从Json输出中提取数据的主要内容,如果未能解决你的问题,请参考以下文章