How ansible sets variables ('required_pkgs')?

Viewed 86

I'm working on Kubespray 2.16

At roles/kubernetes/preinstall/tasks/0070-system-packages.yml, there is a task "Install packages requirements"

- name: Install packages requirements
  package:
    name: "{{ required_pkgs | default([]) | union(common_required_pkgs|default([])) }}"
    state: present
  register: pkgs_task_result
  until: pkgs_task_result is succeeded
  retries: "{{ pkg_install_retries }}"
  delay: "{{ retry_stagger | random + 3 }}"
  when: not (ansible_os_family in ["Flatcar Container Linux by Kinvolk", "ClearLinux"] or is_fedora_coreos)
  tags:
    - bootstrap-os

According to the code, the package {{ required_pkgs | default([]) | union(common_required_pkgs|default([])) }} will be installed.

And I found the required_pkgs in roles/kubernetes/preinstall/vars/*.yml

In redhat.yml,

required_pkgs:
  - "{{ ( (ansible_distribution_major_version | int) < 8) | ternary('libselinux-python','python3-libselinux') }}"
  - device-mapper-libs
  - nss
  - conntrack

In centos.yml,

required_pkgs:
  - "{{ ( (ansible_distribution_major_version | int) < 8) | ternary('libselinux-python','python3-libselinux') }}"
  - device-mapper-libs
  - nss
  - conntrack

I think there is no process for Ansible to check my OS in "this task". Then how the Ansible set required_pkgs among a lot of YML files?

p.s. My OS is RedHat.

1 Answers

By default, when Ansible first starts executing a play it will implicitly run the setup module on all the remote hosts involved in the play. This is called the "fact gathering" step, and is controlled by the gather_facts option in the play or the gathering option in ansible.cfg.

The facts gathered in this step include things like operating system flavor, release, information about interfaces and disks, and a variety of other host metadata. The variables are all all in the ansible_* namespace (e.g., ansible_distribution_major_version).

Kubespray is taking advantage of these automatically gathered facts to load the appropriate vars files.

For more information:

Etc.

Related