【发布时间】:2016-05-31 10:10:42
【问题描述】:
我正在尝试重新编写此脚本 (How to use Ansible 2.0 Python API to run a Playbook?) 以在针对 Windows 客户端运行的 python 脚本中调用 ansible playbook。我有一个测试文件夹,其中包含子文件夹 group_vars/win_clones.yml 中的 playbook (deploy.yml) 主机文件 (hosts) 和其他变量 (ansible_user,ansible_port..)。
我想指向这些文件以在 python 脚本中运行我的剧本。我为deploy.yml 和hosts 文件这样做了,但我不知道在哪里指向group_vars/win_clones.yml。我怎样才能做到这一点?
from collections import namedtuple
from ansible.parsing.dataloader import DataLoader
from ansible.vars import VariableManager
from ansible.inventory import Inventory
from ansible.executor.playbook_executor import PlaybookExecutor
variable_manager = VariableManager()
loader = DataLoader()
inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list='fullpath/to/hosts')
playbook_path = 'fullpath/to/deploy.yml'
if not os.path.exists(playbook_path):
print '[INFO] The playbook does not exist'
sys.exit()
Options = namedtuple('Options', ['listtags', 'listtasks', 'listhosts', 'syntax', 'connection','module_path', 'forks', 'remote_user', 'private_key_file', 'ssh_common_args', 'ssh_extra_args', 'sftp_extra_args', 'scp_extra_args', 'become', 'become_method', 'become_user', 'verbosity', 'check'])
options = Options(listtags=False, listtasks=False, listhosts=False, syntax=False, connection='ssh', module_path=None, forks=100, remote_user='slotlocker', private_key_file=None, ssh_common_args=None, ssh_extra_args=None, sftp_extra_args=None, scp_extra_args=None, become=True, become_method=None, become_user='root', verbosity=None, check=False)
variable_manager.extra_vars = {'ansible_user': 'ansible', 'ansible_port': '5986', 'ansible_connection': 'winrm', 'ansible_password': 'pass', 'ansible_winrm_server_cert_validation': 'ignore'} # Here are the variables used in the winclones.yml
passwords = {}
pbex = PlaybookExecutor(playbooks=[playbook_path], inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords=passwords)
results = pbex.run()
编辑 1
这是通过python脚本运行的输出:
TASK [setup] *******************************************************************
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: AttributeError: 'NoneType' object has no attribute 'upper'
fatal: [cl3]: FAILED! => {"failed": true, "msg": "Unexpected failure during module execution.", "stdout": ""}
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: AttributeError: 'NoneType' object has no attribute 'upper'
fatal: [cl1]: FAILED! => {"failed": true, "msg": "Unexpected failure during module execution.", "stdout": ""}
【问题讨论】: