ansible作为python模块库使用的方法实例


Posted in Python onJanuary 17, 2017

前言

ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能。ansible是基于模块工作的,本身没有批量部署的能力。真正具有批量部署的是ansible所运行的模块,ansible只是提供一种框架。

主要包括:

      (1)、连接插件connection plugins:负责和被监控端实现通信;

      (2)、host inventory:指定操作的主机,是一个配置文件里面定义监控的主机;

      (3)、各种模块核心模块、command模块、自定义模块;

      (4)、借助于插件完成记录日志邮件等功能;

      (5)、playbook:剧本执行多个任务时,非必需可以让节点一次性运行多个任务。

Asible是运维工具中算是非常好的利器,我个人比较喜欢,可以根据需求灵活配置yml文件来实现不同的业务需求,因为不需要安装客户端,上手还是非常容易的,在某些情况下你可能需要将ansible作为python的一个库组件写入到自己的脚本中,今天的脚本脚本就将展示下ansible如何跟python脚本结合,也就是如何在python脚本中使用ansible,我们逐步展开。

先看第一个例子:

#!/usr/bin/python 
import ansible.runner
import ansible.playbook
import ansible.inventory
from ansible import callbacks
from ansible import utils
import json
 
# the fastest way to set up the inventory
 
# hosts list
hosts = ["10.11.12.66"]
# set up the inventory, if no group is defined then 'all' group is used by default
example_inventory = ansible.inventory.Inventory(hosts)
 
pm = ansible.runner.Runner(
 module_name = 'command',
 module_args = 'uname -a',
 timeout = 5,
 inventory = example_inventory,
 subset = 'all' # name of the hosts group 
 )
 
out = pm.run()
 
print json.dumps(out, sort_keys=True, indent=4, separators=(',', ': '))

这个例子展示我们如何在python脚本中运行如何通过ansible运行系统命令,我们接下来看第二个例子,跟我们的yml文件对接。

简单的yml文件内容如下:

- hosts: sample_group_name
 tasks:
 - name: just an uname
 command: uname -a

调用playbook的python脚本如下:

#!/usr/bin/python 
import ansible.runner
import ansible.playbook
import ansible.inventory
from ansible import callbacks
from ansible import utils
import json
 
### setting up the inventory
 
## first of all, set up a host (or more)
example_host = ansible.inventory.host.Host(
 name = '10.11.12.66',
 port = 22
 )
# with its variables to modify the playbook
example_host.set_variable( 'var', 'foo')
 
## secondly set up the group where the host(s) has to be added
example_group = ansible.inventory.group.Group(
 name = 'sample_group_name'
 )
example_group.add_host(example_host)
 
## the last step is set up the invetory itself
example_inventory = ansible.inventory.Inventory()
example_inventory.add_group(example_group)
example_inventory.subset('sample_group_name')
 
# setting callbacks
stats = callbacks.AggregateStats()
playbook_cb = callbacks.PlaybookCallbacks(verbose=utils.VERBOSITY)
runner_cb = callbacks.PlaybookRunnerCallbacks(stats, verbose=utils.VERBOSITY)
 
# creating the playbook instance to run, based on "test.yml" file
pb = ansible.playbook.PlayBook(
 playbook = "test.yml",
 stats = stats,
 callbacks = playbook_cb,
 runner_callbacks = runner_cb,
 inventory = example_inventory,
 check=True
 )
 
# running the playbook
pr = pb.run() 
 
# print the summary of results for each host
print json.dumps(pr, sort_keys=True, indent=4, separators=(',', ': '))

总结

以上就是为大家展示的2个小例子希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流。

