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. Office 2016、2019 与 Office 365 的区别

    点开观看更清晰:

  2. c++学习---vector

    vector存放类型不同,{}有些区别-: vector的size的返回类型: push_back的使用: 要防止缓冲区溢出,使用范围for语句:

  3. SpringCloud Stream 消息驱动

    1.什么是消息驱动 SpringCloud Stream消息驱动可以简化开发人员对消息中间件的使用复杂度,让系统开发人员更多尽力专注与核心业务逻辑的开发.SpringCloud Stream基于Spr ...

  4. Centos7安装gitlab11 学习笔记之备份恢复及邮箱配置

    一.备份 修改配置文件 vim /etc/gitlab/gitlab.rb 默认路径为 # gitlab_rails['backup_path'] = "/var/opt/gitlab/ba ...

  5. JS基础_函数作用域

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  6. css 关于"浮动边距加倍"及其解决方法-------解决方案是在这个div里面加上display:inline;

    写div代码的时候,经常发现明明宽度算得很准确,但是莫明其妙的会和计划的布局不太一样- -|||开始以为自己代码写的有问题,拼命找bug也一无所获,最后可能会稍微修改样式来达到想要的效果,但终究也是外 ...

  7. 关于cubic-bezier 贝塞尔曲线的简单了解

    在animation和transition两个属性中,cubic-bezier是控制变化的速度曲线,主要是生成速度曲线的函数 规定用法是: cubic-bezier(<x1>,<y1 ...

  8. Go Select使用

    原文:https://golangbot.com/pointers/ 作者:Nick Coghlan 译者:Noluye 什么是 select? select 语句用于在多个发送/接收信道操作中进行选 ...

  9. leetcode-62. Unique Paths · DP + vector

    题面 A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). ...

  10. flask的多个url对应同一个视图函数

    # -*- coding: utf-8 -*- from flask import Flask app = Flask(__name__) @app.route('/') def index(): r ...