安塞布尔版本:2.0.0.1
我现在已经四处寻找了很多,我发现的大多数文档要么不完整,要么已弃用(这篇文章适用于1.8.4版本,即 https://stackoverflow.com/questions/27590039/running-ansible-playbook-using-python-api?newreg=7a292f45eb6c43e2a4c3be5ac30efc11)
我正在尝试通过 Python API 启动 Ansible playbook。 Ansible 的文档似乎展示了如何生成和播放任务,但没有展示如何加载和运行 playbook yml 文件。我一直在深入研究代码,试图了解如何启动它,我认为我已经取得了一些进展,但我真的遇到了困难。这是我到目前为止所拥有的:
def createcluster(region, environment, cluster):
Options = namedtuple('Options', ['region','env', 'cluster'])
# initialize needed objects
variable_manager = VariableManager()
loader = DataLoader()
options = Options(region=region, env=environment, cluster=cluster)
options.listhosts = False
vault_password = getpass.getpass('Enter vault password :')
passwords = dict(vault_pass=vault_password)
#Getting hosts
hostsread = open('provisioning/inventory/hosts','r')
hosts = hostsread.read()
inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list=hosts)
variable_manager.set_inventory(inventory)
#Create and load the playbook file
playbook = Playbook(loader)
playbook.load('provisioning/cluster.yml', variable_manager,loader)
#Create an executor to launch the playbook ?
executor = None
executor = PlaybookExecutor(playbook,inventory,variable_manager,loader,options,passwords)
try:
result = executor.run()
finally:
if executor is not None:
executor.cleanup()
我完全不确定执行器部分,当我尝试启动代码时,我不断收到“AttributeError:'Options'对象没有属性'listhosts'”错误(很奇怪,因为它应该忽略它的缺失,我认为(第 60 行) https://github.com/ansible/ansible/blob/stable-2.0.0.1/lib/ansible/executor/playbook_executor.py)
我应该如何加载 YML 文件并通过 Python API 启动它?我是走在正确的道路上还是迷失了自己?为什么 Ansible 没有更好的文档记录?为什么 42 是 7*7 的答案?