You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ansible文档示例使用报错:‘ansible.builtin.uri’非Play有效属性

问题解决:Ansible Playbook模块任务报错处理

错误原因

你复制的官方文档示例只是单个任务片段,但Ansible Playbook必须遵循完整的Play结构:每个Play需要指定hosts(目标主机),然后在tasks列表下定义具体的模块任务。直接运行任务片段会导致Ansible把它当成Play的属性,所以报错'ansible.builtin.uri' is not a valid attribute for a Play。

正确的Playbook写法

参照你能正常运行的ping Playbook结构,把uri模块的任务放到tasks下面即可,完整代码如下:

---
- name: Check web page status and content
  hosts: localhost  # 指定目标主机,和你的ping示例保持一致
  tasks:
    - name: Check that a page returns a status 200 and fail if the word AWESOME is not in the page contents
      ansible.builtin.uri:
        url: http://www.example.com
        return_content: yes
      register: this
      failed_when: "'AWESOME' not in this.content"

关键说明

  • 最外层的- name: ...是一个Play,必须包含hosts字段指定执行的目标主机
  • 所有模块任务(比如uri、ping)都必须嵌套在tasks列表下
  • 你之前的ping Playbook能正常运行,核心原因就是它完整包含了Play的必备结构:hosts + tasks

内容的提问来源于stack exchange,提问作者alfrerra

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 03:35:16