
There are a few different Ansible facts that can be used to return information about a target systems Operating System.
- ansible_distribution can be used to return the distribution of a target system's Operating System (e.g. CentOS, Ubuntu)
- ansible_distribution_version can be used to return the version of a target system's Operating System (e.g. 9.6 if the target system is Red Hat Linux 9.6 (Plow))
- ansible_distribution_major_version (this article) can be used to return the major version of a target system's Operating System (e.g. 9 if the target system is Red Hat Linux 9.6 (Plow))
- ansible_distribution_release can be used to return a target system's Operating System Release (e.g. Red Hat Linux 9.6 (Plow))
ansible_distribution_major_version can be used to return the major version of a target system's Operating System. Refer to run tasks on the control node if you want to run this on your control node.
For example, let's say you have the following playbook.
---
- hosts: all
tasks:
- ansible.builtin.debug:
msg: "{{ ansible_distribution_major_version }}"
...
If you get the following output, the gather_facts module may be set to false. gather_facts must be set to true, or not defined at all in your playbook, as gather_facts is true by default.
TASK [debug]
fatal: [server1.example.com]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'ansible_distribution' is undefined\n\nThe error appears to be in 'testing.yml': line 8, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n tasks:\n - debug:\n ^ here\n"}
Running this playbook should return something like this. In this example, the target systems Operating System major version is 9 (Red Hat Enterprise Linux (RHEL) version 9 in this example).
TASK [debug]
ok: [server1.example.com] => {
"msg: "9"
}
Did you find this article helpful?
If so, consider buying me a coffee over at