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

时间:2018-04-17 14:59:49

标签: ansible ansible-2.x

我正在使用以下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!该领域的东道主'是必需的,但没有设置

但如上所述,hosts字段已设置!

有什么建议吗?

1 个答案:

答案 0 :(得分:4)

您可以在剧本中混合任务和角色,也可以使用“pre_tasks”和“post_tasks”控制任务执行的时间。

在我看来,你有一个不应该在那里的任务,可能会认为它是一个新的游戏。

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

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

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

---
- 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