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?
source to share
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 }}" }
source to share
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
source to share
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.
source to share