Ansible 2.7.10中此错误的原因是什么-“ ERROR!剧本条目必须是有效的剧本或include语句|”

时间:2019-04-30 18:56:04

标签: ansible yaml ansible-2.x

我正在观看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,还没有发现任何提示发生该错误的提示!

1 个答案:

答案 0 :(得分:3)

您的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"

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