ansible-playbook api
Posted 3one
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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的主要内容,如果未能解决你的问题,请参考以下文章