Ansible:带有参数“body_format: json”的“uri”模块报告“HTTP 错误 400:错误请求”下载

问题描述 投票:0回答:1

从特定网站提取 JSON 时,我在使用 Ansible

uri
模块时遇到一些问题。

Ansible 代码

 - name: check for Tenable Agent updates.
   uri:
     url: https://www.tenable.com/downloads/api/v1/public/pages/nessus-agents
     follow_redirects: none
     validate_certs: false
     return_content: yes
     body_format: json
   register: tenable_result

运行时遵循 Ansible 输出:

完整错误消息

TASK [agent-check : check for Tenable Agent updates.] ***************************************************************************************************************************
fatal: [testhost.local]: FAILED! => {"cf_cache_status": "DYNAMIC", "cf_ray": "66e527b55f742542-SJC", "changed": false, "connection": "close", "content": "Bad Request", "content_length": "11", "content_type": "text/plain; charset=utf-8", "date": "Tue, 13 Jul 2021 20:10:30 GMT", "elapsed": 0, "expect_ct": "max-age=604800, report-uri=\"https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct\"", "msg": "Status code was 400 and not [200]: HTTP Error 400: Bad Request", "redirected": false, "server": "cloudflare", "set_cookie": "AWSALB=mjbt0/pPG5uYbItH6JZZsMNU31tfIYcjN3EzGUGGwe0yh1IyClNI3QZwwaIsvvxTBsxAtONNkX5ikvZUQxB/3m5RsPKsZKCs9GC/shJpEAmAiRTvDFgzMdSEsBl2; Expires=Tue, 20 Jul 2021 20:10:30 GMT; Path=/, AWSALBCORS=mjbt0/pPG5uYbItH6JZZsMNU31tfIYcjN3EzGUGGwe0yh1IyClNI3QZwwaIsvvxTBsxAtONNkX5ikvZUQxB/3m5RsPKsZKCs9GC/shJpEAmAiRTvDFgzMdSEsBl2; Expires=Tue, 20 Jul 2021 20:10:30 GMT; Path=/; SameSite=None; Secure, __cf_bm=cf4aba5cd769c19f68e8a6b84448295bb7acca50-1626207030-1800-AcDXU5VzEKMNjpyeGluMOFzCeIfWcxvrpS8wIms3LQHysBisfPjp051BUkVGysLvq7pPwEfXNDrNwGV7VV2GPbI=; path=/; expires=Tue, 13-Jul-21 20:40:30 GMT; domain=.tenable.com; HttpOnly; Secure; SameSite=None", "status": 400, "strict_transport_security": "max-age=31536000", "url": "https://www.tenable.com/downloads/api/v1/public/pages/nessus-agents", "x_content_type_options": "nosniff"}

简短的错误消息

"msg": "Status code was 400 and not [200]: HTTP Error 400: Bad Request"

问题

我如何告诉 Ansible 预测此重定向并满足 Cloudflare 的任何需求,以便我继续处理

tenable.com
上的文件和 ID 数据?

我知道 URL 有效并且可以输出所需的 JSON 数据,因为我可以通过

curl
jq
解析文件名和下载 ID,就像

$ curl -s https://www.tenable.com/downloads/api/v1/public/pages/nessus-agents| jq '.downloads[] | "\(.file) \(.id)"'
"Agent_plugins_expires_2021-07-15.tgz 13183"
"NessusAgent-8.3.0-x64.msi 13130"
"NessusAgent-8.3.0-Win32.msi 13131"
"NessusAgent-8.3.0.dmg 13132"
"NessusAgent-8.3.0-debian6_amd64.deb 13134"
"NessusAgent-8.3.0-debian6_i386.deb 13135"
"NessusAgent-8.3.0-amzn.x86_64.rpm 13133"
"NessusAgent-8.3.0-es5.x86_64.rpm 13136"
"NessusAgent-8.3.0-es5.i386.rpm 13137"
"NessusAgent-8.3.0-es6.x86_64.rpm 13138"
"NessusAgent-8.3.0-es6.i386.rpm 13139"
"NessusAgent-8.3.0-es7.x86_64.rpm 13140"
"NessusAgent-8.3.0-amzn2.aarch64.rpm 13149"
"nessus-agent-updates-8.3.0.tar.gz 13150"
"NessusAgent-8.3.0-es8.x86_64.rpm 13141"
"NessusAgent-8.3.0-fc20.x86_64.rpm 13142"
"NessusAgent-8.3.0-suse11.x86_64.rpm 13143"
"NessusAgent-8.3.0-suse11.i586.rpm 13144"
"NessusAgent-8.3.0-suse12.x86_64.rpm 13145"
"NessusAgent-8.3.0-suse15.x86_64.rpm 13146"
"NessusAgent-8.3.0-ubuntu1110_amd64.deb 13147"
"NessusAgent-8.3.0-ubuntu1110_i386.deb 13148"
"tenable-2048.gpg 7000"
"tenable-1024.gpg 6998"

