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')

最新文章

  1. Spring+struts2的基础上继续加hibernate3的jar包
  2. net-snmp的dateandtime数据类型
  3. mongodb 连接和操作
  4. 如何修改SVN已提交项目的message log
  5. interrupt ,interrupted 和 isInterrupted
  6. 字符设备驱动、平台设备驱动、设备驱动模型、sysfs的比较和关联
  7. android99 拍照摄像
  8. Painting The Wall 期望DP Codeforces 398_B
  9. Missing iOS Distribution signing identity问题解决
  10. 处理IIS报“由于 Web 服务器上的“ISAPI 和 CGI 限制”列表设置,无法提供您请求的页面”
  11. java 访问 mysql 数据库的字符集设置
  12. poj3254(状压dp)
  13. 【Android基础】eclipse常用快捷键
  14. 模拟。。。 Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) C
  15. MySQL错误2003:Can't connect to MySQL server (10060)
  16. gorm的日志模块源码解析
  17. Java学习之Java接口回调理解
  18. 使用scrapy中xpath选择器的一个坑点
  19. 【NLP】大白话讲解word2vec到底在做些什么
  20. Big Number-Asia 2002, Dhaka (Bengal) (计算位数)题解

热门文章

  1. 关于php发送邮件(PHPmailer)的傻瓜式操作
  2. JDBC(Java项目使用Oracle数据库)
  3. 关于scanf()读取与返回值和回车键的问题
  4. java 字符串处理的
  5. 怎样获取当前对象的原型对象prototype
  6. 字符串的简单操作----记录次数 hdu2617
  7. arcgis for android100.x 禁止地图旋转
  8. 恺撒密码 B
  9. POJ1083(Moving Tables)--简单模拟
  10. Array + two points leetcode.15-3Sum