6

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?

1 Answer 1

15

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.