本文整理汇总了Python中ansible.playbook.PlayBook方法的典型用法代码示例。如果您正苦于以下问题:Python playbook.PlayBook方法的具体用法?Python playbook.PlayBook怎么用?Python playbook.PlayBook使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ansible.playbook
的用法示例。
在下文中一共展示了playbook.PlayBook方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: run
# 需要导入模块: from ansible import playbook [as 别名]
# 或者: from ansible.playbook import PlayBook [as 别名]
def run(self, playbook_relational_path, extra_vars=None):
"""
run ansible playbook,
only surport relational path.
"""
stats = callbacks.AggregateStats()
playbook_cb = callbacks.PlaybookCallbacks(verbose=utils.VERBOSITY)
runner_cb = callbacks.PlaybookRunnerCallbacks(stats, verbose=utils.VERBOSITY)
playbook_path = os.path.join(ANSIBLE_DIR, playbook_relational_path)
pb = PlayBook(
playbook=playbook_path,
stats=stats,
callbacks=playbook_cb,
runner_callbacks=runner_cb,
inventory=self.inventory,
extra_vars=extra_vars,
check=False)
self.results = pb.run()
示例2: run
# 需要导入模块: from ansible import playbook [as 别名]
# 或者: from ansible.playbook import PlayBook [as 别名]
def run(self, task, playbook, extra_vars=None, check=False):
stats = MyAggregateStats()
playbook_callback = callbacks.PlaybookCallbacks(verbose=utils.VERBOSITY)
runner_callback = callbacks.PlaybookRunnerCallbacks(stats, verbose=utils.VERBOSITY)
abs_playbook_path = os.path.join(current_app.config['ANSIBLE_PLAYBOOKS_DIR'], playbook)
pb = PlayBook(
playbook=abs_playbook_path,
stats=stats,
callbacks=playbook_callback,
runner_callbacks=runner_callback,
inventory=self.inventory,
extra_vars=extra_vars,
check=check,
)
self.result_raw = pb.run()
self.result_raw['celery_task_id'] = task
log_redis(self.result_raw)
return self.result_raw
示例3: adduser
# 需要导入模块: from ansible import playbook [as 别名]
# 或者: from ansible.playbook import PlayBook [as 别名]
def adduser(ips, users):
inventory ="""
{% for i in hosts -%}
{{ i }}
{% endfor %}
"""
inventory_template = jinja2.Template(inventory)
rendered_inventory = inventory_template.render({'hosts': ips})
hosts = NamedTemporaryFile(delete=False,suffix='tmp',dir='/tmp/ansible/')
hosts.write(rendered_inventory)
hosts.close()
inventory = Inventory(hosts.name)
stats = callbacks.AggregateStats()
playbook_cb =callbacks.PlaybookCallbacks()
runner_cb =callbacks.PlaybookRunnerCallbacks(stats)
vars={}
vars['users'] = users
results = PlayBook(playbook='user.yaml',callbacks=playbook_cb,runner_callbacks=runner_cb,stats=stats,inventory=inventory,extra_vars=vars)
res = results.run()
logs = []
logs.append("finish playbook\n")
logs.append(str(res))
return logs
示例4: run
# 需要导入模块: from ansible import playbook [as 别名]
# 或者: from ansible.playbook import PlayBook [as 别名]
def run(self, playbook_relational_path, extra_vars=None):
"""
run ansible playbook,
only surport relational path.
"""
stats = callbacks.AggregateStats()
playbook_cb = callbacks.PlaybookCallbacks(verbose=utils.VERBOSITY)
runner_cb = callbacks.PlaybookRunnerCallbacks(
stats, verbose=utils.VERBOSITY)
playbook_path = os.path.join(ANSIBLE_DIR, playbook_relational_path)
pb = PlayBook(
playbook=playbook_path,
stats=stats,
callbacks=playbook_cb,
runner_callbacks=runner_cb,
inventory=self.inventory,
extra_vars=extra_vars,
check=False)
self.results = pb.run()
示例5: playbook_run
# 需要导入模块: from ansible import playbook [as 别名]
# 或者: from ansible.playbook import PlayBook [as 别名]
def playbook_run(inventory, playbook, default_user=None, default_port=None, default_pri_key_path=None):
stats = callbacks.AggregateStats()
playbook_cb = callbacks.PlaybookCallbacks(verbose=utils.VERBOSITY)
runner_cb = callbacks.PlaybookRunnerCallbacks(stats, verbose=utils.VERBOSITY)
# run the playbook
print default_user, default_port, default_pri_key_path, inventory, playbook
if default_user and default_port and default_pri_key_path:
playbook = PlayBook(host_list=inventory,
playbook=playbook,
forks=5,
remote_user=default_user,
remote_port=default_port,
private_key_file=default_pri_key_path,
callbacks=playbook_cb,
runner_callbacks=runner_cb,
stats=stats,
become=True,
become_user='root')
else:
playbook = PlayBook(host_list=inventory,
playbook=playbook,
forks=5,
callbacks=playbook_cb,
runner_callbacks=runner_cb,
stats=stats,
become=True,
become_user='root')
results = playbook.run()
print results
results_r = {'unreachable': [], 'failures': [], 'success': []}
for hostname, result in results.items():
if result.get('unreachable', 2):
results_r['unreachable'].append(hostname)
print "%s >>> unreachable" % hostname
elif result.get('failures', 2):
results_r['failures'].append(hostname)
print "%s >>> Failed" % hostname
else:
results_r['success'].append(hostname)
print "%s >>> Success" % hostname
return results_r
示例6: Playbook
# 需要导入模块: from ansible import playbook [as 别名]
# 或者: from ansible.playbook import PlayBook [as 别名]
def Playbook():
vars={}
inventory = Inventory(hostfile)
stats = callbacks.AggregateStats()
playbook_cb =callbacks.PlaybookCallbacks()
runner_cb =callbacks.PlaybookRunnerCallbacks(stats)
hosts=request.args.get('ip')
task=request.args.get('playbook')
vars['hosts'] = hosts
play=task + '.yml'
results = PlayBook(playbook=play,callbacks=playbook_cb,runner_callbacks=runner_cb,stats=stats,inventory=inventory,extra_vars=vars)
res = results.run()
return json.dumps(res,indent=4)
示例7: playbook
# 需要导入模块: from ansible import playbook [as 别名]
# 或者: from ansible.playbook import PlayBook [as 别名]
def playbook():
inventory = """
[initial]
{% for i in hosts %}
{{ i }}
{% endfor %}
"""
inventory_template = jinja2.Template(inventory)
#inst_ip = [ i.encode('ascii') for i in request.form['ips']]
#ips = [ i for i in request.form.getlist("ips") ]
#inst_ip = request.form.get('ips')
#inst_ip = json.loads(request.form.getlist('ips'))
#inst_ip = [ i.encode('ascii') for i in request.form.getlist('ips') ]
#print inst_ip
#print request.form
print request.get_data()
#data = request.get_data()
#inst_ip = request.form.getlist('ips[]')
#print data
#inst_ip = data['ips']
#inst_ip = request.form.getlist('ips')
data = json.loads(request.get_data())
inst_ip = data["ips"]
print inst_ip
#inst_ip = ["192.168.1.3","192.168.1.4","192.168.1.5"]
rendered_inventory = inventory_template.render({'hosts':inst_ip})
hosts = NamedTemporaryFile(delete=False,suffix='tmp',dir='/tmp/ansible/')
hosts.write(rendered_inventory)
hosts.close()
inventory = Inventory(hosts.name)
print inventory
vars = {}
stats = callbacks.AggregateStats()
playbook_cb = callbacks.PlaybookCallbacks()
runner_cb = callbacks.PlaybookRunnerCallbacks(stats)
# hosts = request.args.get('ip')
# task = request.args.get('playbook')
# vars['hosts'] = hosts
# play = task + '.yml'
pb = PlayBook(playbook='mytest.yml',callbacks=playbook_cb,runner_callbacks=runner_cb,stats=stats,inventory=inventory,extra_vars=vars)
job = q.enqueue_call(pb.run, result_ttl=5000, timeout=2000)
jid = job.get_id()
if jid:
app.logger.info("Job Succesfully Queued with JobID: %s" % jid)
else:
app.logger.error("Failed to Queue the Job")
return jid