ansible 2.7.10 中出现此错误的原因是什么 - "ERROR! playbook entries must be either a valid play or an include statement|"

What is the reason of this error in ansible 2.7.10 - "ERROR! playbook entries must be either a valid play or an include statement|"

我正在观看 Ansible 教程,其中我必须部署剧本。剧本代码如下:

- name:"Do a demo"
  hosts:groupA

  tasks:!!seq
    - name:demo task 1
      debug:!!seq
        msg:"this is task 1"

    - name:demo task 2
      debug:!!seq
        msg:"this is task 2"

- name:"Do another demo"
  hosts:groupB

  tasks:!!seq
    - name:demo task 3
      debug:!!seq
      msg:"this is task 3"

    - name:demo task 4
      debug:!!seq
        msg:"this is task 4"

当我尝试使用 ansible-playbook -i hosts demoplays.yaml 命令部署上述剧本时,出现了错误:-

ERROR! playbook entries must be either a valid play or an include statement

The error appears to have been in '/home/user/demoplays.yaml': line 1, column 3, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:


- name:"Do a demo"
  ^ here

起初我认为是 yaml 语法错误,但是 YAML linter 验证它是正确的。我在我的初级 OS Loki 系统上使用 ansible 2.7.10。我刚开始学习 Ansible 和 YAML,还没有找到任何提示为什么会发生这个错误!

您的 YAML 相当于:

[
  "name:\"Do a demo\" hosts:groupA\ntasks:!!seq - name:demo task 1 debug:!!seq msg:\"this is task 1\"\n- name:demo task 2 debug:!!seq msg:\"this is task 2\"", 
  "name:\"Do another demo\" hosts:groupB\ntasks:!!seq - name:demo task 3 debug:!!seq msg:\"this is task 3\"\n- name:demo task 4 debug:!!seq msg:\"this is task 4\""
]

这可能不是您想要的。尝试更改此设置,以便 YAML 中根级别序列的项目成为映射:

- name: "Do a demo"
  hosts: groupA

  tasks: !!seq
    - name: demo task 1
      debug: !!seq
        msg: "this is task 1"

    - name: demo task 2
      debug: !!seq
        msg: "this is task 2"

- name: "Do another demo"
  hosts: groupB

  tasks: !!seq
    - name: demo task 3
      debug: !!seq
        msg: "this is task 3"

    - name: demo task 4
      debug: !!seq
        msg: "this is task 4"

请注意,我不仅在冒号后添加了一个 space 使其成为一个值指示器,我还缩进了 msg: "this is task 3" 以确认其他 msg 键。