can roles and tasks exist in the same playbook?

ansible include role in playbook
ansible playbook roles
ansible include tasks with vars
ansible-playbook vs role
ansible role tags
ansible nested roles
ansible pre_tasks
ansible tasks
---
# 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?

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 }}" }

Ansible - Roles, Since handlers are tasks too, you can also include handler files from the but a '​play' include cannot be inside other plays only alongside them at the same level. If roles/x/defaults/main.yml exists, variables listed therein will be added to the  The roles are running which is odd. Is it possible for a playbook to contain both tasks and roles? Answer: 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.

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/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'

>

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

What is the difference between ansible playbook and roles?, No, using a single playbook can make it confusing and prone to blunders. Instead, you can create 10 different roles, where each role will perform one task. Then, all you That directory exists exactly for this reason. You create  Any copy, script, template or include tasks (in the role) can reference files in roles/x/{files,templates,tasks}/ (dir depends on task) without having to path them relatively or absolutely. When used in this manner, the order of execution for your playbook is as follows: Any pre_tasks defined in the play. Any handlers triggered so far will be run.

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

If the play still has a ‘tasks’ section, those tasks are executed after roles are applied.

If you wish to run tasks before or after roles are executed, you need to list these under pre_tasks and post_tasks. Thus, there is no way to run "loose" tasks between two roles. You might want to create a dedicated role for these tasks.

Playbook, Roles are a level of abstraction on top of tasks and playbooks that let you the tasks/main.yml file, they need to exist or Ansible will be unable to run playbooks call roles, the command to run ours is exactly the same as if it  No, each has a different role and way of working. Starting with the easy ones: A playbook is simply a list of plays. The highest level of a playbook YAML is a list, and on that list only two things are accepted: a play definition or the keyword import_playbook, which imports a list of plays from another playbook file, as if they were defined in that place of the calling file.

Short follow up to the already mentioned options by quoting the latest Ansible docs docs.ansible.com/latest/playbooks_reuse_roles:

As of Ansible 2.4, you can now use roles inline with any other tasks using import_role or include_role:

---    
- hosts: webservers
  tasks:
    - debug:
      msg: "before we run our role"
    - import_role:
      name: example
    - include_role:
      name: example
    - debug:
      msg: "after we ran our role"`

Code snippet is also from Ansible docs.

Be aware of the difference between static (import*) & dynamic (include*) usage.

Playbook Roles and Include Statements, And I want beginners to feel the same as I do. TL;DR: It can be OK to tag a role in a playbook, but no one should use tag Why do tags exist in Ansible? Both plays and tasks support a “tags:” attribute for this reason. Note, you are still allowed to list tasks, vars_files, and handlers “loose” in playbooks without using roles, but roles are a good organizational feature and are highly recommended. If there are loose things in the playbook, the roles are evaluated first.

Ansible Roles, You can use the Ansible include_tasks module to import tasks from other files. This could be within a playbook or a role's task files. roles/my_role/tasks/main.​yml - name: install dependencies apt: name: "{{ item }}" state: present become: true loop: - nginx This will produce the same results as the original list of tasks. can roles and tasks exist in the same playbook? ansible,ansible-playbook. 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 commend, rather than an answer,

How to Use Ansible Roles to Abstract your Infrastructure , In Ansible, the role is the primary mechanism for breaking a playbook into multiple files. The default structure can be changed but for now let us stick to defaults. present register: Output always: - debug: msg: - "Install Tomcat artifacts task  ---Accepted---Accepted---Accepted---I make it work just moving roles folder inside the plays folder and removing the play/ansible.cfg:. ├── plays │ ├── playbook_01.yml │ ├── playbook_02.yml │ ├── playbook_03.yml │ └── roles │ ├── role_A │ │ ├── files │ │ └── tasks │ │ └── main.yml │ └── role_B

Best practices to build great Ansible playbooks, Ansible roles are simple to get set up and allow for complexity when necessary. overlooked in lieu of straightforward playbooks for the task at hand. init <​ROLE_NAME> to create a new role in your present working directory. Also, we can override the default variables we configured using the same  For example, openssl is a dependency for 4 of the roles: So, in the above example playbook that uses the newer syntax, openssl is installed 5 times. But, in the example playbook that uses the classic syntax, openssl is only installed once.

Comments
  • Would I be right in thinking if your playbook looked like this, then its possible both plays might be executed in parallel?
  • No, the 2nd play will only start once the first completed.
  • I saw that in the docs but for whatever reason, my task won't run.
  • Try the simple playbook I posted. Add a role that just does a shell: echo something and see if that works. Maybe you have something else going on in your playbook that keeps it from working as you expect.