Ansible变量优先级 – 什么是’角色参数’和’包含参数’

huangapple go评论58阅读模式
英文:

Ansible Variable precedence - what are 'role params' & 'include params'

问题

Ansible文档中有一份变量优先级的列表。

其中一些对我来说很清楚,但我想知道是否有人可以对这两个进行一些解释。

> 20. role(和include_role)参数
> 21. include参数

用法、位置、语法。

我正在尝试将变量声明提到更复杂的剧本中,我目前正在使用两个角色。

具体来说...在一个角色的任务文件中,一些值最好被声明为一个位置的变量。

英文:

The Ansible documentation has a list of Variable precedence

Some of them are clear to me but I wonder whether anybody coulde kinkdy shed some light on those 2.

> 20. role (and include_role) params
> 21. include params

usage, location, syntax.

I am trying to get Variable declaration a little further to the surface inside a little more complex playbook utilizing 2 roles I am currently working on.

concrete ... some values inside task files of a role should rather be declared as variables in a single location.

答案1

得分: 4

在简要示例中:

角色参数

(完整剧本)

- name: 我是一个虚拟剧本
  hosts: localhost

  roles:
    - role: somerole
      vars:
        param1: "我是一个角色参数"

包含角色参数

(仅任务)

- name: 包含角色 somerole
  ansible.builtin.include_role:
    name: somerole
  vars:
    param1: "我是一个包含角色参数"

包含参数

(仅任务)

- name: 包含任务文件
  ansible.builtin.include_tasks: sometasks.yaml
  vars:
    param1: "我是一个包含参数"

作为虚构且(很可能是不良实践)的示例:如果您在包含一个传递参数的角色后,稍后在该角色中包含一个任务文件并传递相同参数但具有不同值,那么包含参数将优先于角色参数。

英文:

In a nutshell examples:

role params

(full playbook)

- name: I'm a dummy play
  hosts: localhost

  roles:
    - role: somerole
      vars:
        param1: "I'm a role param"

Include role params

(task only)

- name: Including role somerole
  ansible.builtin.include_role:
    name: somerole
  vars:
    param1: "I'm an include role param"

Include params

(task only)

- name: Including a task file
  ansible.builtin.include_tasks: sometasks.yaml
  vars:
    param1: "I'm an include param"

As a fictive and (most probably bad practice) example: if you include a role passing a parameter and later include a task file in that role passing that same parameter with a different value, the include param will take precedence over the role param.

huangapple
  • 本文由 发表于 2023年6月2日 15:05:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76387881.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定