Import OS
Import Sys
Import Ansible
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
def playbook_action (Playbook, hosts):
Variable_manager = Variablemanager ()
Loader = Dataloader ()
Inventory = ansible.inventory.Inventory (Loader=loader,variable_manager=variable_manager)
Inventory.clear_pattern_cache ()
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=none, Private_key_file=none, Ssh_common_args=none, Ssh_extra_args=none, Sftp_extra_args=None , Scp_extra_args=none, Become=none, Become_method=none, Become_user=none, Verbosity=none, Check=False)
Variable_manager.extra_vars = {' Host ': Hosts}
passwords = {}
Pbex = Playbookexecutor (Playbooks=[playbook], inventory=inventory, Variable_manager=variable_manager, Loader=loader , Options=options, Passwords=passwords)
Pbex.run ()
Ansible-playbook API