如何通过 Ansible 事实判断主机是容器还是虚拟机?

问题描述 投票:0回答:1

有没有办法通过收集事实或者其他方式来判断主机是容器还是虚拟机?

ansible ansible-facts
1个回答
0
投票

只需收集可靠的事实。来自最小的示例剧本

---
- hosts: test
  become: true

  gather_facts: true
  gather_subset:
    - "!all"
    - "min"
  # - "hardware"

  tasks:

  - name: Show Facts
    debug:
      msg: "{{ ansible_facts }}"

输出结果已经包含了所请求的信息

    os_family: RedHat
    product_name: VMware7,1
    system: Linux
    system_vendor: VMware, Inc.
    virtualization_role: guest
    virtualization_type: VMware

    os_family: RedHat
    product_name: Virtual Machine
    product_version: Hyper-V UEFI Release v1.0
    system: Linux
    system_vendor: Microsoft Corporation

    os_family: RedHat
    product_name: HVM domU
    system: Linux
    system_vendor: Xen
    virtualization_role: guest
    virtualization_type: xen

有了这些信息,基于

ansible_facts
的条件就变得微不足道了。就像在

when: ansible_facts['os_family'] == "Debian" and ansible_facts['system_vendor'] == "Microsoft Corporation"

类似问答

© www.soinside.com 2019 - 2024. All rights reserved.