简单剧本中的Ansible YAML语法错误

简单剧本中的Ansible YAML语法错误,ansible,yaml,Ansible,Yaml,我有我的第一本剧本,但失败了。我认为这是一个语法错误,但由于我不是一个程序员,我不知道为什么YAML会失败?这与间距有关吗 以下是我所拥有的: --- - name: Update all packages to the latest version become: true apt: update_cache: yes upgrade: dist - name: Remove useless packages from the cache

我有我的第一本剧本,但失败了。我认为这是一个语法错误,但由于我不是一个程序员,我不知道为什么YAML会失败?这与间距有关吗

以下是我所拥有的:

---
- name: Update all packages to the latest version
    become: true
    apt:
      update_cache: yes       
      upgrade: dist

- name: Remove useless packages from the cache
    apt:
      autoclean: yes

- name: Remove dependencies that are no longer required
    apt:
      autoremove: yes

首先:这不是剧本,因为它不包含剧本(必须包含
hosts
声明),而是任务

第二:缩进被严重破坏——在YAML中保持声明正确对齐至关重要(也就是说,您看到的错误不是YAML语法错误,而是由于在正确编写的YAML文件中定义了不正确的数据而导致的Ansible错误)

如果要在本地运行,它应该大致如下所示:

---
- hosts: localhost
  connection: local
  tasks:
    - name: Update all packages to the latest version
      become: true
      apt:
        update_cache: yes       
        upgrade: dist
        autoclean: yes
        autoremove: yes

#在.yaml文件中正确使用空格的提示:不要使用空格,而要使用像%(易于计数)这样的字符。输入完代码后,用一个空格替换(编辑>查找>替换)%
---
- hosts: localhost
  connection: local
  tasks:
    - name: Update all packages to the latest version
      become: true
      apt:
        update_cache: yes       
        upgrade: dist
        autoclean: yes
        autoremove: yes