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

Ansible加载YAML时抛出Syntax Error:未找到预期键的问题排查求助

Ansible YAML Syntax Error: "did not find expected key" in Playbook

Let's break down your issue and fix it quickly. The error message points to the return_content: yes line, but the actual problem lies in the line right above it.

What's Causing the Error?

Your Instance_id variable holds a large numeric value (8529834022607504819). In YAML, unquoted numbers are treated as numeric types instead of strings. When Ansible renders the {{ Instance_id }} template, the YAML parser gets confused by this numeric value in a context where it expects a string (the password field). This confusion throws off the parser's understanding of the YAML structure, leading it to report a missing key on the next line.

The Fix

Wrap the variable in double quotes to force YAML to treat it as a string. Update your body section like this:

body:
  username: sysadmin
  password: "{{ Instance_id }}"

Quick Additional Checks

  1. Indentation: Double-check that you're using consistent spaces (4 spaces is standard for Ansible/YAML) instead of mixing tabs and spaces. Your current indentation looks correct, but this is a common YAML pitfall.
  2. Inventory Warnings: The warnings about missing inventory are harmless if you're only targeting localhost, but if you want to eliminate them, create a simple inventory file (e.g., inventory.ini) with just localhost in it, then run your playbook with ansible-playbook -i inventory.ini ddve6-post-deploy-object-store.yml.

Here's the corrected snippet of your task for reference:

tasks:
  - name: login access token
    uri:
      url: https://{{ DDVE_public_IP }}:{{ destination_port }}/{{ resource_path }}
      method: POST
      headers:
        Content-Type: application/json
      body_format: json
      body:
        username: sysadmin
        password: "{{ Instance_id }}"
      return_content: yes
      ignore_errors: yes
    register: rest_post
    vars:
      resource_path: rest/v1.0/auth
  - name: DEBUG / GOT INFO
    debug:
      msg: "{{ rest_post.json }}"
    when: rest_post.status == 201

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.29 07:37:38