在具有操作系统特定默认值的角色中定义 Ansible 变量,该默认值可以轻松覆盖 [英] Define Ansible variable in a role with OS specific default which can be easily overridden
问题描述
我正在编写一个 Ansible 角色,该角色可用于不同的 Linux 操作系统系列,并且每个操作系统系列的变量具有不同的默认值.
I'm writing an Ansible role that is usable on different Linux OS families and has a different default value for variables per OS family.
起初,我认为在角色中使用 include vars 任务会很容易设置,例如:
At first, I thought this would be easy to set up using an include vars task in the role, such as:
- name: Gather OS family variables
include_vars: "{{ ansible_os_family|lower }}.yml"
与
my_role_variable: "Here is the default for Enterprise Linux"
在myrole/vars/redhat.yml
my_role_variable: "Here is the default for Debian Linux"
在myrole/vars/debian.yml
但是,就我而言,使用角色的剧本能够轻松覆盖默认值非常重要.
However, in my case it's very important that a playbook using the role be able to easily override the default values.
因此,我试图找出一种方法,为每个操作系统系列的变量设置不同的默认值,如上所述,但我希望变量是角色默认变量而不是角色包含变量.有没有办法做到这一点?
So, I'm trying to figure out a way to set up a different default value for the variable per OS family as described above, but I want the variables be role default vars instead of role include vars. Is there a way to do this?
推荐答案
如果在 defaults/main.yml 文件中有类似以下内容会怎样
what about having something like the following in your defaults/main.yml file
my_role_variable_default: "global default"
my_role_variable_redhat: "redhat specific default"
my_role_variable: "{{ lookup('vars', 'my_role_variable_'+ansible_os_family|lower, default=my_role_variable_default) }}"
这篇关于在具有操作系统特定默认值的角色中定义 Ansible 变量,该默认值可以轻松覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!