gpt4 book ai didi

python - Ansible:从自定义模块中访问主机/组变量

转载 作者:太空狗 更新时间:2023-10-29 20:24:10 26 4
gpt4 key购买 nike

有没有一种方法可以从 custom written module 中访问主机/组变量? ?我想避免将所有必需的变量作为模块参数传递。

我的模块是用 Python 编写的,我使用的是样板文件。我检查了几乎所有可用的变量,但它们没有存储在任何地方:

def main():
pprint(dir())
pprint(globals())
pprint(locals())
for name in vars().keys():
print(name)

现在我唯一的希望是可以通过未记录的模块实用程序以某种方式访问​​它们。

我猜这是不可能的,因为模块在目标机器上运行并且可能事实/主机/组变量没有随模块一起传输...

编辑:找到 module utils现在看起来并不乐观。

最佳答案

Is there a way how one can access host/group vars from within a customwritten module?

不是内置的。

您必须自己以一种或另一种方式传递它们:

  • 模块参数。
  • 序列化到本地文件系统(使用 pickleyaml.dump()json 或 ...)并发送文件.
  • 您能想到的任何其他创新点子。

不幸的是,你不能按原样发送整个主机/groupvar 文件,因为你必须实现 ansible 的变量范围/优先级解析算法,这是未定义的(它不是 Zen philosophy of ansible 来定义这种小事事情 :P).

--编辑--

我看到他们有 some precedence defined现在。

Ansible does apply variable precedence, and you might have a use forit. Here is the order of precedence from least to greatest (the lastlisted variables override all other variables):

  • command line values (for example, -u my_user, these are not variables)
  • role defaults (defined in role/defaults/main.yml) 1
  • inventory file or script group vars 2
  • inventory group_vars/all 3
  • playbook group_vars/all 3
  • inventory group_vars/* 3
  • playbook group_vars/* 3
  • inventory file or script host vars 2
  • inventory host_vars/* 3
  • playbook host_vars/* 3
  • host facts / cached set_facts 4
  • play vars
  • play vars_prompt
  • play vars_files
  • role vars (defined in role/vars/main.yml)
  • block vars (only for tasks in block)
  • task vars (only for the task)
  • include_vars
  • set_facts / registered vars
  • role (and include_role) params
  • include params
  • extra vars (for example, -e "user=my_user")(always win precedence)

In general, Ansible gives precedence to variables that were definedmore recently, more actively, and with more explicit scope. Variablesin the defaults folder inside a role are easily overridden. Anythingin the vars directory of the role overrides previous versions of thatvariable in the namespace. Host and/or inventory variables overriderole defaults, but explicit includes such as the vars directory or aninclude_vars task override inventory variables.

Ansible merges different variables set in inventory so that morespecific settings override more generic settings. For example,ansible_ssh_user specified as a group_var is overridden byansible_user specified as a host_var. For details about the precedenceof variables set in inventory, see How variables are merged.

Footnotes

1 Tasks in each role see their own role’s defaults. Tasks definedoutside of a role see the last role’s defaults.

2(1,2) Variables defined in inventory file or provided by dynamicinventory.

3(1,2,3,4,5,6) Includes vars added by ‘vars plugins’ as well ashost_vars and group_vars which are added by the default vars pluginshipped with Ansible.

4 When created with set_facts’s cacheable option, variables have thehigh precedence in the play, but are the same as a host factsprecedence when they come from the cache.

关于python - Ansible:从自定义模块中访问主机/组变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29026094/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com