ImportError: cannot import name Inventory
ImportError: cannot import name VariableManager
最近项目中用到了ansible,有个以前别人写好的程序,但是死活就是运行不起来,报错如下
然后检查了一下报错的python文件,里面是如下语句
from ansible.inventory import Inventory
在shell 窗口可以正常通过ansible --version 查询到版本号,可以确认的是ansible安装是没有问题,到底是哪个环节出问题了呢?
通过百度,终于找到原因,原来ansible 2.4 以上的版本导入Inventory的姿势和以前不一样了
ansible2.4以前版本导入的方式如下
import os
import json
import re
from tempfile import NamedTemporaryFile
from ansible.inventory import Inventory
from ansible.vars import VariableManager
from ansible.parsing.dataloader import DataLoader
from ansible.executor import playbook_executor
from ansible.executor.playbook_executor import PlaybookExecutor
from ansible.plugins.callback import CallbackBase
from ansible.errors import AnsibleParserError
from ansible.utils.display import Display
from collections import namedtuple
from symbol import except_clause
ansible2.4以后版本导入的方式是这样的
import os
import json
import re
from tempfile import NamedTemporaryFile
from ansible.inventory.manager import InventoryManager
from ansible.vars.manager import VariableManager
from ansible.parsing.dataloader import DataLoader
from ansible.executor import playbook_executor
from ansible.executor.playbook_executor import PlaybookExecutor
from ansible.plugins.callback import CallbackBase
from ansible.errors import AnsibleParserError
from ansible.utils.display import Display
from collections import namedtuple
from symbol import except_clause
主要变化是Inventory和 VariableManager 模块的导入方式
2.4 以前 from ansible.inventory import Inventory
2.4以后 from ansible.inventory.manager import InventoryManager
2.4 以前 from ansible.vars import VariableManager
2.4以后 from ansible.vars.manager import VariableManager