--- # 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 create user task is not running. I have searched every key phrase I can think of on Google to find an answer without success.
The roles are running which is odd.
Is it possible for a playbook to contain both tasks and roles?
Role is a set of tasks and additional files to configure host to serve for a certain role. Playbook is a mapping between hosts and roles. Example from documentation describes example project.
tl;dr A task is a single declaration (operation); a role is one of many ways for grouping tasks. A task in Ansible is a basic unit of work, a kind of counterpart to a line of code in other languages. A task is executed and has a result (ok, changed, failed).
Playbook Structure Each playbook is an aggregation of one or more plays in it. Playbooks are structured using Plays. There can be more than one play inside a playbook. The function of a play is to map a set of instructions defined against a particular host.
A Playbook can have multiple Plays and a Play can have one or multiple Tasks. In a Task a Module is called, like the Modules in the previous chapter. The goal of a Play is to map a group of hosts to a list of Tasks. The goal of a Task is to implement Modules against those hosts.
You can also do pre_tasks: and post_tasks: if you need to do things before or after. From the Docs https://docs.ansible.com/ansible/latest/user_guide/playbooks_reuse_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'
Gives the output: PLAY [localhost]
GATHERING 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 ansible 1.9.1
Actually this should be possible and I remember I did this a few times during testing. Might be something with your version - or the order does matter, so that the tasks will be executed after the roles.
I would have posted this as a comment, rather than an answer, but I wouldn't be able to give the following example in a comment:
Whatever might be the reason why your task is not executed, you can always separate your playbook into several plays, like so:
--- # 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 }}" }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With