为什么在加载Ansible YAML时出现语法错误?

为什么在加载Ansible YAML时出现语法错误?,ansible,Ansible,我的剧本 --- - hosts: all var_files: -vars.yml strategy : free tasks: - name: Add the Google signing key apt_key : url=https://packages.cloud.google.com/apt/doc/apt-key.gpg state=present - name: Add the k8s APT repo apt_reposi

我的剧本

---
- hosts: all
  var_files: 
   -vars.yml
  strategy : free

  tasks:
   - name: Add the Google signing key
     apt_key : url=https://packages.cloud.google.com/apt/doc/apt-key.gpg state=present

   - name: Add the k8s APT repo
     apt_repository: repo='deb http://apt.kubernetes.io/ kubernetes-xenial main' state=present

   - name: Install packages
     apt: name="{{ item }}" state=installed update_cache=true force=yes with_items: "{{ PACKAGES }}"
当我跑的时候

ansible-playbook -i hosts playbook.yml
发生错误,尽管我稍微修改了文件。 令人不快的一行似乎是:

   - name: Install packages
     apt: name="{{ item }}" state=installed update_cache=true force=yes with_items: "{{ PACKAGES }}"
                                                                                  ^ here
We could be wrong, but this one looks like it might be an issue with
missing quotes.  Always quote template expression brackets when they
start a value. For instance:

    with_items:
      - {{ foo }}

Should be written as:

    with_items:
      - "{{ foo }}"
我在Ubuntu上,所以apt语法肯定不是问题。 包裹


我是Ansible的新手,如何解决这个简单的问题

with_items
应与任务关联,而不是与模块关联(您将其与
apt
关联,而不是与整个任务关联)。此外,将键值对放在键值对行的末尾是无效的YAML语法。查看更多信息

您可以按如下方式修复错误:

- name: Install packages
  apt: name="{{ item }}" state=installed update_cache=true force=yes
  with_items: "{{ PACKAGES }}"
我还建议在这里使用现代Ansible格式:

- name: Install packages
  apt:
    name: "{{ item }}"
    state: installed
    update_cache: true
    force: yes
  with_items: "{{ PACKAGES }}"
还请注意,在现代Ansible中,如果将
与_项目一起使用,则可能会收到警告。现在建议将包数组直接放置在
name
参数中:

- name: Install packages
  apt:
    name: "{{ PACKAGES }}"
    state: installed
    update_cache: true
    force: yes
- name: Install packages
  apt:
    name: "{{ PACKAGES }}"
    state: installed
    update_cache: true
    force: yes