Ansible: Playbook to install packages using yum and dnf

I'm having problem executing an Ansible playbook to install a package using yum on RHEL7 and dnf on RHEL8. I'm using a condition as shown on my playbook below, but keep getting errors. Error Playbook How do I use my playbook to skip the RHEL7 and install the package on RHEL8 using dnf?

I'm having problem executing an Ansible playbook to install a package using yum on RHEL7 and dnf on RHEL8.

I'm using a condition as shown on my playbook below, but keep getting errors.

Error

{"msg": "The conditional check 'ansible_os_family == \"RedHat\" and ansible_lsb.major_release|int == \"7\"' failed. The error was: error while evaluating conditional (ansible_os_family == \"RedHat\" and ansible_lsb.major_release|int == \"7\"): 'dict object' has no attribute 'major_release'\n\nThe error appears to be in '/ansible/master/intall.pkg.yml': line 9, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n # (Task-1) Checks if ansible_os_family == \"RHEL7\" and then Installs telnet on Remote Node\n - name: Install telnet on RHEL7 Server\n ^ here\n"} 

Playbook

 --- - hosts: all gather_facts: true become: yes #become_user: ansible become_method: sudo tasks: # (Task-1) Checks if ansible_os_family == "RHEL7" and then Installs telnet on Remote Node - name: Install telnet on RHEL7 Server yum: name=telnet state=present when: ansible_os_family == "RedHat" and ansible_lsb.major_release|int == "7" # (Task-2) Checks if ansible_os_family == "RHEL8" and then Installs telnet on Remote Node - name: Install telnet on RHEL8 Server package: name=telnet state=present when: ansible_os_family == "RedHat" and ansible_lsb.major_release|int == "8" 

How do I use my playbook to skip the RHEL7 and install the package on RHEL8 using dnf?

Thank you.

4

1 Answer

As already mentioned within the comments by @Zeitounator you may have a look into Conditionals based on ansible_facts since the fact respective variable is a string.

The following minimal example is working in an production environment.

- name: "Install telnet on RHEL-{{ ansible_distribution_major_version }} Remote Node" yum: name: telnet state: present when: ansible_distribution == 'RedHat' and ansible_distribution_major_version == '7' - name: "Install telnet on RHEL-{{ ansible_distribution_major_version }} Remote Node" yum: name: telnet state: present when: ansible_distribution == 'RedHat' and ansible_distribution_major_version == '8' 

This is possible because of Software management tools in RHEL 8

Although YUM v4 used in RHEL 8 is based on DNF, it is compatible with YUM v3 used in RHEL 7. For software installation, the yum command and most of its options work the same way in RHEL 8 as they did in RHEL 7.

This means that in your specific case by using the yum or package module

This modules manages packages on a target without specifying a package manager module (like ansible.builtin.yum, ansible.builtin.apt, …). It is convenient to use in an heterogeneous environment of machines without having to create a specific task for each package manager. package calls behind the module for the package manager used by the operating system discovered by the module ansible.builtin.setup.

there would be no need for a conditional check unless there would be differences in the availability of packages and or naming.

A single task like

- name: "Install telnet on RHEL-{{ ansible_distribution_major_version }} Remote Node" package: # or even yum name: telnet state: present 

should just work.

Depending on your infrastructure and to be prepared for future releases you could switch to the dnf module for RHEL-8 and RHEL-9 instances.

- name: "Install telnet on RHEL-{{ ansible_distribution_major_version }} Remote Node" dnf: name: telnet state: present when: ansible_distribution == 'RedHat' and ansible_distribution_major_version == '8' 

ncG1vNJzZmirpJawrLvVnqmfpJ%2Bse6S7zGiorp2jqbawutJobm1vZWWAc4WOmqWsoZKhsm68y5qwm6efoHq1u4yipayskaG5brzAnKKan5Woera%2FyKeeZrGlonqiusNmm6ee

 Share!