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 # since API is constructed for CLI it expects certain options to always be set, named tuple 'fakes' the args parsing options object
# 这里只是定义执行ansible时的选项,参见ansible --help
# connection: 连接的类型,有local(本机执行)/ssh(通过ssh执行)/smart(智能选择)
# module_path: 自定义的模块路径
# forks: 并行执行程序的进程数
# become相关:建议使用普通用户连接远程服务器,执行操作的时候再切换成root,become指定切换成哪个用户,切换的方法是什么
# check: 不真正的执行命令,而是预言将会发生什么
# diff: 当改变文件或模板时,指出修改了什么
Options = namedtuple('Options', ['connection', 'module_path', 'forks', 'become', 'become_method', 'become_user', 'check', 'diff'])
options = Options(connection='ssh', module_path=['/to/mymodules'], forks=10, become=None, become_method=None, become_user=None, check=False, diff=False) # initialize needed objects
# loader: 负责查找和读取yaml、json和ini文件,能够在文件中取出正确的数据
loader = DataLoader() # Takes care of finding and reading yaml, json and ini files
# 存储各种各样的密码
passwords = dict() # create inventory, use path to host config file as source or hosts in a comma separated string
# 定义主机清单,可以采用两种方式:
# sources='用逗号分开的所有的主机'
# sources=[主机清单文件]
# inventory = InventoryManager(loader=loader, sources='localhost,')
inventory = InventoryManager(loader=loader, sources=['myansible/hosts']) # variable manager takes care of merging all the different sources to give you a unifed view of variables available in each context
# 定义变量管理器
variable_manager = VariableManager(loader=loader, inventory=inventory) # create datastructure that represents our play, including tasks, this is basically what our YAML loader does internally.
# 创建要执行的play源
play_source=dict(
name="my ansible play",
hosts='webservers', # 指定在哪些主机上执行任务
gather_facts='no', # 不收集主机的信息
tasks=[ # 定义在主机上执行的任务
dict(action=dict(module='shell', args='ls -ld /home'), register='shell_out'),
dict(action=dict(module='debug', args=dict(msg='{{shell_out.stdout}}')))
]
) # Create play object, playbook objects use .load instead of init or new methods,
# this will also automatically create the task objects from the info provided in play_source
# 创建play实例
play = Play().load(play_source, variable_manager=variable_manager, loader=loader) # Run it - instantiate task queue manager, which takes care of forking and setting up all objects to iterate over host list and tasks
tqm = None
try:
tqm = TaskQueueManager(
inventory=inventory,
variable_manager=variable_manager,
loader=loader,
options=options,
passwords=passwords,
)
result = tqm.run(play) # most interesting data for a play is actually sent to the callback's methods
finally:
# we always need to cleanup child procs and the structres we use to communicate with them
# 清理任务
if tqm is not None:
tqm.cleanup() # Remove ansible tmpdir
# 删除临时目录
shutil.rmtree(C.DEFAULT_LOCAL_TMP, True)

