python执行ad_hoc
2024-09-01 13:37:39
import shutil
from collections import namedtuple
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
import ansible.constants as C def ad_hoc(inventory_fname, hosts, module, args):
Options = namedtuple('Options', ['connection', 'module_path', 'forks', 'become', 'become_method', 'become_user', 'check', 'diff'])
options = Options(connection='ssh', module_path=[''], forks=10, become=None, become_method=None, become_user=None, check=False, diff=False)
loader = DataLoader()
passwords = dict()
inventory = InventoryManager(loader=loader, sources=inventory_fname)
variable_manager = VariableManager(loader=loader, inventory=inventory)
play_source=dict(
name="my ansible play",
hosts=hosts,
gather_facts='no',
tasks=[
dict(action=dict(module=module, args=args), 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,
options=options,
passwords=passwords,
)
result = tqm.run(play)
finally:
if tqm is not None:
tqm.cleanup() shutil.rmtree(C.DEFAULT_LOCAL_TMP, True) if __name__ == '__main__':
inventory_fname = ['myansible/hosts']
hosts = "webservers"
module = "shell"
args = "ls -ld /home"
ad_hoc(inventory_fname, hosts, module, args)
ad_hoc(inventory_fname, 'dbservers', module, 'mkdir /tmp/mydemo')
最新文章
- Spring+struts2的基础上继续加hibernate3的jar包
- net-snmp的dateandtime数据类型
- mongodb 连接和操作
- 如何修改SVN已提交项目的message log
- interrupt ,interrupted 和 isInterrupted
- 字符设备驱动、平台设备驱动、设备驱动模型、sysfs的比较和关联
- android99 拍照摄像
- Painting The Wall 期望DP Codeforces 398_B
- Missing iOS Distribution signing identity问题解决
- 处理IIS报“由于 Web 服务器上的“ISAPI 和 CGI 限制”列表设置,无法提供您请求的页面”
- java 访问 mysql 数据库的字符集设置
- poj3254(状压dp)
- 【Android基础】eclipse常用快捷键
- 模拟。。。 Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) C
- MySQL错误2003:Can't connect to MySQL server (10060)
- gorm的日志模块源码解析
- Java学习之Java接口回调理解
- 使用scrapy中xpath选择器的一个坑点
- 【NLP】大白话讲解word2vec到底在做些什么
- Big Number-Asia 2002, Dhaka (Bengal) (计算位数)题解