# Determine virtual or physical machine from existing default Ansible facts

**URL:** <https://forum.ansible.com/t/determine-virtual-or-physical-machine-from-existing-default-ansible-facts/23672>\
**Category:** Ansible Project\
**Created:** [February 7, 2017, 6:15pm UTC](https://forum.ansible.com/t/determine-virtual-or-physical-machine-from-existing-default-ansible-facts/23672 "2017-02-07T18:15:05Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Simon\_Wydooghe](https://avatars.discourse-cdn.com/v4/letter/s/90db22/32.png) [@Simon\_Wydooghe](https://forum.ansible.com/u/Simon_Wydooghe)\
**Post date:** [February 7, 2017, 6:15pm UTC](https://forum.ansible.com/t/determine-virtual-or-physical-machine-from-existing-default-ansible-facts/23672/1 "2017-02-07T18:15:05Z")

</div>

Does anyone have a suggestion on how to detect whether a machine is a bare metal one or a virtual one using the existing Ansible facts? In Puppet there was a ‘physical’ boolean which could be used, but going through the Ansible facts of an example host here, I can’t quite figure out something I can use. Am I missing something? I could of course write my own fact, but I’d like to check here to see if that can be avoided.

Regards,  
Simon

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [February 7, 2017, 7:24pm UTC](https://forum.ansible.com/t/determine-virtual-or-physical-machine-from-existing-default-ansible-facts/23672/2 "2017-02-07T19:24:54Z")

</div>

You have ansible\_virtualization\_role, host or guest.

If you would like to find out which hypervisor you can check the  
ansible\_virtualization\_type.

---

<div class="post-metadata">

**Author:** ![Simon\_Wydooghe1](https://avatars.discourse-cdn.com/v4/letter/s/df788c/32.png) [@Simon\_Wydooghe1](https://forum.ansible.com/u/Simon_Wydooghe1)\
**Post date:** [February 8, 2017, 12:21am UTC](https://forum.ansible.com/t/determine-virtual-or-physical-machine-from-existing-default-ansible-facts/23672/3 "2017-02-08T00:21:05Z")

</div>

Thanks for the reply, Kai. I did notice those variables, however they just list ‘NA’ for a simple desktop I have here. Anyway, thanks to your tip I had a quick browse through the facts source code, and if the machine was any type of guest, it seems it would definitely get picked up. So I think it’s fairly safe to say that ‘NA’ can be presumed to be a bare metal machine which solves my problem as far as I am concerned!

---

<div class="post-metadata">

**Author:** ![Strung6013](https://avatars.discourse-cdn.com/v4/letter/s/85f322/32.png) [@Strung6013](https://forum.ansible.com/u/Strung6013)\
**Post date:** [February 8, 2017, 12:55am UTC](https://forum.ansible.com/t/determine-virtual-or-physical-machine-from-existing-default-ansible-facts/23672/4 "2017-02-08T00:55:29Z")

</div>

Sorry to hijack this topic, but I am having an issue and tried to post a topic earlier, but it’s not showing up.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [February 9, 2017, 3:15pm UTC](https://forum.ansible.com/t/determine-virtual-or-physical-machine-from-existing-default-ansible-facts/23672/5 "2017-02-09T15:15:58Z")

</div>

If you read the information on top here  
[https://groups.google.com/forum/#!forum/ansible-project](https://groups.google.com/forum/#!forum/ansible-project)

you will see the following information  
"(note: to avoid spam, initial posts to this group are moderated, but are approved pretty quickly)"

"pretty quickly" in this content can sometimes mean a few days unfortunately.

To list admin, do you need some help approving legitimate users?

---

<div class="post-metadata">

**Author:** ![Jean-Francois\_Gratto](https://avatars.discourse-cdn.com/v4/letter/j/90ced4/32.png) [@Jean-Francois\_Gratto](https://forum.ansible.com/u/Jean-Francois_Gratto)\
**Post date:** [March 5, 2019, 11:57pm UTC](https://forum.ansible.com/t/determine-virtual-or-physical-machine-from-existing-default-ansible-facts/23672/6 "2019-03-05T23:57:54Z")

</div>

I come into this one a bit late as I had this problem this week only.  
I sometimes do not need roles or playbooks to run on virtual machines, so one way I found to work around the fact that ansible is missing puppet’s “is\_virtual” fact is that put my physical machines in a specific group.

I would have an inventory looking like this:

[baremetal]  
oslo  
bergen

[builders]  
rpmbuilder  
debbuilder

[ci]  
perforce:222

My playbook would include a role that’d play on each and every node in the inventory, but if I wanted to restrict it to non-virtual nodes, I’d add this line in my role tasks:

- name: “Jetbrain’s products”  
tags:
- jetbrains  
include: jetbrains.yaml  
when: ansible\_hostname == ‘perforce’ or ansible\_hostname in groups [‘baremetal’]

That does the trick for me, no need to register variables, or such.

Another way is to have multiple inventory files. This makes hosts management a bit more complicated, but also more flexible.

Regards,

– Jeff
