Ansible加载YAML时抛出Syntax Error:未找到预期键的问题排查求助
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
- 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.
- 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 justlocalhostin it, then run your playbook withansible-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

