Ansible - set a fact from dynamic variables

空扰寡人 提交于 2021-01-20 13:27:06

问题


I call specific variables file from a fact

- name: Load vars - {{ ansible_distribution }} {{ ansible_distribution_major_version }} package list
  include_vars:
    file: "{{ ansible_distribution | lower }}-pkglist.yml"
  # if vars file is not found we continue
  ignore_errors: yes

Variables in that file could have different name, depending of the distribution

#package for all debians server
prefix_debian:  
- pkgname
- pkgname
...

#package for all debian 10 server
prefix_10 :
- specific major version pkgname
- specific major version pkgname
- specific major version pkgname
...

I also have a 'common' package list which is used for all linux distrib

I need to concat these 3 different list the the package list that have to be install on the host

common_pkg_list + {{ ansible_ distribution }}-pkglist + {{ ansible_distribution_major_version }}-pkglist

To achieve that I tried to setfact for each list from the dynamics variable name:

-name:
  set_fact:
    common_pkg: "{{ prefix_commonpkglist }}"
    ditro_pkg: "prefix_{{ ansible_distribution | lower }}"
    version_pkg: "prefix_{{ ansible_distribution_major_version }}"

But after the sefact, the list is lost...

How can I set my dynamic(variables) list name as a fact ?


回答1:


Here the final set_fact I used:

- name: Building package list for {{ ansible_distribution }} {{ ansible_distribution_major_version }}
  set_fact:
    common_pkg: "{{ prefix_commonpkglist }}"
    ditro_pkg: "{{ lookup('vars', 'prefix_' + ansible_distribution | lower) }}"
    version_pkg: "{{ lookup('vars','prefix_' + ansible_distribution_major_version) }}"

- name: Installing packages
  package:
    name: "{{ common_pkg + ditro_pkg + version_pkg }}"
    state: present


来源:https://stackoverflow.com/questions/64921342/ansible-set-a-fact-from-dynamic-variables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!