# Executing multiple roles in succession

**URL:** <https://forum.ansible.com/t/executing-multiple-roles-in-succession/37327>\
**Category:** Ansible Project\
**Tags:** windows, vmware\
**Created:** [November 9, 2022, 10:30pm UTC](https://forum.ansible.com/t/executing-multiple-roles-in-succession/37327 "2022-11-09T22:30:45Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chris\_Bachmeyer](https://avatars.discourse-cdn.com/v4/letter/c/3e96dc/32.png) [@Chris\_Bachmeyer](https://forum.ansible.com/u/Chris_Bachmeyer)\
**Post date:** [November 9, 2022, 10:30pm UTC](https://forum.ansible.com/t/executing-multiple-roles-in-succession/37327/1 "2022-11-09T22:30:45Z")

</div>

Hey all,

I have two roles that are included in my site.yml and the goal is to execute the second role as soon as the first role completes. I have added a task to generate a dynamic inventory in the first role that the second role can use. I have a job Template in Ansible Tower and the first role executes cleanly but job Template completes without the second role executing.

What am I missing?

- name: VMware-windows create role  
hosts: localhost  
gather\_facts: no  
roles:
- vmware-windows

vars\_files:

- ./roles/vmware-windows/vars/{{ vc\_location }}.yml

- ./roles/vmware-windows/vars/{{ network }}.yml

- name: Windows-post role  
hosts: windows  
gather\_facts: no  
roles:

- windows-post

Thanks!

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [November 10, 2022, 12:22pm UTC](https://forum.ansible.com/t/executing-multiple-roles-in-succession/37327/2 "2022-11-10T12:22:43Z")

</div>

You need to make sure you use proper indentation. Your roles list needs to be indented two spaces like your vars\_files.

Is it possible also that your sourcing of those the vars\_files from within your role is overwriting the value of “windows” such that there is no inventory for the second play? Why do you source those vars files? The vmware-windows role will do that automatically.

> - name: VMware-windows create role  
> hosts: localhost  
> gather\_facts: no  
> roles:
> - vmware-windows

> 

> ### this vars\_files should not be needed
> 
> vars\_files:
> 
> - ./roles/vmware-windows/vars/{{ vc\_location }}.yml
> 
> - ./roles/vmware-windows/vars/{{ network }}.yml
> 
> - name: Windows-post role  
> hosts: windows  
> gather\_facts: no  
> roles:
> 
> - windows-post

I presume that your vmware-windows role in the first play creates the host group called “windows” that you use as the inventory in your second play?

Walter

---

<div class="post-metadata">

**Author:** ![Chris\_Bachmeyer](https://avatars.discourse-cdn.com/v4/letter/c/3e96dc/32.png) [@Chris\_Bachmeyer](https://forum.ansible.com/u/Chris_Bachmeyer)\
**Post date:** [November 10, 2022, 2:54pm UTC](https://forum.ansible.com/t/executing-multiple-roles-in-succession/37327/3 "2022-11-10T14:54:17Z")

</div>

Hi, Walter,

That is correct. I am, the vmware-windows role is the first play and creates the host\_group “windows” to be used as inventory in the second play.  
Right now, the second role is not indented but let me give that a go this morning. I’m presuming to indent the entire block starting with the hyphen  
( - name: … )?

Regarding the vars\_files, use import statements in the vars/main.yml of the first role? I have multiple vars files under vars/ that are specific to the vcenters  
and vlans. Using vars\_files and variables in the Ansible Tower Survey gives the Admins a way to customize the builds to the environment (which works well!).

Thank you!

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [November 10, 2022, 3:26pm UTC](https://forum.ansible.com/t/executing-multiple-roles-in-succession/37327/4 "2022-11-10T15:26:26Z")

</div>

Does the vmware-windows role need the vars defined from the vars\_files? If so then the role itself should include them inside it’s task file(s). Move the vars\_files inside tasks/main.yml. Anything the role does after that will get the value of those vars definitions.

The vmware-windows role will see the values of network and vc\_location that come into the playbook executing the role. I presume they come in as extra\_vars from command line or ansible tower.

Walter

---

<div class="post-metadata">

**Author:** ![Chris\_Bachmeyer](https://avatars.discourse-cdn.com/v4/letter/c/3e96dc/32.png) [@Chris\_Bachmeyer](https://forum.ansible.com/u/Chris_Bachmeyer)\
**Post date:** [November 10, 2022, 5:28pm UTC](https://forum.ansible.com/t/executing-multiple-roles-in-succession/37327/5 "2022-11-10T17:28:57Z")

</div>

Initially, I set up the Ansible Playbook outside of using a role, a plays/, vars/, group\_vars/, and collections/ directories. Yes, the network and vc\_locations were being passed  
in as extra\_vars in Anbible Tower via a Survey.

As things have grown, I recently converted that work over to an Ansible role. The conversion worked well and things run accordingly through Ansible Tower. The site.yml is what  
is used including the vars\_files sans the second role. Now I am working on including the second role for post-processing needs of the deployed vms.

Yes, the vmware-windows role needs variables defined for the vcenter, vlans, etc. Currently, I have multiple ymls built out holding the vars such as:  
vlan1.yml, vlan2.yml, etc.  
vcenter1.yml, vcenter2.yml, etc.  
Doing it like this, I can have a prompt in the Survey for the Admins to customize the deployment. I think I am following you, I can move those yml files over to tasks/ in the first role.

I did indent the second role, having to remove the hyphen from “name.” In doing so, I received two warning:

**"[WARNING]: While constructing a mapping from /tmp/bwrap\_ … /project/vmware-windows- role/provision.yml, line 3, column 3, found a duplicate dict key (name). Using last defined value only**

# Windows would be the inventory for the second role

**[WARNING]: Could not match supplied host pattern, ignoring: windows**

Thanks!

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [November 10, 2022, 5:54pm UTC](https://forum.ansible.com/t/executing-multiple-roles-in-succession/37327/6 "2022-11-10T17:54:55Z")

</div>

I have perused your reply but can’t dig into it right at the moment. I will get back to it. Hopefully others also will chime in.

Walter

---

<div class="post-metadata">

**Author:** ![Chris\_Bachmeyer](https://avatars.discourse-cdn.com/v4/letter/c/3e96dc/32.png) [@Chris\_Bachmeyer](https://forum.ansible.com/u/Chris_Bachmeyer)\
**Post date:** [November 11, 2022, 3:14am UTC](https://forum.ansible.com/t/executing-multiple-roles-in-succession/37327/7 "2022-11-11T03:14:26Z")

</div>

Hi, Walter,

I have everything running as it should. It was my mistake. I didn’t intend the full block in the initial email I sent this morning, only the block for the second role.  
The dynamic inventory is generated in the first role and passed to the second, and the end-to-end deployment of instances on VMware now works smoothly.

I haven’t had time to look at restructuring the vars yet but thanks for the input.

Really appreciate it!

Thanks again!