知道我的 Ansible 代码中缺少什么吗?

python curl ansible uri nessus
1个回答
0
投票

错误信息

"msg": "Status code was 400 and not [200]: HTTP Error 400: Bad Request"

可能是由于您使用参数:

body_format: json
,主要用于上传内容而不是下载。

一个最小的示例手册

---
- hosts: localhost
  become: false
  gather_facts: false

  tasks:

  - name: Get Content
    uri:
      url: "https://www.tenable.com/downloads/api/v1/public/pages/nessus-agents"
      method: GET
      validate_certs: true
      return_content: true
      status_code: 200
    register: response

  - name: Show full response.content
    debug:
      msg: "{{ (response.content | from_yaml)['downloads'] }}"

  - name: Show only .downloads.file
    debug:
      msg: "{{ item.file }}"
    loop_control:
      label: "{{ item.name }}"
    loop: "{{ (response.content | from_yaml)['downloads'] }}"

将产生

的输出
TASK [Show only .downloads.file] ***********************************
ok: [localhost] => (item=RPM-GPG-KEY-Tenable-4096) =>
  msg: tenable-4096.gpg
ok: [localhost] => (item=Agent_plugins_expires_2023-12-09.tgz) =>
  msg: Agent_plugins_expires_2023-12-09.tgz
ok: [localhost] => (item=NessusAgent-10.4.4-Win32.msi) =>
  msg: NessusAgent-10.4.4-Win32.msi
ok: [localhost] => (item=NessusAgent-10.4.4-suse15.x86_64.rpm) =>
  msg: NessusAgent-10.4.4-suse15.x86_64.rpm
ok: [localhost] => (item=NessusAgent-10.4.4.dmg) =>
  msg: NessusAgent-10.4.4.dmg
ok: [localhost] => (item=NessusAgent-10.4.4-x64.msi) =>
  msg: NessusAgent-10.4.4-x64.msi
ok: [localhost] => (item=NessusAgent-10.4.4-es7.x86_64.rpm) =>
  msg: NessusAgent-10.4.4-es7.x86_64.rpm
ok: [localhost] => (item=NessusAgent-10.4.4-fc34.x86_64.rpm) =>
  msg: NessusAgent-10.4.4-fc34.x86_64.rpm
ok: [localhost] => (item=NessusAgent-10.4.4-suse12.x86_64.rpm) =>
  msg: NessusAgent-10.4.4-suse12.x86_64.rpm
ok: [localhost] => (item=NessusAgent-10.4.4-es8.aarch64.rpm) =>
  msg: NessusAgent-10.4.4-es8.aarch64.rpm
ok: [localhost] => (item=NessusAgent-10.4.4-ubuntu1404_amd64.deb) =>
  msg: NessusAgent-10.4.4-ubuntu1404_amd64.deb
ok: [localhost] => (item=NessusAgent-10.4.4-amzn2.aarch64.rpm) =>
  msg: NessusAgent-10.4.4-amzn2.aarch64.rpm
ok: [localhost] => (item=NessusAgent-10.4.4-amzn2.x86_64.rpm) =>
  msg: NessusAgent-10.4.4-amzn2.x86_64.rpm
ok: [localhost] => (item=nessus-agent-updates-10.4.4.tar.gz) =>
  msg: nessus-agent-updates-10.4.4.tar.gz
ok: [localhost] => (item=NessusAgent-10.4.4-es8.x86_64.rpm) =>
  msg: NessusAgent-10.4.4-es8.x86_64.rpm
ok: [localhost] => (item=NessusAgent-10.4.4-debian10_i386.deb) =>
  msg: NessusAgent-10.4.4-debian10_i386.deb
ok: [localhost] => (item=NessusAgent-10.4.4-es6.x86_64.rpm) =>
  msg: NessusAgent-10.4.4-es6.x86_64.rpm
ok: [localhost] => (item=NessusAgent-10.4.4-es7.aarch64.rpm) =>
  msg: NessusAgent-10.4.4-es7.aarch64.rpm
ok: [localhost] => (item=NessusAgent-10.4.4-debian10_amd64.deb) =>
  msg: NessusAgent-10.4.4-debian10_amd64.deb
ok: [localhost] => (item=NessusAgent-10.4.4-ubuntu1804_aarch64.deb) =>
  msg: NessusAgent-10.4.4-ubuntu1804_aarch64.deb
ok: [localhost] => (item=RPM-GPG-KEY-Tenable-2048) =>
  msg: tenable-2048.gpg
© www.soinside.com 2019 - 2024. All rights reserved.