Ansible:禁止混合角色和任务?

Ansible: mixing roles and tasks prohibited?

我正在使用以下 site.yml 剧本并通过

调用它

ansible-playbook site.yml

- hosts: some_hosts
  vars:
    pip_install_packages:
      - name: docker

- tasks:

  - name: Conditionally include bar vars
    include_vars:
      file: bar_vars.yml
    when: some_condition == "bar"


  - name: Conditionally include foo vars
    include_vars:
      file: foo_vars.yml
    when: some_condition == "foo"


  roles:
    - role1
    - role2


  environment:
    SOME_ENV_VAR: "{{ vault_some_env_var }}"

调用失败如下:

ERROR! the field 'hosts' is required but was not set

但如上所示,hosts 字段 已设置

有什么建议吗?

您可以在剧本中混合任务和角色,还可以使用 "pre_tasks" 和 "post_tasks" 控制任务何时执行。

在我看来你有一个 - 在不应该出现的任务上,可能认为它是一个新游戏。

- hosts: some_hosts
  vars:
    pip_install_packages:
      - name: docker

- tasks: <-- This should not have a dash

使用预任务和 post 任务来控制任务何时执行与角色相关的示例:

---
- hosts: all
  name: Roles with pre and post tasks
  vars:
    somevar: foobar

  roles:
    - { role: common, tags: ["common"] }

  pre_tasks:
    - debug:
        msg: I execute before roles

  post_tasks:
    - debug:
        msg: I execute after roles