# 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" #___________________________________________________________________________________________ # 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 it’s 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.