Can roles and tasks exist in the same tutorial?

---
# file: main.yml

- hosts: fotk
  remote_user: fakesudo
  tasks:
  - name: create a developer user
    user: name={{ user }}
          password={{ password }}
          shell=/bin/bash
          generate_ssh_key=yes
          state=present
  roles:
  - { role: create_developer_environment, sudo_user: "{{ user }}" }
  - { role: vim, sudo_user: "{{ user }}" }

      

For some reason, the user creation task has not been started. I have searched every key phrase I can google to find an answer with no success.

Roles that are odd are executed.

Is it possible for a tutorial to contain both tasks and roles?

+3


source to share


4 answers


It should actually be possible, and I remember doing this several times during testing. It could be something with your version - or the order matters so that tasks will run after roles.

I would post this as a comment, not an answer, but I couldn't provide the following example in a comment:



Whatever the reason your task is not getting done, you can always split your playlist into multiple pieces, for example:

---
# file: main.yml

- hosts: fotk
  remote_user: fakesudo
  tasks:
  - name: create a developer user
    user: name={{ user }}
          password={{ password }}
          shell=/bin/bash
          generate_ssh_key=yes
          state=present

- hosts: fotk
  remote_user: fakesudo
  roles:
  - { role: create_developer_environment, sudo_user: "{{ user }}" }
  - { role: vim, sudo_user: "{{ user }}" }

      

+2


source


You can also do pre_tasks: and post_tasks: if you need to do something before or after. From the docs https://docs.ansible.com/playbooks_roles.html

- hosts: localhost

  pre_tasks:
    - shell: echo 'hello in pre'

  roles:
    - { role: some_role }

  tasks:
    - shell: echo 'in tasks'

  post_tasks:
    - shell: echo 'goodbye in post'

      



<P β†’

Outputs: PLAY [localhost]


COLLECTIVE FACTS ********************************************** ** ************* ok: [localhost]

TASK: [shell echo 'hello in pre'] ************************************** ****** changed: [localhost]

TASK: [some_role | shell echo 'hello from the role'] ************************* changed: [localhost]

TASK: [shell echo 'in tasks] **************************************** ******** changed: [localhost]

TASK: [shell echo 'goodbye in post'] ************************************** *** changed: [localhost]

PLAY RECAP *********************************************** ** ******************* localhost: ok = 5 changed = 4 unreachable = 0
failed = 0

This is with 1.9.1 available

+2


source


I recently made a project where I had multiple tasks and roles in the same tutorial, so I can confirm that it works as well. However, I had them under separate hosts, so breaking them down might fix it.

+2


source


https://docs.ansible.com/playbooks_roles.html#roles

If the game still has a "tasks" section, these tasks are performed after the roles have been completed.

If you want to run tasks before or after the roles are performed, you need to list them under pre_tasks

and post_tasks

. Thus, it is not possible to run "free" tasks between the two roles. You might want to create a dedicated role for these tasks.

+1


source







All Articles