Ansible Python API如何获取在变量文件、主机变量、组变量中定义的变量

2024-04-26 23:31:22 发布

您现在位置:Python中文网/ 问答频道 /正文

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

在我的剧本中我有

---
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中定义的变量缺失。如何得到它们


Tags: pathhostplaygetvarymlmanagervars