【问题标题】:ansible is trowing a Syntax Error while loading YAML. did not find expected keyansible 在加载 YAML 时出现语法错误。没有找到预期的密钥
【发布时间】:2021-02-09 12:55:30
【问题描述】:

我的 yml 看起来像:

---
# YAML documents begin with the document separator ---

# The minus in YAML this indicates a list item.  The playbook contains a list 
# of plays, with each play being a dictionary
-
 
  # Hosts: where our play will run and options it will run with
  hosts: localhost
  gather_facts: false

  # Vars: variables that will apply to the play, on all target systems
  vars:
      DDVE_public_IP : 34.107.103.175
      destination_port: 3009
      Instance_id : 8529834022607504819
      S3_bucket_name : bucket_for_ddve_6

  # Tasks: the list of tasks that will be executed within the playbook
  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

  # Handlers: the list of handlers that are executed as a notify key from a task

  # Roles: list of roles to be imported into the play

# Three dots indicate the end of a YAML document
...

ansible-playbook ddve6-post-deploy-object-store.yml

[警告]:未解析任何清单,只有隐式本地主机可用。 [警告]:提供的主机列表为空,只有 localhost 可用。注意 隐式本地主机不匹配“全部”。 错误!我们无法读取 JSON 或 YAML,以下是我们从每个错误中得到的错误: JSON:预期值:第 1 行第 1 列(字符 0)

加载 YAML 时出现语法错误。 没有找到预期的密钥

错误似乎在“/Users/juergen/Documents/DPSCodeAcademy/Ansible/#dev/ddve/ddve6-post-deploy-object-store.yml”中:第 30 行,第 9 列,但可能 根据确切的语法问题,位于文件中的其他位置。

违规行似乎是:

             password: {{ Instance_id }}
       return_content: yes
       ^ here

我不知道错误出现在哪里,因为我在这里找不到响应问题。

【问题讨论】:

    标签: ansible


    【解决方案1】:

    你只是在那个 YAML 文档中有一些缩进错误。选择一个缩进级别(例如,每个级别 2 个空格)并始终坚持下去。许多编辑器都有插件,可以在您编写 YAML 文档时对其进行语法检查。

    以下验证正确:

    ---
    
    - hosts: localhost
      gather_facts: false
    
      vars:
        DDVE_public_IP: 34.107.103.175
        destination_port: 3009
        Instance_id: 8529834022607504819
        S3_bucket_name: bucket_for_ddve_6
    
      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: true
          ignore_errors: true
          register: rest_post
          vars:
            resource_path: rest/v1.0/auth
    
        - name: DEBUG / GOT INFO
          debug:
            msg: "{{ rest_post.json }}"
          when: rest_post.status ==  201
    

    请注意,使用 ... 标记终止 YAML 文档的情况非常罕见。

    【讨论】:

    • 谢谢。现在我遇到了一些 ReST uri 问题
    猜你喜欢
    • 2019-01-10
    • 2019-12-13
    • 2022-07-29
    • 1970-01-01
    • 1970-01-01
    • 2021-12-27
    • 2020-03-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多