本文整理汇总了Python中ansible.executor.playbook_executor.PlaybookExecutor方法的典型用法代码示例。如果您正苦于以下问题:Python playbook_executor.PlaybookExecutor方法的具体用法?Python playbook_executor.PlaybookExecutor怎么用?Python playbook_executor.PlaybookExecutor使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ansible.executor.playbook_executor
的用法示例。
在下文中一共展示了playbook_executor.PlaybookExecutor方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: run
# 需要导入模块: from ansible.executor import playbook_executor [as 别名]
# 或者: from ansible.executor.playbook_executor import PlaybookExecutor [as 别名]
def run(self, playbook_path, extra_vars=None, **kwargs):
C.DEFAULT_ROLES_PATH = self.options.roles_path
if extra_vars and isinstance(extra_vars, dict):
self.variable_manager.extra_vars = extra_vars
executor = PlaybookExecutor(
playbooks=[playbook_path],
inventory=self.inventory,
variable_manager=self.variable_manager,
loader=self.loader,
options=self.options,
passwords=self.options.passwords
)
if executor._tqm:
executor._tqm._stdout_callback = self.results_callback
try:
executor.run()
return self.results_callback.results
except AnsibleError:
executor._tqm.cleanup()
self.loader.cleanup_all_tmp_files()
示例2: run
# 需要导入模块: from ansible.executor import playbook_executor [as 别名]
# 或者: from ansible.executor.playbook_executor import PlaybookExecutor [as 别名]
def run(self):
executor = PlaybookExecutor(
playbooks=[self.playbook_path],
inventory=self.inventory,
variable_manager=self.variable_manager,
loader=self.loader,
options=self.options,
passwords=self.passwords
)
if executor._tqm:
executor._tqm._stdout_callback = self.results_callback
executor.run()
executor._tqm.cleanup()
try:
results_callback = self.results_callback.output['plays'][0]['tasks'][1]['hosts']
status = self.results_callback.output['stats']
results = {"results_callback": results_callback, "status": status}
return results
except Exception as e:
raise AnsibleError(
'The hostname parameter or groups parameter in the BaseInventory \
does not match the hosts parameter in the yaml file.{}'.format(e))
示例3: execute_playbook
# 需要导入模块: from ansible.executor import playbook_executor [as 别名]
# 或者: from ansible.executor.playbook_executor import PlaybookExecutor [as 别名]
def execute_playbook():
playbook_path = "playbook_template.yml"
inventory_path = "hosts"
Options = namedtuple('Options', ['connection', 'module_path', 'forks', 'become', 'become_method', 'become_user', 'check', 'diff', 'listhosts', 'listtasks', 'listtags', 'syntax'])
loader = DataLoader()
options = Options(connection='local', module_path='', forks=100, become=None, become_method=None, become_user=None, check=False,
diff=False, listhosts=False, listtasks=False, listtags=False, syntax=False)
passwords = dict(vault_pass='secret')
inventory = InventoryManager(loader=loader, sources=['inventory'])
variable_manager = VariableManager(loader=loader, inventory=inventory)
executor = PlaybookExecutor(
playbooks=[playbook_path], inventory=inventory, variable_manager=variable_manager, loader=loader,
options=options, passwords=passwords)
results = executor.run()
print results
开发者ID:PacktPublishing,项目名称:Learning-Python-Networking-Second-Edition,代码行数:19,代码来源:execute_playbook.py
示例4: ansible_part
# 需要导入模块: from ansible.executor import playbook_executor [as 别名]
# 或者: from ansible.executor.playbook_executor import PlaybookExecutor [as 别名]
def ansible_part():
playbook_path = "checktemplate.yml"
inventory_path = "hosts"
Options = namedtuple('Options', ['connection', 'module_path', 'forks', 'become', 'become_method', 'become_user', 'check', 'diff', 'listhosts', 'listtasks', 'listtags', 'syntax'])
loader = DataLoader()
options = Options(connection='local', module_path='', forks=100, become=None, become_method=None, become_user=None, check=False,
diff=False, listhosts=False, listtasks=False, listtags=False, syntax=False)
passwords = dict(vault_pass='secret')
inventory = InventoryManager(loader=loader, sources=['inventory'])
variable_manager = VariableManager(loader=loader, inventory=inventory)
executor = PlaybookExecutor(
playbooks=[playbook_path], inventory=inventory, variable_manager=variable_manager, loader=loader,
options=options, passwords=passwords)
results = executor.run()
print results
示例5: run_playbook
# 需要导入模块: from ansible.executor import playbook_executor [as 别名]
# 或者: from ansible.executor.playbook_executor import PlaybookExecutor [as 别名]
def run_playbook(self, playbook_path,extra_vars=None):
"""
run ansible palybook
"""
try:
# if self.redisKey:self.callback = PlayBookResultsCollectorToSave(self.redisKey,self.logId)
self.callback = PlayBookResultsCollector()
if extra_vars:self.variable_manager.extra_vars = extra_vars
executor = PlaybookExecutor(
playbooks=[playbook_path], inventory=self.inventory, variable_manager=self.variable_manager, loader=self.loader,
options=self.options, passwords=self.passwords,
)
executor._tqm._stdout_callback = self.callback
constants.HOST_KEY_CHECKING = False #关闭第一次使用ansible连接客户端是输入命令
executor.run()
except Exception as err:
return False
示例6: run
# 需要导入模块: from ansible.executor import playbook_executor [as 别名]
# 或者: from ansible.executor.playbook_executor import PlaybookExecutor [as 别名]
def run(self):
executor = PlaybookExecutor(
playbooks=[self.playbook_path],
inventory=self.inventory,
variable_manager=self.variable_manager,
loader=self.loader,
options=self.options,
passwords=self.passwords
)
if executor._tqm:
executor._tqm._stdout_callback = self.results_callback
executor.run()
executor._tqm.cleanup()
try:
results_callback=self.results_callback.output['plays'][0]['tasks'][1]['hosts']
status=self.results_callback.output['stats']
results={"results_callback":results_callback,"status":status}
return results
except Exception as e:
raise AnsibleError('The hostname parameter or groups parameter in the BaseInventory \
does not match the hosts parameter in the yaml file.')
示例7: run
# 需要导入模块: from ansible.executor import playbook_executor [as 别名]
# 或者: from ansible.executor.playbook_executor import PlaybookExecutor [as 别名]
def run(self, verbose=False):
if not verbose:
# Results of PlaybookExecutor
cb = DisplayErrorCallback()
self.pbex._tqm._stdout_callback = cb
try:
res = self.pbex.run()
except AnsibleParserError as err:
print(err)
return None
stats = self.pbex._tqm._stats
# Test if success for record_logs
run_success = True
hosts = sorted(stats.processed.keys())
for h in hosts:
t = stats.summarize(h)
if t['unreachable'] > 0 or t['failures'] > 0:
run_success = False
return stats
示例8: load_callbacks
# 需要导入模块: from ansible.executor import playbook_executor [as 别名]
# 或者: from ansible.executor.playbook_executor import PlaybookExecutor [as 别名]
def load_callbacks(self): # 为callback 设置存储id
pass
# 重新封装 PlaybookExecutor , 传入 task_id
示例9: __init__
# 需要导入模块: from ansible.executor import playbook_executor [as 别名]
# 或者: from ansible.executor.playbook_executor import PlaybookExecutor [as 别名]
def __init__(self, host_list, playbook_path, private_key_file="/root/.ssh/id_rsa", forks=5, extra_vars=None):
super(AnsiblePlayTask, self).__init__(host_list, private_key_file, forks, extra_vars)
passwords = {}
self.call_back = result_collector.CallbackModule()
self.pbex = PlaybookExecutor(playbooks=[playbook_path],
inventory=self.inventory,
variable_manager=self.variable_manager,
loader=self.loader,
options=self.options,
passwords=passwords)
self.pbex._tqm._stdout_callback = self.call_back
示例10: setup
# 需要导入模块: from ansible.executor import playbook_executor [as 别名]
# 或者: from ansible.executor.playbook_executor import PlaybookExecutor [as 别名]
def setup(self, pb_file):
self.pb_file = pb_file
self.playbook = PlaybookExecutor(playbooks=[self.pb_file],
inventory=self.inventory,
variable_manager=self.variable_manager,
loader=self.loader,
options=self.options,
passwords={})
示例11: __init__
# 需要导入模块: from ansible.executor import playbook_executor [as 别名]
# 或者: from ansible.executor.playbook_executor import PlaybookExecutor [as 别名]
def __init__(self, playbook, inventory, run_data=None, verbosity=0, tags=None, skip_tags=None):
self.run_data = run_data or {}
self.options = Options()
self.options.verbosity = verbosity
self.options.connection = 'local' # Need a connection type "smart" or "ssh"
self.options.become = True
self.options.become_method = 'sudo'
self.options.become_user = 'root'
self.options.tags = tags or []
self.options.skip_tags = skip_tags or []
# Set global verbosity
self.display = Display()
self.display.verbosity = self.options.verbosity
# Executor appears to have it's own
# verbosity object/setting as well
playbook_executor.verbosity = self.options.verbosity
# Become Pass Needed if not logging in as user root
passwords = {}
# Gets data from YAML/JSON files
self.loader = DataLoader()
self.loader.set_vault_password(os.environ.get('VAULT_PASS',''))
# All the variables from all the various places
self.variable_manager = VariableManager()
self.variable_manager.extra_vars = self.run_data
self.inventory = Inventory(loader=self.loader, variable_manager=self.variable_manager, host_list=inventory)
self.variable_manager.set_inventory(self.inventory)
# Setup playbook executor, but don't run until run() called
self.pbex = playbook_executor.PlaybookExecutor(
playbooks=[playbook],
inventory=self.inventory,
variable_manager=self.variable_manager,
loader=self.loader,
options=self.options,
passwords=passwords)
示例12: __init__
# 需要导入模块: from ansible.executor import playbook_executor [as 别名]
# 或者: from ansible.executor.playbook_executor import PlaybookExecutor [as 别名]
def __init__(self, playbook, hosts='hosts', options={}, passwords={}, vault_pass=None):
# Set options
self.options = Options()
for k, v in options.iteritems():
setattr(self.options, k, v)
# Set global verbosity
self.display = display
self.display.verbosity = self.options.verbosity
# Executor has its own verbosity setting
playbook_executor.verbosity = self.options.verbosity
# Gets data from YAML/JSON files
self.loader = DataLoader()
# Set vault password
if vault_pass is not None:
self.loader.set_vault_password(vault_pass)
elif 'VAULT_PASS' in os.environ:
self.loader.set_vault_password(os.environ['VAULT_PASS'])
# All the variables from all the various places
self.variable_manager = VariableManager()
if self.options.python_interpreter is not None:
self.variable_manager.extra_vars = {
'ansible_python_interpreter': self.options.python_interpreter
}
# Set inventory, using most of above objects
self.inventory = Inventory( loader=self.loader,
variable_manager=self.variable_manager,
host_list=hosts)
if len(self.inventory.list_hosts()) == 0:
# Empty inventory
self.display.error("Provided hosts list is empty.")
sys.exit(1)
self.inventory.subset(self.options.subset)
if len(self.inventory.list_hosts()) == 0:
# Invalid limit
self.display.error("Specified limit does not match any hosts.")
sys.exit(1)
self.variable_manager.set_inventory(self.inventory)
# Setup playbook executor, but don't run until run() called
self.pbex = playbook_executor.PlaybookExecutor(
playbooks=[playbook],
inventory=self.inventory,
variable_manager=self.variable_manager,
loader=self.loader,
options=self.options,
passwords=passwords)