ansible2.8 官网api问题, 无输出
问题描述
ansible2.8 官网提供的代码,只能连接本机,
我把 context.CLIARGS = ImmutableDict(connection='local',....)
修改为 context.CLIARGS = ImmutableDict(connection='ssh',....)
准备远程连接的时候,执行py ,没有任何结果输出,也不报错。
问题出现的平台版本及自己尝试过哪些方法
ansible2.8
相关代码
import json
import shutil
from ansible.context import CLIARGS
from ansible.module_utils.common.collections import ImmutableDict
from ansible.parsing.dataloader import DataLoader
from ansible.vars.manager import VariableManager
from ansible.inventory.manager import InventoryManager
from ansible.playbook.play import Play
from ansible.executor.task_queue_manager import TaskQueueManager
from ansible.plugins.callback import CallbackBase
from ansible import context
import ansible.constants as C
class ResultCallback(CallbackBase):
"""A sample callback plugin used for performing an action as results come in
If you want to collect all results into a single object for processing at
the end of the execution, look into utilizing the ``json`` callback plugin
or writing your own custom callback plugin
"""
def v2_runner_on_ok(self, result, **kwargs):
"""Print a json representation of the result
This method could store the result in an instance attribute for retrieval later
"""
host = result._host
print(json.dumps({host.name: result._result}, indent=4))
context.CLIARGS = ImmutableDict(connection='ssh',module_path=['/home/zhuchao/anaconda/lib/python3.6/site-packages/ansible-2.8.2-py3.6.egg/ansible/modules'], forks=10, become=None, become_method=None, become_user=None, check=False, diff=False)
loader = DataLoader() # Takes care of finding and reading yaml, json and ini files
passwords = dict(vault_pass='123456')
results_callback = ResultCallback()
inventory = InventoryManager(loader=loader, sources='/etc/ansible/hosts')
variable_manager = VariableManager(loader=loader, inventory=inventory)
play_source = dict(
name = "Ansible Play",
hosts = '10.25.80.75',
gather_facts = 'no',
tasks = [
dict(action=dict(module='shell', args='whoami'), 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)
tqm = None
try:
tqm = TaskQueueManager(
inventory=inventory,
variable_manager=variable_manager,
loader=loader,
passwords=passwords,
stdout_callback=results_callback, # Use our custom callback instead of the ``default`` callback plugin, which prints to stdout
)
result = tqm.run(play)
finally:
if tqm is not None:
tqm.cleanup()
shutil.rmtree(C.DEFAULT_LOCAL_TMP, True)
你期待的结果是什么?实际看到的错误信息又是什么?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
因为它不叫 SSH了
兄弟你的问题解决了吗?我也遇到同样的问题求解