ad_hoc详解
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详解的更多相关文章
- Linq之旅:Linq入门详解(Linq to Objects)
示例代码下载:Linq之旅:Linq入门详解(Linq to Objects) 本博文详细介绍 .NET 3.5 中引入的重要功能:Language Integrated Query(LINQ,语言集 ...
- 架构设计:远程调用服务架构设计及zookeeper技术详解(下篇)
一.下篇开头的废话 终于开写下篇了,这也是我写远程调用框架的第三篇文章,前两篇都被博客园作为[编辑推荐]的文章,很兴奋哦,嘿嘿~~~~,本人是个很臭美的人,一定得要截图为证: 今天是2014年的第一天 ...
- EntityFramework Core 1.1 Add、Attach、Update、Remove方法如何高效使用详解
前言 我比较喜欢安静,大概和我喜欢研究和琢磨技术原因相关吧,刚好到了元旦节,这几天可以好好学习下EF Core,同时在项目当中用到EF Core,借此机会给予比较深入的理解,这里我们只讲解和EF 6. ...
- Java 字符串格式化详解
Java 字符串格式化详解 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 文中如有纰漏,欢迎大家留言指出. 在 Java 的 String 类中,可以使用 format() 方法 ...
- Android Notification 详解(一)——基本操作
Android Notification 详解(一)--基本操作 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 源码:AndroidDemo/Notification 文中如有纰 ...
- Android Notification 详解——基本操作
Android Notification 详解 版权声明:本文为博主原创文章,未经博主允许不得转载. 前几天项目中有用到 Android 通知相关的内容,索性把 Android Notificatio ...
- Git初探--笔记整理和Git命令详解
几个重要的概念 首先先明确几个概念: WorkPlace : 工作区 Index: 暂存区 Repository: 本地仓库/版本库 Remote: 远程仓库 当在Remote(如Github)上面c ...
- Drawable实战解析:Android XML shape 标签使用详解(apk瘦身,减少内存好帮手)
Android XML shape 标签使用详解 一个android开发者肯定懂得使用 xml 定义一个 Drawable,比如定义一个 rect 或者 circle 作为一个 View 的背景. ...
- Node.js npm 详解
一.npm简介 安装npm请阅读我之前的文章Hello Node中npm安装那一部分,不过只介绍了linux平台,如果是其它平台,有前辈写了更加详细的介绍. npm的全称:Node Package M ...
随机推荐
- API参考
http://www.yfvb.com/help/win32sdk/index.htm?page=html/13dsy.g.htm
- STM32之ADC实例(基于DMA方式)
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/zouleideboke/article/details/75112224 ADC简介: ADC(An ...
- 【win10】 ffmpeg的安装
安装肯定要先下载,官方下载地址:http://www.ffmpeg.org/download.html 然后会进入这个页面. 然后根据你的操作系统选择 根据自己系统选择,我的系统是64位的所以下载的是 ...
- js 监听键盘的enter键
// js 版本 window.onload=function(){ document.onkeydown=function(ev){ var event=ev ||event if(event.ke ...
- Luogu4081 USACO17DEC Standing Out from the Herd(广义后缀自动机)
建出广义SAM,通过parent树对每个节点求出其是否仅被一个子串包含及被哪个包含. 写了无数个sam板子题一点意思都没啊 #include<bits/stdc++.h> using na ...
- SpinWait
其实SpinWait的code 非常简单,以前看过很多遍,但是从来都没有整理过,整理也是再次学习吧. 我们先看看SpinWait的一些评论或者注意点吧:如果等待某个条件满足需要的时间很短,而且不希望发 ...
- Html5+Mui前端框架,开发记录(三):七牛云 上传图片
1.Html界面: <div id="container"> <label>凭证:</label> <div id="uploa ...
- restTemplate源码解析(二)restTemplate的核心逻辑
所有文章 https://www.cnblogs.com/lay2017/p/11740855.html 正文 上一篇文章中,我们构造了一个RestTemplate的Bean实例对象.本文将主要了解一 ...
- KVM之磁盘管理工具qemu-img小结
基本语法: qemu-img command [command options] 主要参数: info : 查看镜像的信息: create: 创建镜像: check: 检查镜像: convert: 转 ...
- Oracle学习笔记——Linux下开启Oracle
1.开启数据库 sqlplus / as sysdba startup 2.启动监听:lsnrctl start; 查看监听状态:lsnrctl status; 3.登入数据库 Linux 设置 ...