ansible.inventory.Inventory class 从 Ansible 2.4+ 开始替换

ansible.inventory.Inventory class replacement from Ansible 2.4+ onwards

我目前正在使用 ansible 2.3.3 并尝试将其升级到新版本,但自 2.4+ 以来,ansible.inventory.Inventory class 已被删除。

有人知道 Ansible 2.4+ 中 ansible.inventory.Inventory 的等效推荐替代品是什么吗?

from ansible.inventory import Inventory
from ansible.parsing.dataloader import DataLoader
from ansible.vars import VariableManager

inv = Inventory(
        loader = DataLoader(),
        variable_manager = VariableManager(),
        host_list = my_path,
)

在较新的代码中有 isn't a one to one replacement 但可以使用以下代码,但请注意库存现在具有不同的数据结构

from ansible.inventory.manager import InventoryManager
from ansible.parsing.dataloader import DataLoader

m = InventoryManager(loader=DataLoader(), sources='/path/to/inventory')

m.hosts
m.groups