Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can roles and tasks exist in the same playbook?

Tags:

--- # 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?

like image 339
guyja Avatar asked Jun 22 '15 19:06

guyja


People also ask

What is the difference between a playbook and a role?

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.

What is the difference between roles and tasks in Ansible?

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).

What is the structure of a playbook?

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.

Can a playbook have multiple plays?

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.


2 Answers

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

like image 131
Russ Huguley Avatar answered Sep 21 '22 15:09

Russ Huguley


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 }}" } 
like image 39
udondan Avatar answered Sep 20 '22 15:09

udondan