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 Web框架Pylons中使用MongoDB的例子
Dec 03 Python
Python functools模块学习总结
May 09 Python
Python黑帽编程 3.4 跨越VLAN详解
Sep 28 Python
详解python调度框架APScheduler使用
Mar 28 Python
Python 3.x读写csv文件中数字的方法示例
Aug 29 Python
python实现键盘控制鼠标移动
Nov 27 Python
Python高斯消除矩阵
Jan 02 Python
如何在VSCode上轻松舒适的配置Python的方法步骤
Oct 28 Python
Python数据持久化存储实现方法分析
Dec 21 Python
pytorch中torch.max和Tensor.view函数用法详解
Jan 03 Python
Python中os模块功能与用法详解
Feb 26 Python
python字符串判断密码强弱
Mar 18 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下删除字符串中HTML标签的函数
2008/08/27 PHP
IP138 IP地址查询小偷实现代码
2010/02/15 PHP
PHP删除目录及目录下所有文件的方法详解
2013/06/06 PHP
利用中国天气预报接口实现简单天气预报
2014/01/20 PHP
PHP小教程之实现链表
2014/06/09 PHP
php获取远程文件大小
2015/10/20 PHP
PHP格式化显示时间date()函数代码
2018/10/03 PHP
PHP5.5基于mysqli连接MySQL数据库和读取数据操作实例详解
2019/02/16 PHP
网页自动刷新,不产生嗒嗒声的一个解决方法
2007/03/27 Javascript
window.location的重写及判断location是否被重写
2014/09/04 Javascript
js读取json的两种常用方法示例介绍
2014/10/19 Javascript
AngularJS入门心得之directive和controller通信过程
2016/01/25 Javascript
jQuery提示插件qTip2用法分析(支持ajax及多种样式)
2016/06/08 Javascript
jQuery实现表格文本框淡入更改值后淡出效果
2016/09/27 Javascript
js正则表达式注册页面表单验证
2016/10/11 Javascript
jQuery无刷新上传之uploadify简单代码
2017/01/17 Javascript
jQuery实现下拉菜单的实例代码
2017/06/19 jQuery
JQuery用$.ajax或$.getJSON跨域获取JSON数据的实现代码
2017/09/23 jQuery
Vue+Vux项目实践完整代码
2017/11/30 Javascript
[01:13:46]iG vs Winstrike 2018国际邀请赛小组赛BO2 第一场 8.16
2018/08/17 DOTA
python实现连接mongodb的方法
2015/05/08 Python
Python 专题五 列表基础知识(二维list排序、获取下标和处理txt文本实例)
2017/03/20 Python
详解python中Numpy的属性与创建矩阵
2018/09/10 Python
Python实现求两个数组交集的方法示例
2019/02/23 Python
pytorch+lstm实现的pos示例
2020/01/14 Python
python matplotlib实现将图例放在图外
2020/04/17 Python
django models里数据表插入数据id自增操作
2020/07/15 Python
继承时候类的执行顺序问题,一般都是选择题,问你将会打印出什么?
2015/11/18 面试题
大学生就业自荐信
2013/10/26 职场文书
家长学校实施方案
2014/03/15 职场文书
大学生评语大全
2014/04/18 职场文书
办公室班子四风问题对照检查材料
2014/10/04 职场文书
考试没考好检讨书(精选篇)
2014/11/16 职场文书
2015年药店工作总结
2015/04/20 职场文书
驳回起诉民事裁定书
2015/05/19 职场文书
纪律委员竞选稿
2015/11/19 职场文书