Python 相关文章推荐
简单介绍Python的Django框架的dj-scaffold项目
May 30 Python
Django ORM框架的定时任务如何使用详解
Oct 19 Python
python并发和异步编程实例
Nov 15 Python
python3 实现验证码图片切割的方法
Dec 07 Python
Python发送邮件测试报告操作实例详解
Dec 08 Python
python实现彩色图转换成灰度图
Jan 15 Python
python 动态迁移solr数据过程解析
Sep 04 Python
python网络爬虫 CrawlSpider使用详解
Sep 27 Python
动态设置django的model field的默认值操作步骤
Mar 30 Python
解决启动django,浏览器显示“服务器拒绝访问”的问题
May 13 Python
Python+Kepler.gl实现时间轮播地图过程解析
Jul 20 Python
Python趣味入门教程之循环语句while
Aug 26 Python
python 基础教程之Map使用方法
Jan 17 #Python
Python获取某一天是星期几的方法示例
Jan 17 #Python
Python正则表达式匹配中文用法示例
Jan 17 #Python
python下如何查询CS反恐精英的服务器信息
Jan 17 #Python
python基础教程之匿名函数lambda
Jan 17 #Python
python基础教程之Filter使用方法
Jan 17 #Python
python正则分析nginx的访问日志
Jan 17 #Python
You might like
PHP curl 获取响应的状态码的方法
2014/01/13 PHP
Smarty使用自定义资源的方法
2015/08/08 PHP
PHP仿qq空间或朋友圈发布动态、评论动态、回复评论、删除动态或评论的功能(上)
2017/05/26 PHP
Thinkphp 框架扩展之类库扩展操作详解
2020/04/23 PHP
DOM Scripting中的图片切换[兼容Firefox]
2010/06/12 Javascript
关于hashchangebroker和statehashable的补充文档
2011/08/08 Javascript
extjs 3.31 TreeGrid实现静态页面加载json到TreeGrid里面
2013/04/02 Javascript
jquery中get,post和ajax方法的使用小结
2014/02/04 Javascript
JavaScript实现的encode64加密算法实例分析
2015/04/15 Javascript
适用于javascript开发者的Processing.js入门教程
2016/02/24 Javascript
jQuery简单实现仿京东分类导航层效果
2016/06/07 Javascript
jQuery通过ajax请求php遍历json数组到table中的代码(推荐)
2016/06/12 Javascript
详解js产生对象的3种基本方式(工厂模式,构造函数模式,原型模式)
2017/01/09 Javascript
使用JS和canvas实现gif动图的停止和播放代码
2017/09/01 Javascript
Angular 4.x+Ionic3踩坑之Ionic 3.x界面传值详解
2018/03/13 Javascript
node.js遍历目录的方法示例
2018/08/01 Javascript
对node通过fs模块判断文件是否是文件夹的实例讲解
2019/06/10 Javascript
Python实现生成简单的Makefile文件代码示例
2015/03/10 Python
python flask 多对多表查询功能
2017/06/25 Python
浅析python打包工具distutils、setuptools
2018/04/20 Python
Python 求数组局部最大值的实例
2019/11/26 Python
matplotlib.pyplot画图并导出保存的实例
2019/12/07 Python
浅谈pytorch、cuda、python的版本对齐问题
2020/01/15 Python
Python用5行代码实现批量抠图的示例代码
2020/04/14 Python
Python存储读取HDF5文件代码解析
2020/11/25 Python
JD Sports德国官网:英国领先的运动鞋和运动服饰零售商
2018/02/26 全球购物
Ben Sherman官方网站:英国男装品牌
2019/10/22 全球购物
Douglas意大利官网:购买香水和化妆品
2020/05/27 全球购物
四年级数学教学反思
2014/02/02 职场文书
幼儿园父亲节活动方案
2014/03/11 职场文书
简单的项目建议书模板
2014/03/12 职场文书
2014年大学生党员自我评议
2014/09/22 职场文书
局领导领导班子四风对照检查材料
2014/09/27 职场文书
社区活动总结范文
2015/05/07 职场文书
小人国观后感
2015/06/11 职场文书
MySQL实现用逗号进行拼接、以逗号进行分割
2022/12/24 MySQL