我試圖重新編寫此腳本(How to use Ansible 2.0 Python API to run a Playbook?),以在針對Windows客戶端運行的python腳本中調用一個合理的劇本。我有一個測試文件夾,其中包含子文件夾group_vars/win_clones.yml
中的主機文件(hosts
)和其他變量(ansible_user
,ansible_port
..)中的操作手冊(deploy.yml
)。Ansible python API 2.0:在Windows客戶端的python腳本中運行劇本
我想指向這些文件在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": ""}