Determine virtual or physical machine from existing default Ansible facts

6,170 views
Skip to first unread message

Simon Wydooghe

unread,
Feb 7, 2017, 1:15:05 PM2/7/17
to Ansible Project
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

Kai Stian Olstad

unread,
Feb 7, 2017, 2:25:19 PM2/7/17
to ansible...@googlegroups.com
You have ansible_virtualization_role, host or guest.

If you would like to find out which hypervisor you can check the
ansible_virtualization_type.


--
Kai Stian Olstad

Simon Wydooghe

unread,
Feb 7, 2017, 7:21:58 PM2/7/17
to ansible...@googlegroups.com
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!



--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/YtIU-mRFeIE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/b617a8d7-5eae-a0a7-c585-71be3af1e82e%40olstad.com.

For more options, visit https://groups.google.com/d/optout.

Ansible User

unread,
Feb 9, 2017, 8:58:31 AM2/9/17
to Ansible Project, wydoogh...@gmail.com
Sorry to hijack this topic, but I am having an issue and tried to post a topic earlier, but it's not showing up.


On Tuesday, February 7, 2017 at 6:21:58 PM UTC-6, Simon Wydooghe wrote:
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!
On Tue, Feb 7, 2017 at 8:24 PM, Kai Stian Olstad <ansible-pr...@olstad.com> wrote:
On 07. feb. 2017 19:15, Simon Wydooghe wrote:
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.

You have ansible_virtualization_role, host or guest.

If you would like to find out which hypervisor you can check the
ansible_virtualization_type.


--
Kai Stian Olstad


--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/YtIU-mRFeIE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.

Kai Stian Olstad

unread,
Feb 9, 2017, 10:16:13 AM2/9/17
to ansible...@googlegroups.com
On 08. feb. 2017 01:55, Ansible User wrote:
> Sorry to hijack this topic, but I am having an issue and tried to post a
> topic earlier, but it's not showing up.

If you read the information on top here
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?

--
Kai Stian Olstad

Jean-Francois Gratton

unread,
Mar 6, 2019, 10:07:01 AM3/6/19
to Ansible Project
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

On Tuesday, 7 February 2017 19:21:58 UTC-5, Simon Wydooghe wrote:
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!
On Tue, Feb 7, 2017 at 8:24 PM, Kai Stian Olstad <ansible-pr...@olstad.com> wrote:
On 07. feb. 2017 19:15, Simon Wydooghe wrote:
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.

You have ansible_virtualization_role, host or guest.

If you would like to find out which hypervisor you can check the
ansible_virtualization_type.


--
Kai Stian Olstad


--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/YtIU-mRFeIE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages