2016-05-31 38 views
3

我試圖重新編寫此腳本(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.ymlhosts文件,但我不知道在哪裏指向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": ""} 

回答

4

同時使用Ansible蟒蛇的API我有同樣的問題。經過長時間的研究和一些深入研究,我找到了可能的原因。 根據Ansible Documentation您應該設置become_method供應成爲和become_user時,您還應該設置become_method值。一般來說,CLI命令會從ansible.cfg文件中讀取。 但是,在API情況下,您將become_method設置爲None,它在成爲特定用戶(您的情況下爲root)時被內部調用。

將become_method設置爲(sudo,,su,pbrun,pfexec,doas,dzdo)中的一個值並且它應該可以正常工作。

from collections import namedtuple 
from ansible.parsing.dataloader import DataLoader 
from ansible.vars import VariableManager 
from ansible.inventory import Inventory 
from ansible.playbook.play import Play 
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='sudo', 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()