Skip to content

linuxfabrik.lfops.platform_select (filter)

Pick the value matching the target host from a platform-keyed dictionary

Synopsis

  • Picks a value from a dictionary whose keys are platform identifiers, returning the entry that matches the target host most specifically.
  • The selection precedence mirrors roles/shared/tasks/platform-variables.yml. From least to most specific the order is os_family, os_family + distribution_major_version, os_family + distribution_version, distribution, distribution + distribution_major_version, distribution + distribution_version. The most specific present key wins, even when less specific keys are also present.
  • Intended for the __dependent_var pattern, where a role publishes a value that an earlier role in the same play consumes. vars/<os>.yml cannot serve this case because those files are loaded only when the publishing role's tasks run, which is too late. Defining the value as a platform-keyed dict in the publishing role's vars/main.yml and selecting through this filter resolves the timing problem without losing the vars/ precedence.

Available since LFOps 6.0.2.

Mandatory Parameters

_input

  • Platform-keyed dictionary. Keys are platform identifiers (e.g. RedHat, RedHat8, Rocky8.10, Debian, Suse).
  • Type: Dictionary.

ansible_facts

  • The host's ansible_facts dictionary. The filter reads os_family, distribution, distribution_major_version, and distribution_version from it; missing fact keys are tolerated and skip the affected precedence steps.
  • Type: Dictionary.

Optional Parameters

default

  • Value to return when no key in _input matches the target host. If omitted, an unmatched call raises AnsibleFilterError.
  • Type: Raw.

Examples

# in a role's vars/main.yml (auto-loaded at play parse, so visible to roles
# that run earlier in the same play via the `__dependent_var` pattern):
mariadb_server__python__modules__dependent_var:
  Debian:
    - name: 'python3-pymysql'
  RedHat:
    - name: 'python3-PyMySQL'

# in any playbook that feeds this value into an earlier role:
- name: 'Install python modules and the database'
  hosts: 'all'
  roles:
    - role: 'linuxfabrik.lfops.python'
      python__modules__dependent_var: '{{
          mariadb_server__python__modules__dependent_var
          | linuxfabrik.lfops.platform_select(ansible_facts)
        }}'

    - role: 'linuxfabrik.lfops.mariadb_server'


# combining selections from multiple roles, with a safe default for hosts
# whose platform is not in the dict:
- role: 'linuxfabrik.lfops.python'
  python__modules__dependent_var: '{{
      (mariadb_server__python__modules__dependent_var
        | linuxfabrik.lfops.platform_select(ansible_facts, default=[]))
      + (apache_httpd__python__modules__dependent_var
        | linuxfabrik.lfops.platform_select(ansible_facts, default=[]))
    }}'

Return Values

_value

  • The value associated with the most specific matching key in _input, or the supplied default if no key matches.
  • Type: Raw.