# playbook: run role only if any previous roles have changes

**URL:** <https://forum.ansible.com/t/playbook-run-role-only-if-any-previous-roles-have-changes/14066>\
**Category:** Ansible Project\
**Created:** [December 4, 2013, 10:26pm UTC](https://forum.ansible.com/t/playbook-run-role-only-if-any-previous-roles-have-changes/14066 "2013-12-04T22:26:22Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![senorsmile](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/senorsmile/32/9024_2.png) [@senorsmile](https://forum.ansible.com/u/senorsmile)\
**Post date:** [December 4, 2013, 10:26pm UTC](https://forum.ansible.com/t/playbook-run-role-only-if-any-previous-roles-have-changes/14066/1 "2013-12-04T22:26:22Z")

</div>

I’m trying to come up with a seemingly simple logic, yet can’t figure out how to do it in ansible:

I want to run the last role only if any previous roles have changes.

---

<div class="post-metadata">

**Author:** ![senorsmile](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/senorsmile/32/9024_2.png) [@senorsmile](https://forum.ansible.com/u/senorsmile)\
**Post date:** [December 4, 2013, 10:44pm UTC](https://forum.ansible.com/t/playbook-run-role-only-if-any-previous-roles-have-changes/14066/2 "2013-12-04T22:44:40Z")

</div>

I have been thinking about this all wrong. I just need to set up a handler that includes the yml for the role I want, and put a notify on the tasks of the other role.

If anyone has a better (more direct way from site.yml), feel free to post.

---

<div class="post-metadata">

**Author:** ![senorsmile](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/senorsmile/32/9024_2.png) [@senorsmile](https://forum.ansible.com/u/senorsmile)\
**Post date:** [December 5, 2013, 6:35pm UTC](https://forum.ansible.com/t/playbook-run-role-only-if-any-previous-roles-have-changes/14066/3 "2013-12-05T18:35:48Z")

</div>

After trying to implement this workaround, I realize that this does NOT fix my issue. I need to have a single task run at the very end, if any of other multiple roles have change.

Currently, I must duplicate the handler into the multiple roles, which means the handler gets run multiple times if multiple roles have changes. I need it to only run once at the very end.

Still searching for solution to OP…

---

<div class="post-metadata">

**Author:** ![James\_Martin2](https://avatars.discourse-cdn.com/v4/letter/j/b9e5f3/32.png) [@James\_Martin2](https://forum.ansible.com/u/James_Martin2)\
**Post date:** [December 5, 2013, 7:09pm UTC](https://forum.ansible.com/t/playbook-run-role-only-if-any-previous-roles-have-changes/14066/4 "2013-12-05T19:09:08Z")

</div>

Seems like something in your workflow is odd -- can you describe more  
specifically what you are trying to do?

Based on what you said, I think this might work:

Maybe in your playbook could have a pre-task set a fact -- something like:

pre\_tasks:  
&nbsp;&nbsp;- name: "Set role change"  
&nbsp;&nbsp;&nbsp;&nbsp;set\_fact: role\_change="no"

That should run before all the other roles are run.

Your role tasks would send a notify to a common handler that would

- name: "Set role change"  
&nbsp;&nbsp;&nbsp;&nbsp;set\_fact: role\_change="yes"

then in your playbook have a task that gets executed after your roles:

- name: do this task when roles change  
&nbsp;&nbsp;command: foo bar  
&nbsp;&nbsp;when: role\_change == "yes"

- James

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [December 5, 2013, 9:45pm UTC](https://forum.ansible.com/t/playbook-run-role-only-if-any-previous-roles-have-changes/14066/5 "2013-12-05T21:45:29Z")

</div>

You should probably touch a changed file on the system if something changes and use when with the stat module.

– Michael

---

<div class="post-metadata">

**Author:** ![senorsmile](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/senorsmile/32/9024_2.png) [@senorsmile](https://forum.ansible.com/u/senorsmile)\
**Post date:** [December 5, 2013, 10:11pm UTC](https://forum.ansible.com/t/playbook-run-role-only-if-any-previous-roles-have-changes/14066/6 "2013-12-05T22:11:25Z")

</div>

Thanks all! I had been trying to implement the “set\_fact” module, but it sets a different variable per host. I need a global variable that is not tied to a specific host. So, it would end up running the last role multiple times.

Sounds like the file and stat modules are what are going to do the trick. It would be nice to have the option to set a global variable, sort of like set\_fact does, but NOT tied to any specific host, and all host plays could access and modify it. This would replace the need to write to disk during a play.

Where can I get a list of all the possible parameters used with stat?  
e.g. the docs show when: p.stat.isdir is defined and p.stat.isdir == true  
but I’m not sure how to run a task conditionally using stat to check for a file existing.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [December 5, 2013, 10:18pm UTC](https://forum.ansible.com/t/playbook-run-role-only-if-any-previous-roles-have-changes/14066/7 "2013-12-05T22:18:12Z")

</div>

Run the stat module like this and it’s easy to see:

ansible hostname -m stat -a “path=/etc/motd”

And you will see what it returns (ditto if done using a playbook run with -v, or a debug task)

We’re going to have the returns to all the modules listed in the future.

–Michael

---

<div class="post-metadata">

**Author:** ![senorsmile](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/senorsmile/32/9024_2.png) [@senorsmile](https://forum.ansible.com/u/senorsmile)\
**Post date:** [December 5, 2013, 10:19pm UTC](https://forum.ansible.com/t/playbook-run-role-only-if-any-previous-roles-have-changes/14066/8 "2013-12-05T22:19:45Z")

</div>

checking of the file exists with stat is done like this:

---

<div class="post-metadata">

**Author:** ![senorsmile](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/senorsmile/32/9024_2.png) [@senorsmile](https://forum.ansible.com/u/senorsmile)\
**Post date:** [December 5, 2013, 10:23pm UTC](https://forum.ansible.com/t/playbook-run-role-only-if-any-previous-roles-have-changes/14066/9 "2013-12-05T22:23:46Z")

</div>

OK, great!

Quick list for reference:  
{  
“changed”: false,  
“item”: “”, “stat”:  
{  
“atime”: 1386281904.370964,  
“ctime”: 1386281904.370964,  
“dev”: 180969250,  
“exists”: true,  
“gid”: 0,  
“inode”: 106175,  
“isblk”: false,  
“ischr”: false,  
“isdir”: false,  
“isfifo”: false,  
“isgid”: false,  
“islnk”: false,  
“isreg”: true,  
“issock”: false,  
“isuid”: false,  
“md5”: “d41d8cd98f00b204e9800998ecf8427e”,  
“mode”: 420,  
“mtime”: 1386281904.370964,  
“nlink”: 1,  
“pw\_name”: “root”,  
“rgrp”: true,  
“roth”: true,  
“rusr”: true,  
“size”: 0,  
“uid”: 0,  
“wgrp”: false,  
“woth”: false,  
“wusr”: true,  
“xgrp”: false,  
“xoth”: false,  
“xusr”: false}  
}
