Files
2026-07-20 09:23:17 -04:00

54 lines
2.2 KiB
YAML
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# To create an Ansible playbook that checks if qemu-guest-agent is installed, and if not, installs it, you can use the ansible.builtin.package module, which can manage packages across various types of package managers.
# Below is a simple playbook that accomplishes this:
#_________________________________________________________________________________________
---
- name: Check and Install qemu-guest-agent
hosts: all
become: yes # Use this if you need elevated privileges to install packages
tasks:
- name: Check if qemu-guest-agent is installed
ansible.builtin.package_facts:
- name: update apt cache
command: apt update
- name: upgrade all packages
command: apt upgrade -y
- name: Install qemu-guest-agent if not installed
ansible.builtin.package:
name: qemu-guest-agent
state: present
when: "'qemu-guest-agent' not in ansible_facts.packages"
- name: Start guest qemu-guest-agent
ansible.builtin.service:
name: qemu-guest-agent
state: started
enabled: true
#___________________________________________________________________________________________
# Explanation:
#
# hosts: all - This playbook will run on all hosts in your inventory.
# become: yes - This allows the tasks to run with elevated privileges (root), which is often required for installing packages.
# package_facts - This module gathers facts about installed packages on the target machine and stores them in ansible_facts.
# package - This module installs the specified package. The state: present ensures the package is installed.
# when condition - The when clause checks if qemu-guest-agent is present in the ansible_facts.packages. If its not installed, the package will be installed.
# Usage
# Save the above YAML content to a file, for instance, install_qemu_guest_agent.yml.
# Run the playbook using the following command:
# COMMAND TO RUN: ansible-playbook -i your_inventory_file install_qemu_guest_agent.yml
# Replace your_inventory_file with the path to your Ansible inventory file that defines your target hosts.
# This playbook should effectively install the qemu-guest-agent on any host where it is not already installed.