2016-05-31 4 views
3

Ich versuche dieses Skript (How to use Ansible 2.0 Python API to run a Playbook?) zu schreiben, um ein Ansible-Playbook in einem Python-Skript aufzurufen, das gegen Windows-Clients ausgeführt wird. Ich habe einen Testordner, der das Playbook (deploy.yml) enthält die Host-Datei (hosts) und zusätzliche Variablen (ansible_user, ansible_port ..) im Unterordner group_vars/win_clones.yml.Ansible Python API 2.0: Ausführen eines Playbooks in einem Python-Skript für Windows-Clients

Ich möchte auf diese Dateien verweisen, um mein Playbook innerhalb des Python-Skripts zu starten. Ich tat dies für deploy.yml und hosts Dateien, aber ich weiß nicht, wo auf group_vars/win_clones.yml zeigen. Wie kann ich das schaffen?

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() 

EDIT 1

Dies ist der Ausgang des Spiels durch Python-Skript ausführen:

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": ""} 

Antwort

4

ich gleiche Problem gehabt haben, während ansible APIs Python verwenden. Nach einer langen Recherche und einigen Nachforschungen nach einem möglichen Code fand ich die mögliche Ursache. Gemäß Ansible Documentation sollten Sie, set_method beim Bereitstellen von werden und were_user setzen, sollten Sie auch Wert für "Methode_Methode" festlegen. Im allgemeinen CLI-Befehl würde dies aus ansible.cfg-Datei lesen. Im API-Fall setzen Sie jedoch "anemethode" explizit auf "None", was intern aufgerufen wird, während Sie als spezifischer Benutzer (in Ihrem Fall root) wird.

Setzen Sie "Methode" als einen der Werte aus (sudo,, su, pbrun, pfexec, doas, dzdo) und es sollte gut funktionieren.

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() 
Verwandte Themen