# Best way to define variables based on facts for a role

**URL:** <https://forum.ansible.com/t/best-way-to-define-variables-based-on-facts-for-a-role/30103>\
**Category:** Ansible Project\
**Created:** [April 12, 2019, 12:20pm UTC](https://forum.ansible.com/t/best-way-to-define-variables-based-on-facts-for-a-role/30103 "2019-04-12T12:20:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bruno\_Cochofel](https://avatars.discourse-cdn.com/v4/letter/b/4da419/32.png) [@Bruno\_Cochofel](https://forum.ansible.com/u/Bruno_Cochofel)\
**Post date:** [April 12, 2019, 12:20pm UTC](https://forum.ansible.com/t/best-way-to-define-variables-based-on-facts-for-a-role/30103/1 "2019-04-12T12:20:20Z")

</div>

Hi,

I’m trying to write a role that has different template file if the server is virtual os physical.  
What’s the best way to filter this?

Thanks,  
Bruno

---

<div class="post-metadata">

**Author:** ![Raghavendra\_Rao](https://avatars.discourse-cdn.com/v4/letter/r/c77e96/32.png) [@Raghavendra\_Rao](https://forum.ansible.com/u/Raghavendra_Rao)\
**Post date:** [April 12, 2019, 2:21pm UTC](https://forum.ansible.com/t/best-way-to-define-variables-based-on-facts-for-a-role/30103/2 "2019-04-12T14:21:35Z")

</div>

I use these ansible facts for differentiating between physical and virtual guests:

“ansible\_virtualization\_role”: “guest”,  
“ansible\_virtualization\_type”: “kvm”,

These work perfect along with a ‘when’ conditional. For example:

- template:  
src:   
dest:   
when: ansible\_virtualization\_role is defined or ansible\_virtualization\_role == “kvm”

If you want to explore more variables, run the following adhoc command on both the physical and virtual hosts and notice the change is the value of variables that facts report:

#ansible all -m setup | less

-R.Rao

---

<div class="post-metadata">

**Author:** ![Bruno\_Cochofel](https://avatars.discourse-cdn.com/v4/letter/b/4da419/32.png) [@Bruno\_Cochofel](https://forum.ansible.com/u/Bruno_Cochofel)\
**Post date:** [April 12, 2019, 3:04pm UTC](https://forum.ansible.com/t/best-way-to-define-variables-based-on-facts-for-a-role/30103/3 "2019-04-12T15:04:18Z")

</div>

Hi R.Rao,

Thanks, gonna try that

Bruno
