本文整理汇总了Python中ansible.executor.task_queue_manager.TaskQueueManager类的典型用法代码示例。如果您正苦于以下问题:Python TaskQueueManager类的具体用法?Python TaskQueueManager怎么用?Python TaskQueueManager使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了TaskQueueManager类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: run_module
def run_module(self, hosts='localhost', rules=[{'module': 'setup'}]):
'''
rules=[
{'module': 'shell', 'args': 'echo "ok"', 'register': 'echo_ok'},
{'module': 'debug', 'args': {'msg': '{{echo_ok.stdout}}'}}
]
'''
tasks = []
for rule in rules:
if 'register' in rule:
register = rule.pop('register')
tasks.append(dict(dict(action=rule), register=register))
play_source = dict(
name = "Ansible Play",
hosts = hosts,
gather_facts = 'no',
tasks = tasks
)
results_callback = ResultCallback()
play = Play().load(play_source, variable_manager=self.variable_manager, loader=self.loader)
qm = None
try:
tqm = TaskQueueManager(
inventory=self.inventory,
variable_manager=self.variable_manager,
loader=self.loader,
options=self.options,
passwords=self.passwords,
stdout_callback=results_callback,
)
tqm.run(play)
return results_callback
finally:
if tqm is not None:
tqm.cleanup()
示例2: run
def run(self, play_data):
"""
paly_data = dict(
name="Ansible Ad-Hoc",
hosts=pattern,
gather_facts=True,
tasks=[dict(action=dict(module='service', args={'name': 'vsftpd', 'state': 'restarted'}), async=async, poll=poll)]
)
"""
self._prepare_run()
play = Play().load(play_data, variable_manager=self.variable_manager, loader=self.loader)
tqm = None
try:
tqm = TaskQueueManager(
inventory=self.inventory,
variable_manager=self.variable_manager,
loader=self.loader,
options=self.options,
passwords=self.passwords,
stdout_callback=self.cb,
run_additional_callbacks=C.DEFAULT_LOAD_CALLBACK_PLUGINS,
run_tree=False,
)
result = tqm.run(play)
return result
finally:
if tqm:
tqm.cleanup()
示例3: run_module
def run_module(self, hosts, module_name, module_args):
# create play with tasks
play_source = dict(
name="Ansible Play",
hosts=hosts,
gather_facts='no',
tasks=[dict(action=dict(module=module_name, args=module_args))])
play = Play().load(
play_source,
variable_manager=self.variable_manager,
loader=self.loader)
self.results_callback = ModuleResultCallback()
# actually run it
tqm = None
try:
tqm = TaskQueueManager(
inventory=self.inventory,
variable_manager=self.variable_manager,
loader=self.loader,
options=self.options,
passwords=self.passwords,
stdout_callback=self.
results_callback, # Use our custom callback instead of the ``default`` callback plugin
)
result = tqm.run(play)
finally:
if tqm is not None:
tqm.cleanup()
示例4: run
def run(self):
# create play with tasks
play_source = dict(
name = "Ansible Play",
hosts = self.host_list,
#hosts = 'localhost',
gather_facts = 'no',
tasks = [
dict(action=dict(module='shell', args='ls')),
#dict(action=dict(module='shell', args='ls'), register='shell_out'),
#dict(action=dict(module='debug', args=dict(msg='{{shell_out.stdout}}')))
]
)
play = Play().load(play_source, variable_manager=self.variable_manager, loader=self.loader)
# actually run it
tqm = None
try:
tqm = TaskQueueManager(
inventory=self.inventory,
variable_manager=self.variable_manager,
loader=self.loader,
options=self.options,
passwords=self.passwords,
stdout_callback=self.results_callback, # Use our custom callback instead of the ``default`` callback plugin
)
result = tqm.run(play)
finally:
if tqm is not None:
tqm.cleanup()
示例5: run_command
def run_command(cmd, host):
callback_ob = callback_class()
play_source = dict(
name = "Ansible Play",
hosts = host,
gather_facts = 'no',
tasks = [
dict(action=dict(module='shell', args=cmd), register='shell_out'),
dict(action=dict(module='debug', args=dict(msg='{{shell_out.stdout}}')))
]
)
play = Play().load(play_source, variable_manager=variable_manager, loader=loader)
task = TaskQueueManager(
inventory=inventory,
variable_manager=variable_manager,
loader=loader,
options=options,
passwords=passwords,
stdout_callback=callback_ob,
)
# redirecting stdout to grab the output from the task
normal_stdout = sys.stdout
sys.stdout = new_stdout = StringIO()
result = task.run(play)
sys.stdout = normal_stdout
# import pdb; pdb.set_trace()
return new_stdout.readlines()
示例6: run
def run(self, host_list, module_name, module_args,):
"""
run module from andible ad-hoc.
module_name: ansible module_name
module_args: ansible module args
"""
# create play with tasks
play_source = dict(
name="Ansible Play",
hosts=host_list,
gather_facts='no',
tasks=[dict(action=dict(module=module_name, args=module_args))]
)
play = Play().load(play_source, variable_manager=self.variable_manager, loader=self.loader)
# actually run it
tqm = None
self.callback = ResultsCollector()
try:
tqm = TaskQueueManager(
inventory=self.inventory,
variable_manager=self.variable_manager,
loader=self.loader,
options=self.options,
passwords=self.passwords,
)
tqm._stdout_callback = self.callback
result = tqm.run(play)
finally:
if tqm is not None:
tqm.cleanup()
示例7: my_runner
def my_runner(host_list,module_name,module_args):
variable_manager.extra_vars={} # 增加外部变量
# 构建pb, 这里很有意思, 新版本运行ad-hoc或playbook都需要构建这样的pb, 只是最后调用play的类不一样
# :param name: 任务名,类似playbook中tasks中的name
# :param hosts: playbook中的hosts
# :param tasks: playbook中的tasks, 其实这就是playbook的语法, 因为tasks的值是个列表,因此可以写入多个task
play_source = {"name":"Ansible Ad-Hoc","hosts":host_list,"gather_facts":"no","tasks":[{"action":{"module":module_name,"args":module_args}}]}
play = Play().load(play_source,variable_manager=variable_manager,loader=loader)
tqm = None
# results_callback = call_json.CallbackModule()
try:
tqm = TaskQueueManager(
inventory=inventory,
variable_manager=variable_manager,
loader=loader,
options=options,
passwords=None,
stdout_callback='minimal',
# stdout_callback=results_callback, # Use our custom callback instead of the ``default`` callback plugin
)
savedStdout = sys.stdout
with open(File_PATH,'w+') as file:
sys.stdout = file #标准输出重定向至文件
result = tqm.run(play)
sys.stdout = savedStdout
return result
finally:
if tqm is not None:
tqm.cleanup()
示例8: ansible_adhoc
def ansible_adhoc(ips):
logger.info("ansible需要采集%d个IP" % (len(ips)))
inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list=ips)
# 根据 inventory 加载对应变量
variable_manager.set_inventory(inventory)
# 增加外部变量
variable_manager.extra_vars = {"ansible_ssh_user": ansible_username, "ansible_ssh_pass": ansible_password}
play_source = {"name": "Ansible Ad-Hoc", "hosts": ips, "gather_facts": "no",
"tasks": [{"action": {"module": "setup", "args": ""}}]}
play = Play().load(play_source, variable_manager=variable_manager, loader=loader)
tqm = None
try:
tqm = TaskQueueManager(
inventory=inventory,
variable_manager=variable_manager,
loader=loader,
options=options,
passwords=None,
stdout_callback=resultcallback,
run_tree=False,
)
tqm.run(play)
return ansible_facts_info
finally:
if tqm is not None:
tqm.cleanup()
示例9: runansible
def runansible(self,host_list, task_list):
play_source = dict(
name = "Ansible Play",
hosts = host_list,
gather_facts = 'no',
tasks = task_list
)
play = Play().load(play_source, variable_manager=self.variable_manager, loader=self.loader)
tqm = TaskQueueManager(
inventory=self.inventory,
variable_manager=self.variable_manager,
loader=self.loader,
options=self.options,
passwords=self.passwords,
stdout_callback=self.callback,
)
result = tqm.run(play)
result_raw = {'success':{},'failed':{},'unreachable':{}}
for host,result in self.callback.host_ok.items():
result_raw['success'][host] = result._result
for host,result in self.callback.host_failed.items():
result_raw['failed'][host] = result._result
for host,result in self.callback.host_unreachable.items():
result_raw['unreachable'][host] = result._result
js = json.dumps(result_raw, sort_keys=False, indent=4)
return js
示例10: AnsibleTask
def AnsibleTask(task_list,host_list,user):
'''ansible python api 2.0'''
loader = DataLoader()
variable_manager = VariableManager()
inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list=host_list)
variable_manager.set_inventory(inventory)
task_dict = []
for i in task_list:
task_dict.append({"action": {"module": i[0], "args": i[1] }})
variable_manager.extra_vars = {"ansible_ssh_user": user, "ansible_ssh_pass": ""}
play_source = {"name" : "Ansible PlayBook Run", "hosts": host_list[0], "gather_facts": "no","tasks": task_dict}
play = Play().load(play_source, variable_manager=variable_manager, loader=loader)
tqm = None
try:
tqm = TaskQueueManager(
inventory = inventory,
variable_manager = variable_manager,
loader = loader,
options = options,
passwords = None,
stdout_callback = 'minimal',
run_tree = False,
)
result = tqm.run(play)
except Exception,e:
result = e
示例11: run_adhoc
def run_adhoc():
variable_manager.extra_vars = {
"ansible_ssh_user": "root", "ansible_ssh_pass": "Xinao.com123123"
} # 增加外部变量
# 构建pb, 这里很有意思, 新版本运行ad-hoc或playbook都需要构建这样的pb, 只是最后调用play的类不一样
# :param name: 任务名,类似playbook中tasks中的name
# :param hosts: playbook中的hosts
# :param tasks: playbook中的tasks, 其实这就是playbook的语法, 因为tasks的值是个列表,因此可以写入多个task
play_source = {
"name": "Ansible Ad-Hoc",
"hosts": "localhost",
"gather_facts": "no",
"tasks": [
{"action": {"module": "shell", "args": "w"}}
]
}
play = Play().load(play_source, variable_manager=variable_manager, loader=loader)
tqm = None
try:
tqm = TaskQueueManager(
inventory=inventory,
variable_manager=variable_manager,
loader=loader,
options=options,
passwords=None,
stdout_callback='minimal',
run_tree=False,
)
result = tqm.run(play)
print result
finally:
if tqm is not None:
tqm.cleanup()
示例12: main
def main():
host_list = ['localhost', 'www.example.com', 'www.google.com']
Options = namedtuple('Options', ['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'])
# initialize needed objects
variable_manager = VariableManager()
loader = DataLoader()
options = Options(connection='smart', module_path='/usr/share/ansible', 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)
passwords = dict()
# create inventory and pass to var manager
inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list=host_list)
variable_manager.set_inventory(inventory)
# create play with tasks
play_source = dict(
name="Ansible Play",
hosts=host_list,
gather_facts='no',
tasks=[dict(action=dict(module='command', args=dict(cmd='/usr/bin/uptime')))]
)
play = Play().load(play_source, variable_manager=variable_manager, loader=loader)
# actually run it
tqm = None
callback = ResultsCollector()
try:
tqm = TaskQueueManager(
inventory=inventory,
variable_manager=variable_manager,
loader=loader,
options=options,
passwords=passwords,
)
tqm._stdout_callback = callback
result = tqm.run(play)
finally:
if tqm is not None:
tqm.cleanup()
print("UP ***********")
for host, result in callback.host_ok.items():
print('{} >>> {}'.format(host, result._result['stdout']))
print("FAILED *******")
for host, result in callback.host_failed.items():
print('{} >>> {}'.format(host, result._result['msg']))
print("DOWN *********")
for host, result in callback.host_unreachable.items():
print('{} >>> {}'.format(host, result._result['msg']))
示例13: main
def main(args):
# Options definition
# Custom tuple to store playbook options
Options = namedtuple('Options', ['connection', 'module_path', 'forks',
'become', 'become_method', 'become_user',
'check'])
# Object initialization
variable_manager = VariableManager()
loader = DataLoader()
options = Options(connection='ssh',module_path='library',
forks=100, become=None, become_method=None,
become_user=None, check=False)
passwords = {}
# Dinamyc inventory
inventory = Inventory(loader=loader, variable_manager=variable_manager,
host_list=args)
# Inventory assignation
variable_manager.set_inventory(inventory)
# Play creation with tasks
play_source = dict(
name="Ansible Play",
hosts=args,
gather_facts='no',
tasks=[
dict(action=dict(module='shell', args='hostname -f'),
register='shell_out'),
dict(action=dict(module='debug',
args=dict(msg='{{shell_out.stdout}}')))
]
)
play = Play().load(play_source, variable_manager=variable_manager,
loader=loader)
# Running it
tqm = None
try:
tqm = TaskQueueManager(
inventory=inventory,
variable_manager=variable_manager,
loader=loader,
options=options,
passwords=passwords,
stdout_callback='default'
)
result = tqm.run(play)
finally:
if tqm is not None:
tqm.cleanup()
示例14: main
def main(argv=sys.argv[1:]):
Options = namedtuple('Options', ['connection', 'module_path', 'forks',
'become', 'become_method', 'become_user',
'check'])
# initialize needed objects
variable_manager = VariableManager()
loader = DataLoader()
options = Options(connection='local', module_path='/path/to/mymodules',
forks=100, become=None, become_method=None,
become_user=None,
check=False)
passwords = dict(vault_pass='secret')
# create inventory and pass to var manager
inventory = Inventory(loader=loader, variable_manager=variable_manager,
host_list='localhost')
variable_manager.set_inventory(inventory)
# create play with tasks
play_source = dict(name="Ansible Play",
hosts='localhost',
gather_facts='no',
tasks=[
dict(action=dict(module='shell',
args='uname -a'), register='shell_out'),
dict(action=dict(module='debug',
args=dict(msg='{{shell_out.stdout}}')))
]
)
play = Play().load(play_source, variable_manager=variable_manager,
loader=loader)
# actually run it
tqm = None
try:
tqm = TaskQueueManager(inventory=inventory,
variable_manager=variable_manager,
loader=loader,
options=options,
passwords=passwords,
stdout_callback='default',
)
result = tqm.run(play)
print result
finally:
if tqm is not None:
tqm.cleanup()
示例15: test_base
def test_base(self):
test_inv_dir = 'test/inventory'
for inv in os.listdir(test_inv_dir):
print "Processing ", inv
res = dynlxc.main(os.path.join(test_inv_dir, inv), '')
variable_manager = VariableManager()
loader = DataLoader()
self.mock_rv.communicate.return_value = [
json.dumps(res), 'mocked_err']
try:
inventory = Inventory(
loader=loader,
variable_manager=variable_manager,
host_list='inventory/dynlxc.py'
)
except Exception as err:
raise Exception("Inventory file {0} processing result '{1}' "
"failed with {2}".format(inv, res, err))
variable_manager.set_inventory(inventory)
play_source = dict(name="Ansible Play", hosts='localhost',
gather_facts='no')
playbook = os.path.abspath(os.path.join(test_inv_dir,
'../playbooks', inv))
if os.path.isfile(playbook):
with open(playbook) as fh:
real_playbook = yaml.load(fh)[0]
play_source.update(real_playbook)
play = Play().load(play_source, variable_manager=variable_manager,
loader=loader)
tqm = None
try:
tqm = TaskQueueManager(
inventory=inventory,
variable_manager=variable_manager,
loader=loader,
options=self.options,
passwords=None,
stdout_callback='default',
)
result = tqm.run(play)
assert result == 0, ("Ansible playbook exitcode "
"different from 0")
finally:
if tqm is not None:
tqm.cleanup()