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多线程爬虫简单示例
Mar 04 Python
python 接口返回的json字符串实例
Mar 27 Python
Python3实现的字典、列表和json对象互转功能示例
May 22 Python
PyTorch学习笔记之回归实战
May 28 Python
利用Python写一个爬妹子的爬虫
Jun 08 Python
解决python读取几千万行的大表内存问题
Jun 26 Python
JavaScript中的模拟事件和自定义事件实例分析
Jul 27 Python
Python正则表达式指南 推荐
Oct 09 Python
Python集中化管理平台Ansible介绍与YAML简介
Jun 12 Python
Python 使用指定的网卡发送HTTP请求的实例
Aug 21 Python
Python使用itchat模块实现群聊转发,自动回复功能示例
Aug 26 Python
Python FuzzyWuzzy实现模糊匹配
Apr 28 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
人工智能开始玩《星际争霸2》 你的操作跟得上吗?
2017/08/11 星际争霸
基于Zend的Captcha机制的应用
2013/05/02 PHP
PHP session文件独占锁引起阻塞问题解决方法
2015/05/12 PHP
PHP使用正则表达式获取微博中的话题和对象名
2015/07/18 PHP
PHP弹出对话框技巧详细解读
2015/09/26 PHP
不错的asp中显示新闻的功能
2006/10/13 Javascript
Extjs中使用extend(js继承) 的代码
2012/03/15 Javascript
jQuery选择器中含有空格的使用示例及注意事项
2013/08/25 Javascript
Flexigrid在IE下不显示数据的处理的解决方法
2013/10/24 Javascript
jQuery 1.9.1源码分析系列(十)事件系统之绑定事件
2015/11/19 Javascript
js创建对象的方法汇总
2016/01/07 Javascript
jQuery 局部div刷新和全局刷新方法总结
2016/10/05 Javascript
javascript鼠标跟随运动3种效果(眼球效果,苹果菜单,方向跟随)
2016/10/27 Javascript
简单谈谈gulp-changed插件
2017/02/21 Javascript
node使用promise替代回调函数
2018/05/07 Javascript
vue input 输入校验字母数字组合且长度小于30的实现代码
2018/05/16 Javascript
Vue实战教程之仿肯德基宅急送App
2019/07/19 Javascript
微信小程序实现图片压缩
2019/12/03 Javascript
js实现表格单列按字母排序
2020/08/12 Javascript
JavaScript实现点击切换功能
2021/01/27 Javascript
[02:48]DOTA2英雄基础教程 拉席克
2013/12/12 DOTA
[00:52]DOTA2齐天大圣预告片
2016/08/13 DOTA
Python的collections模块中namedtuple结构使用示例
2016/07/07 Python
基于pandas向csv添加新的行和列
2020/05/25 Python
Python利用命名空间解析XML文档
2020/08/10 Python
Auchan Direct波兰:欧尚在线杂货店
2016/10/19 全球购物
美国流行背包品牌:JanSport(杰斯伯)
2018/03/02 全球购物
耐克奥地利官网:Nike奥地利
2019/08/16 全球购物
init进程的作用
2012/04/12 面试题
网站创业计划书
2014/04/30 职场文书
生活小常识广播稿
2014/09/16 职场文书
乡镇群众路线专项整治方案
2014/11/03 职场文书
圣诞节开幕词
2015/01/29 职场文书
2015年化验室工作总结
2015/04/23 职场文书
国王的演讲观后感
2015/06/03 职场文书
2015年度学校应急管理工作总结
2015/10/22 职场文书