ad_hoc详解的更多相关文章

  1. Linq之旅:Linq入门详解(Linq to Objects)

    示例代码下载:Linq之旅:Linq入门详解(Linq to Objects) 本博文详细介绍 .NET 3.5 中引入的重要功能:Language Integrated Query(LINQ,语言集 ...

  2. 架构设计:远程调用服务架构设计及zookeeper技术详解(下篇)

    一.下篇开头的废话 终于开写下篇了,这也是我写远程调用框架的第三篇文章,前两篇都被博客园作为[编辑推荐]的文章,很兴奋哦,嘿嘿~~~~,本人是个很臭美的人,一定得要截图为证: 今天是2014年的第一天 ...

  3. EntityFramework Core 1.1 Add、Attach、Update、Remove方法如何高效使用详解

    前言 我比较喜欢安静,大概和我喜欢研究和琢磨技术原因相关吧,刚好到了元旦节,这几天可以好好学习下EF Core,同时在项目当中用到EF Core,借此机会给予比较深入的理解,这里我们只讲解和EF 6. ...

  4. Java 字符串格式化详解

    Java 字符串格式化详解 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 文中如有纰漏,欢迎大家留言指出. 在 Java 的 String 类中,可以使用 format() 方法 ...

  5. Android Notification 详解(一)——基本操作

    Android Notification 详解(一)--基本操作 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 源码:AndroidDemo/Notification 文中如有纰 ...

  6. Android Notification 详解——基本操作

    Android Notification 详解 版权声明:本文为博主原创文章,未经博主允许不得转载. 前几天项目中有用到 Android 通知相关的内容,索性把 Android Notificatio ...

  7. Git初探--笔记整理和Git命令详解

    几个重要的概念 首先先明确几个概念: WorkPlace : 工作区 Index: 暂存区 Repository: 本地仓库/版本库 Remote: 远程仓库 当在Remote(如Github)上面c ...

  8. Drawable实战解析:Android XML shape 标签使用详解(apk瘦身,减少内存好帮手)

    Android XML shape 标签使用详解   一个android开发者肯定懂得使用 xml 定义一个 Drawable,比如定义一个 rect 或者 circle 作为一个 View 的背景. ...

  9. Node.js npm 详解

    一.npm简介 安装npm请阅读我之前的文章Hello Node中npm安装那一部分,不过只介绍了linux平台,如果是其它平台,有前辈写了更加详细的介绍. npm的全称:Node Package M ...

随机推荐

  1. Linux (x86) Exploit 开发系列教程之一(典型的基于堆栈的缓冲区溢出)

    (1)漏洞代码 //vuln.c #include <stdio.h> #include <string.h> int main(int argc, char* argv[]) ...

  2. 1187: 零起点学算法94——今年暑假不AC(Java)

    1187:零起点学算法94--今年暑假不AC Time Limit: 1 Sec Memory Limit: 32 MB 64bit IO Format: %lld Description " ...

  3. 【计算几何】Water Testing

    Water Testing 题目描述 You just bought a large piece of agricultural land, but you noticed that – accord ...

  4. 2019牛客多校四 E. triples II (容斥)

    大意: 给定$n,a$, 求$n$个$3$的倍数, $or$和为$a$的方案数. 简单容斥题 可以求出$f_{x,y}$表示所有$3$的倍数中, 奇数位不超过$x$个$1$, 偶数位不超过$y$个$1 ...

  5. java.lang.ClassCastException: com.sun.proxy.$Proxy4 cannot be cast

    解决方案 在配置文件中配置proxy-target-class="true" <aop:aspectj-autoproxy proxy-target-class=" ...

  6. (十)mybatis之缓存

    一.缓存的意义 将用户经常查询的数据放在缓存(内存)中,用户去查询数据就不用从磁盘上(关系型数据库数据文件)去查询,从缓存中进行查询,从而提高查询效率,解决了高并发系统的性能问题. 二.mybatis ...

  7. SQL Server系统函数:系统信息函数

    原文:SQL Server系统函数:系统信息函数 1.会话id,服务器信息.用户信息 select @@SPID, --返回当前连接的会话ID:SPID @@servername, --SQL Ser ...

  8. Zookeeper 安装及集群配置注意点

    Zookeeper在ubuntu下安装及集群搭建,关于集群搭建,网上很多文章 可以参考:https://www.ibm.com/developerworks/cn/opensource/os-cn-z ...

  9. TypeScript入门八:TypeScript的命名空间

    初识命名空间(namespace指令) 命名空间与文件拆分 多重命名空间与三斜杠指令引入依赖文件 一.初识命名空间(namespace指令) TypeScript的命名空间可以说就是ES6的模块化,其 ...

  10. javamail "535 5.7.3 Authentication unsuccessful" 问题排查

    有一家odm的服务器用Javamail发邮件的时候报错  Authentication unsuccessful  其他的有些又是正常的 网上查了一下解决方法如下 JavaMailSenderImpl ...