I’m currently writing a playbook for a workstation running Rocky Linux, and I can’t figure out one detail.
I want to add a line to a file (yum.conf) only when the vagrant package is installed in version 2.4.1. I know I have to use the package_facts module to do this. I’ve spent a couple hours banging my head on the keyboard trying out various syntaxes, but nothing works. So here’s in plain words what I have to do:
If the vagrant package is installed and its version is 2.4.1 then add the line exclude=vagrant to the /etc/yum.conf file.
- name: Insert line in yum.conf
ansible.builtin.lineinfile:
line: exclude=vagrant
path: /etc/yum.conf
when: ansible_facts.packages.vagrant.release == '2.4.1'
type or paste code here
@hugonz 's answer is good, just two more thoughts, older versions of Ansible would error if ansible_facts.packages.vagrant.release was not defined and if you wanted to check for vagrant version 2.4.1 or above you could also add in use of the ansible.builtin.version test:
- name: Insert line in yum.conf
ansible.builtin.lineinfile:
line: exclude=vagrant
path: /etc/yum.conf
when:
- ansible_facts.packages.vagrant.release is defined
- ansible_facts.packages.vagrant.release is ansible.builtin.version('2.4.1', '>=')"