我使用ansible通过AWS或其他云供应商供应多个实例。这是通过python API ansible提供完成的。
如果按顺序运行Playbook,则可以正常运行。我的意思是顺序执行,一次执行一个,只有前一个完成后才转到下一个。
但是我想以异步形式执行此操作。我想同时执行几本剧本。同时创建多个实例。
尝试同时运行至少2或3时,它崩溃并显示以下错误:
File "/home/usr/proj/ansible.py", line 18, in __init__
self.inventory = InventoryManager(loader=self.loader, sources='/etc/ansible/hosts')
File "/home/usr/proj/venv/lib/python3.6/site-packages/ansible/inventory/manager.py", line 146, in __init__
self.parse_sources(cache=True)
File "/home/usr/proj/venv/lib/python3.6/site-packages/ansible/inventory/manager.py", line 207, in parse_sources
parse = self.parse_source(source, cache=cache)
File "/home/usr/proj/venv/lib/python3.6/site-packages/ansible/inventory/manager.py", line 253, in parse_source
for plugin in self._fetch_inventory_plugins():
File "/home/user/proj/venv/lib/python3.6/site-packages/ansible/inventory/manager.py", line 186, in _fetch_inventory_plugins
plugin = inventory_loader.get(name)
File "/home/usr/proj/venv/lib/python3.6/site-packages/ansible/plugins/loader.py", line 562, in get
obj = getattr(self._module_cache[path], self.class_name)
AttributeError: module 'ansible.plugins.inventory.script' has no attribute 'InventoryModule'
出现的其他错误是跳过了多个插件,但不确定其是否与前一个插件有关。巨大列表中的一个小例子:
[警告]:正在跳过插件(/home/usr/venv/lib/python3.6/site- 包/ansible/plugins/connection/buildah.py) 无效:模块 'ansible.plugins.connection./home/usr/venv/lib/python3.6/site- 包/ ansible /插件/连接/ buildah'没有属性 “连接”
我的应用中声明库存的代码为:
def __init__(self):
self.loader = DataLoader()
self.inventory = InventoryManager(loader=self.loader, sources='/etc/ansible/hosts')
self.variable_manager = VariableManager(loader=self.loader, inventory=self.inventory)
def run_playbook(self, playbook_path: str, playbook_name: str, extra_vars: dict = None, callback=None):
variable_manager = VariableManager(loader=self.loader, inventory=self.inventory)
password = open("pass", "r").read()
variable_manager._extra_vars = {"ansible_sudo_pass": password}
variable_manager._extra_vars = {**variable_manager.extra_vars, **extra_vars}
playbook = playbook_path + playbook_name
if not os.path.exists(playbook_path):
print('[INFO] The playbook does not exist')
sys.exit()
# List Of Obligatory Flags To Pass When Running The Playbook
context.CLIARGS = ImmutableDict(listtags=False,
listtasks=False,
listhosts=False,
syntax=False,
start_at_task=None,
connection='ssh',
module_path=None,
forks=100,
remote_user='root',
private_key_file=None,
ssh_common_args='-o StrictHostKeyChecking=no',
ssh_extra_args='-o StrictHostKeyChecking=no',
sftp_extra_args=None,
scp_extra_args=None,
become=True,
become_method='sudo',
become_user='root',
verbosity=5,
check=False,
diff=False,
force_valid_group_names='never')
pbex = PlaybookExecutor(playbooks=[playbook],
inventory=self.inventory,
variable_manager=variable_manager,
loader=self.loader,
passwords={})
pbex._tqm._stdout_callback = callback
return pbex.run()
尝试了两个source ='localhost'和主机清单的直接路径
Ansble版本是2.8,操作系统是Ubuntu 18
我使用的代码只有一段时间,线程调用了一个其他端点,这将触发执行剧本,这意味着上面的代码:
i = 0
while i <= 2:
x = threading.Thread(target=thread_func, args=(str(i),))
x.start()
i += 1