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 ...
随机推荐
- PHP之即点即改
html: <td data-hide="1200" class="px12" id ="<?php echo $v['g_id'];?& ...
- ThinkPHP读取配置信息
use think\Config; dump(Config::get()); // 或者 dump(config());示例:dump(Config::get('database.database') ...
- 【数据结构 Python & C++】顺序表
用C++ 和 Python实现顺序表的简单操作 C++代码 // Date:2019.7.31 // Author:Yushow Jue #include<iostream> using ...
- go变量的定义并赋值
变量在定义时没有明确的初始化时会赋值为_零值_. 零值是: 数值类型为 `0`, 布尔类型为 `false`, 字符串为 `""`(空字符串). package main impo ...
- 【计算几何】The Queen’s Super-circular Patio
The Queen’s Super-circular Patio 题目描述 The queen wishes to build a patio paved with of a circular cen ...
- shell习题第24题:杀进程
[题目要求] 一台机器负载高,top查看到有很多sh的进程,然后top -c查看可以看到对应的进程命令是sh -c /bin/clear.sh 经分析后发现是因为该脚本执行时间太长,导致后续执行时,上 ...
- Nginx学习笔记(三):Nginx 请求处理
Request Nginx 中的 ngx_http_request_t 是对一个 http 请求的封装: 一个 http 请求包含:请求行.请求头.请求体,响应行.响应头.响应体 Nginx 处理请求 ...
- hadoop2.7.7 分布式集群安装与配置
环境准备 服务器四台: 系统信息 角色 hostname IP地址 Centos7.4 Mster hadoop-master-001 10.0.15.100 Centos7.4 Slave hado ...
- 15-MySQL DBA笔记-运维管理
第15章 运维管理 随着各种技术的快速发展,现今的DBA可以比以前的DBA维护多得多的数据库实例.DBA已经越来越像一个资源的管理者,而不是简单的操作步骤执行人.本章将为读者介绍规模化运维之道.首先, ...
- python 切换虚拟环境
每次电脑重启后,都要切入虚拟环境,命令总是忘记.如果使用IDE,可以指定interpeter方便的切换. 首先conda info --env 查看当前有几个环境 激活/禁用环境 source ac ...