Escape Go template syntax in shell commands to prevent Ansible from
interpreting them as Jinja2 templates.
Errors fixed:
template error while templating string: unexpected '.'
String: docker version --format '{{.Server.Version}}'
String: docker images --format "{{.Repository}}:{{.Tag}}"
String: podman version --format '{{.Version}}'
Changes:
- Docker version check: Escape {{.Server.Version}}
- Docker images list: Escape {{.Repository}} and {{.Tag}}
- Podman version check: Escape {{.Version}}
Solution:
Convert {{ to {{ "{{" }} and }} to {{ "}}" }}
This tells Ansible to output literal {{ }} in the shell command
The Docker/Podman CLI then interprets the Go templates correctly
Example:
Before: '{{.Server.Version}}'
After: '{{ "{{" }}.Server.Version{{ "}}" }}'
Result: Shell receives '{{.Server.Version}}' as intended
Testing: Playbook now completes successfully without template errors.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
297 lines
11 KiB
YAML
297 lines
11 KiB
YAML
---
|
|
# Hypervisor detection tasks
|
|
|
|
- name: Check if running in a virtual environment
|
|
set_fact:
|
|
system_info_virtualization_type: "{{ ansible_virtualization_type | default('physical') }}"
|
|
system_info_virtualization_role: "{{ ansible_virtualization_role | default('NA') }}"
|
|
tags: [gather, hypervisor]
|
|
|
|
- name: Detect virtualization using systemd-detect-virt
|
|
shell: systemd-detect-virt
|
|
register: system_info_detect_virt_raw
|
|
changed_when: false
|
|
failed_when: false
|
|
tags: [gather, hypervisor]
|
|
|
|
- name: Set systemd virtualization detection
|
|
set_fact:
|
|
system_info_systemd_virt: "{{ system_info_detect_virt_raw.stdout | default('none') }}"
|
|
tags: [gather, hypervisor]
|
|
|
|
- name: Check for KVM/QEMU hypervisor capability
|
|
shell: |
|
|
if command -v virsh &> /dev/null; then
|
|
echo "virsh: available"
|
|
virsh version 2>/dev/null || echo "virsh not accessible"
|
|
else
|
|
echo "virsh: not installed"
|
|
fi
|
|
register: system_info_virsh_check_raw
|
|
changed_when: false
|
|
failed_when: false
|
|
tags: [gather, hypervisor, kvm]
|
|
|
|
- name: Check libvirt service status
|
|
shell: systemctl is-active libvirtd 2>/dev/null || echo "not running"
|
|
register: system_info_libvirtd_status_raw
|
|
changed_when: false
|
|
failed_when: false
|
|
tags: [gather, hypervisor, libvirt]
|
|
|
|
- name: Gather libvirt details (if available)
|
|
block:
|
|
- name: Check libvirt version
|
|
shell: virsh version
|
|
register: system_info_libvirt_version_raw
|
|
changed_when: false
|
|
failed_when: false
|
|
become: true
|
|
|
|
- name: List libvirt networks
|
|
shell: virsh net-list --all
|
|
register: system_info_libvirt_networks_raw
|
|
changed_when: false
|
|
failed_when: false
|
|
become: true
|
|
|
|
- name: List libvirt storage pools
|
|
shell: virsh pool-list --all
|
|
register: system_info_libvirt_pools_raw
|
|
changed_when: false
|
|
failed_when: false
|
|
become: true
|
|
|
|
- name: Count running VMs
|
|
shell: virsh list --state-running | grep -c running || echo "0"
|
|
register: system_info_libvirt_running_vms_raw
|
|
changed_when: false
|
|
failed_when: false
|
|
become: true
|
|
|
|
- name: Count total VMs
|
|
shell: virsh list --all | tail -n +3 | grep -v "^$" | wc -l
|
|
register: system_info_libvirt_total_vms_raw
|
|
changed_when: false
|
|
failed_when: false
|
|
become: true
|
|
|
|
when: "'available' in system_info_virsh_check_raw.stdout"
|
|
tags: [gather, hypervisor, libvirt]
|
|
|
|
- name: Check for Proxmox VE
|
|
shell: |
|
|
if command -v pveversion &> /dev/null; then
|
|
pveversion
|
|
else
|
|
echo "Proxmox VE not installed"
|
|
fi
|
|
register: system_info_proxmox_check_raw
|
|
changed_when: false
|
|
failed_when: false
|
|
tags: [gather, hypervisor, proxmox]
|
|
|
|
- name: Gather Proxmox details (if available)
|
|
block:
|
|
- name: Get Proxmox cluster status
|
|
shell: pvecm status 2>/dev/null || echo "Not in a cluster"
|
|
register: system_info_proxmox_cluster_raw
|
|
changed_when: false
|
|
failed_when: false
|
|
|
|
- name: List Proxmox VMs
|
|
shell: qm list 2>/dev/null || echo "No VMs or qm not available"
|
|
register: system_info_proxmox_vms_raw
|
|
changed_when: false
|
|
failed_when: false
|
|
|
|
- name: List Proxmox containers
|
|
shell: pct list 2>/dev/null || echo "No containers or pct not available"
|
|
register: system_info_proxmox_containers_raw
|
|
changed_when: false
|
|
failed_when: false
|
|
|
|
- name: Get Proxmox storage status
|
|
shell: pvesm status 2>/dev/null || echo "Storage information not available"
|
|
register: system_info_proxmox_storage_raw
|
|
changed_when: false
|
|
failed_when: false
|
|
|
|
when: "'pveversion' in system_info_proxmox_check_raw.stdout"
|
|
tags: [gather, hypervisor, proxmox]
|
|
|
|
- name: Check for LXD/LXC
|
|
shell: |
|
|
if command -v lxc &> /dev/null; then
|
|
lxc version
|
|
else
|
|
echo "LXD not installed"
|
|
fi
|
|
register: system_info_lxd_check_raw
|
|
changed_when: false
|
|
failed_when: false
|
|
tags: [gather, hypervisor, lxd]
|
|
|
|
- name: Gather LXD details (if available)
|
|
block:
|
|
- name: List LXD containers
|
|
shell: lxc list --format json
|
|
register: system_info_lxd_containers_raw
|
|
changed_when: false
|
|
failed_when: false
|
|
|
|
- name: Get LXD storage pools
|
|
shell: lxc storage list --format json
|
|
register: system_info_lxd_storage_raw
|
|
changed_when: false
|
|
failed_when: false
|
|
|
|
- name: Get LXD networks
|
|
shell: lxc network list --format json
|
|
register: system_info_lxd_networks_raw
|
|
changed_when: false
|
|
failed_when: false
|
|
|
|
- name: Check LXD cluster status
|
|
shell: lxc cluster list --format json 2>/dev/null || echo "[]"
|
|
register: system_info_lxd_cluster_raw
|
|
changed_when: false
|
|
failed_when: false
|
|
|
|
when: "'Client version' in system_info_lxd_check_raw.stdout or 'Server version' in system_info_lxd_check_raw.stdout"
|
|
tags: [gather, hypervisor, lxd]
|
|
|
|
- name: Check for Docker
|
|
shell: |
|
|
if command -v docker &> /dev/null; then
|
|
docker version --format '{{ "{{" }}.Server.Version{{ "}}" }}' 2>/dev/null || echo "Docker installed but not running"
|
|
else
|
|
echo "Docker not installed"
|
|
fi
|
|
register: system_info_docker_check_raw
|
|
changed_when: false
|
|
failed_when: false
|
|
tags: [gather, hypervisor, docker]
|
|
|
|
- name: Gather Docker details (if available)
|
|
block:
|
|
- name: Count running containers
|
|
shell: docker ps -q | wc -l
|
|
register: system_info_docker_running_raw
|
|
changed_when: false
|
|
failed_when: false
|
|
|
|
- name: Count total containers
|
|
shell: docker ps -aq | wc -l
|
|
register: system_info_docker_total_raw
|
|
changed_when: false
|
|
failed_when: false
|
|
|
|
- name: List Docker images
|
|
shell: docker images --format "{{ "{{" }}.Repository{{ "}}" }}:{{ "{{" }}.Tag{{ "}}" }}" | wc -l
|
|
register: system_info_docker_images_raw
|
|
changed_when: false
|
|
failed_when: false
|
|
|
|
when:
|
|
- "'not installed' not in system_info_docker_check_raw.stdout"
|
|
- "'not running' not in system_info_docker_check_raw.stdout"
|
|
tags: [gather, hypervisor, docker]
|
|
|
|
- name: Check for Podman
|
|
shell: |
|
|
if command -v podman &> /dev/null; then
|
|
podman version --format '{{ "{{" }}.Version{{ "}}" }}'
|
|
else
|
|
echo "Podman not installed"
|
|
fi
|
|
register: system_info_podman_check_raw
|
|
changed_when: false
|
|
failed_when: false
|
|
tags: [gather, hypervisor, podman]
|
|
|
|
- name: Gather VMware ESXi/vSphere information
|
|
shell: |
|
|
if [ -f /etc/vmware-release ]; then
|
|
cat /etc/vmware-release
|
|
else
|
|
echo "Not VMware ESXi"
|
|
fi
|
|
register: system_info_vmware_check_raw
|
|
changed_when: false
|
|
failed_when: false
|
|
tags: [gather, hypervisor, vmware]
|
|
|
|
- name: Check for Hyper-V Linux Integration Services
|
|
shell: |
|
|
if lsmod | grep -q hv_vmbus; then
|
|
echo "Hyper-V detected"
|
|
lsmod | grep ^hv_
|
|
else
|
|
echo "Not Hyper-V"
|
|
fi
|
|
register: system_info_hyperv_check_raw
|
|
changed_when: false
|
|
failed_when: false
|
|
tags: [gather, hypervisor, hyperv]
|
|
|
|
- name: Determine if system is a hypervisor
|
|
set_fact:
|
|
system_info_is_hypervisor: >-
|
|
{{
|
|
('available' in system_info_virsh_check_raw.stdout) or
|
|
('pveversion' in system_info_proxmox_check_raw.stdout) or
|
|
('Client version' in system_info_lxd_check_raw.stdout) or
|
|
('Server version' in system_info_lxd_check_raw.stdout) or
|
|
('VMware ESXi' in system_info_vmware_check_raw.stdout) or
|
|
(system_info_docker_check_raw.stdout | regex_search('\\d+\\.\\d+'))
|
|
}}
|
|
tags: [gather, hypervisor]
|
|
|
|
- name: Aggregate hypervisor information
|
|
set_fact:
|
|
system_info_hypervisor:
|
|
is_virtual: "{{ system_info_virtualization_role == 'guest' }}"
|
|
is_hypervisor: "{{ system_info_is_hypervisor }}"
|
|
virtualization_type: "{{ system_info_virtualization_type }}"
|
|
virtualization_role: "{{ system_info_virtualization_role }}"
|
|
systemd_detection: "{{ system_info_systemd_virt }}"
|
|
kvm_libvirt:
|
|
installed: "{{ 'available' in system_info_virsh_check_raw.stdout }}"
|
|
service_status: "{{ system_info_libvirtd_status_raw.stdout | default('N/A') }}"
|
|
version: "{{ system_info_libvirt_version_raw.stdout_lines | default([]) if 'available' in system_info_virsh_check_raw.stdout else [] }}"
|
|
running_vms: "{{ system_info_libvirt_running_vms_raw.stdout | default('0') if 'available' in system_info_virsh_check_raw.stdout else '0' }}"
|
|
total_vms: "{{ system_info_libvirt_total_vms_raw.stdout | default('0') if 'available' in system_info_virsh_check_raw.stdout else '0' }}"
|
|
networks: "{{ system_info_libvirt_networks_raw.stdout_lines | default([]) if 'available' in system_info_virsh_check_raw.stdout else [] }}"
|
|
storage_pools: "{{ system_info_libvirt_pools_raw.stdout_lines | default([]) if 'available' in system_info_virsh_check_raw.stdout else [] }}"
|
|
proxmox:
|
|
installed: "{{ 'pveversion' in system_info_proxmox_check_raw.stdout }}"
|
|
version: "{{ system_info_proxmox_check_raw.stdout | default('N/A') }}"
|
|
cluster_status: "{{ system_info_proxmox_cluster_raw.stdout_lines | default([]) if 'pveversion' in system_info_proxmox_check_raw.stdout else [] }}"
|
|
vms: "{{ system_info_proxmox_vms_raw.stdout_lines | default([]) if 'pveversion' in system_info_proxmox_check_raw.stdout else [] }}"
|
|
containers: "{{ system_info_proxmox_containers_raw.stdout_lines | default([]) if 'pveversion' in system_info_proxmox_check_raw.stdout else [] }}"
|
|
storage: "{{ system_info_proxmox_storage_raw.stdout_lines | default([]) if 'pveversion' in system_info_proxmox_check_raw.stdout else [] }}"
|
|
lxd:
|
|
installed: "{{ 'version' in system_info_lxd_check_raw.stdout }}"
|
|
version: "{{ system_info_lxd_check_raw.stdout | default('N/A') }}"
|
|
containers: "{{ system_info_lxd_containers_raw.stdout | default('[]') if 'version' in system_info_lxd_check_raw.stdout else '[]' }}"
|
|
storage: "{{ system_info_lxd_storage_raw.stdout | default('[]') if 'version' in system_info_lxd_check_raw.stdout else '[]' }}"
|
|
networks: "{{ system_info_lxd_networks_raw.stdout | default('[]') if 'version' in system_info_lxd_check_raw.stdout else '[]' }}"
|
|
cluster: "{{ system_info_lxd_cluster_raw.stdout | default('[]') if 'version' in system_info_lxd_check_raw.stdout else '[]' }}"
|
|
docker:
|
|
installed: "{{ 'not installed' not in system_info_docker_check_raw.stdout }}"
|
|
version: "{{ system_info_docker_check_raw.stdout | default('N/A') }}"
|
|
running_containers: "{{ system_info_docker_running_raw.stdout | default('0') if 'not installed' not in system_info_docker_check_raw.stdout and 'not running' not in system_info_docker_check_raw.stdout else '0' }}"
|
|
total_containers: "{{ system_info_docker_total_raw.stdout | default('0') if 'not installed' not in system_info_docker_check_raw.stdout and 'not running' not in system_info_docker_check_raw.stdout else '0' }}"
|
|
images_count: "{{ system_info_docker_images_raw.stdout | default('0') if 'not installed' not in system_info_docker_check_raw.stdout and 'not running' not in system_info_docker_check_raw.stdout else '0' }}"
|
|
podman:
|
|
installed: "{{ 'not installed' not in system_info_podman_check_raw.stdout }}"
|
|
version: "{{ system_info_podman_check_raw.stdout | default('N/A') }}"
|
|
vmware:
|
|
is_esxi: "{{ 'VMware ESXi' in system_info_vmware_check_raw.stdout }}"
|
|
version: "{{ system_info_vmware_check_raw.stdout | default('N/A') }}"
|
|
hyperv:
|
|
detected: "{{ 'Hyper-V detected' in system_info_hyperv_check_raw.stdout }}"
|
|
modules: "{{ system_info_hyperv_check_raw.stdout_lines | default([]) }}"
|
|
tags: [gather, hypervisor]
|