pkaramol
pkaramol

Reputation: 19412

Ansible: mixing roles and tasks prohibited?

I am using the following site.yml playbook and calling it via

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

Call is failing as follows:

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

But as is apparent above, the hosts field has been set!

Any suggestions?

Upvotes: 9

Views: 7529

Answers (1)

Tj Kellie
Tj Kellie

Reputation: 6476

You can mix tasks and roles in a playbook, you can also control when the tasks execute by using "pre_tasks" and "post_tasks".

It looks to me like you have a - on tasks that should not be there, probably considering it to be a new play.

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

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

Example using pre and post tasks to control when tasks execute in relation to a role:

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

Upvotes: 20

Related Questions