gpt4 book ai didi

python - 以编程方式运行 Ansible 剧本?

转载 作者:行者123 更新时间:2023-11-28 17:06:40 27 4
gpt4 key购买 nike

我有一个 python 应用程序调用下面的代码,计划通过 Ansible API 以编程方式运行 Ansible 剧本,而不是使用子进程之类的东西。

下面的代码运行但实际上似乎没有执行任何内容。抓取结果的输出只是给了我一个看起来像这样的字典:

[{'plays': [localhost], 'playbook': 'playbooks/asg_elb_example.yml'}]

我不确定哪里出了问题或遗漏了什么。这是我正在运行的代码。

import os
import sys
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
ROOT_DIR = os.path.dirname(SCRIPT_DIR)

import json
from collections import namedtuple
from ansible.parsing.dataloader import DataLoader
from ansible.vars.manager import VariableManager
from ansible.inventory.manager import InventoryManager
from ansible.playbook.play import Play
from ansible.executor.playbook_executor import PlaybookExecutor

def ansible_part():
playbook_path = "playbooks/asg_elb_example.yml"
inventory_path = "hosts"

Options = namedtuple('Options', ['connection', 'module_path', 'forks', 'become', 'become_method', 'become_user', 'check', 'diff', 'listhosts', 'listtasks', 'listtags', 'syntax'])
loader = DataLoader()
options = Options(connection='local', module_path='%s/' % (ROOT_DIR), forks=100, become=None, become_method=None, become_user=None, check=False,
diff=False, listhosts=True, listtasks=False, listtags=False, syntax=False)
passwords = dict(vault_pass='secret')

inventory = InventoryManager(loader=loader, sources=[inventory_path])
variable_manager = VariableManager(loader=loader, inventory=inventory)
executor = PlaybookExecutor(
playbooks=[playbook_path], inventory=inventory, variable_manager=variable_manager, loader=loader,
options=options, passwords=passwords)
results = executor.run()
print results

def main():
ansible_part()

sys.exit(main())

最佳答案

您告诉 Ansible 使用 listhosts=True 列出主机,它正在正确地完成它的工作。

要执行剧本更改为 listhosts=False

关于python - 以编程方式运行 Ansible 剧本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50533577/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com