【发布时间】:2019-03-12 20:32:15
【问题描述】:
我编写了一个 Ansible 剧本来从 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
上面的剧本按预期工作并显示以下输出:
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 方法,但对我不起作用。请指教。
【问题讨论】: