【问题标题】:Ansible Python API - how to get variables defined in vars_files, host_vars, group_varsAnsible Python API - 如何获取在 vars_files、host_vars、group_vars 中定义的变量
【发布时间】:2020-06-05 05:44:43
【问题描述】:

我正在尝试使用Python API从所有可能的位置提取variables

在我的剧本test_play.yml 我有..

---
hosts: abc.com
gather_facts: no

roles:
  - my_roles

vars_files:
  - var/var.yml    

tasks:   
  .....
  .....

文件结构-

test_play.yml
roles
  - my_roles
    -- defaults
vars
  - var.yml
hosts
  - DEV
    -- hosts
    -- group_vars
       --- all.yml

我的代码——

host_file = path_to_host_file
play_path = path_to_playbook

inventory = InventoryManager(loader=DataLoader(), source=[host_file])
variable_manager = VariableManager(loader=DataLoader(), inventory=inventory)

pb = Playbook.load(play_path, variable_manager, DataLoader())
play = pb.get_plays()

# here I get one play, which is fine
all_vars = variable_manager.get_vars(play=plays[0])

all_vars 中,我只得到在inventory file, rules 中定义的变量。但是var.yml, group_vars/all.yml 中定义的变量丢失了。如何获得?

【问题讨论】:

  • 我也有同样的问题;有什么想法吗?

标签: python ansible ansible-inventory


【解决方案1】:

你只需要:

variable_manager.get_vars(host=inventory.get_host("hostname"))

很遗憾 Ansible 没有为他们的 API 提供更好的文档。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-18
    相关资源
    最近更新 更多