Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 820
pkaramol Avatar asked Dec 11 '22 07:12

pkaramol


1 Answers

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
like image 134
Tj Kellie Avatar answered Jan 05 '23 15:01

Tj Kellie