首页 > 代码库 > ansible-playbook api

ansible-playbook api

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