- name: List installed pipx packages
  ansible.builtin.shell: set -o pipefail && pipx list --short | awk '{print $1}'
  register: installed_pipx_packages
  changed_when: false

- name: Define desired pipx packages
  ansible.builtin.set_fact:
    desired_pipx_packages:
      - ansible
      - ansible-lint
      - shyaml

- name: Install/Upgrade pipx packages
  community.general.pipx:
    name: "{{ item }}"
    state: present
  loop: "{{ desired_pipx_packages }}"

- name: Remove undesired pipx packages
  community.general.pipx:
    name: "{{ item }}"
    state: absent
  loop: "{{ installed_pipx_packages.stdout_lines | difference(desired_pipx_packages) }}"
