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框架Flask中使用百度云存储BCS实例
Feb 08 Python
python中argparse模块用法实例详解
Jun 03 Python
详解django中自定义标签和过滤器
Jul 03 Python
Python数据结构与算法之使用队列解决小猫钓鱼问题
Dec 14 Python
Python拼接字符串的7种方法总结
Nov 01 Python
Python Cookie 读取和保存方法
Dec 28 Python
利用python修改json文件的value方法
Dec 31 Python
Python连接字符串过程详解
Jan 06 Python
python Shapely使用指南详解
Feb 18 Python
Python pymsql模块的使用
Sep 07 Python
使用python如何删除同一文件夹下相似的图片
May 07 Python
Python 多线程处理任务实例
Nov 07 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 应用程序安全防范技术研究
2009/09/25 PHP
php中var_export与var_dump的区别分析
2010/08/21 PHP
php遍历目录与文件夹的多种方法详解
2013/11/14 PHP
PHP调用C#开发的dll类库方法
2014/07/28 PHP
关于Curl在Swoole协程中的解决方案详析
2019/09/12 PHP
js类 from qq
2006/11/13 Javascript
jquery focus(fn),blur(fn)方法实例代码
2011/12/16 Javascript
JavaScript对象之深度克隆介绍
2014/12/08 Javascript
node.js中的console.assert方法使用说明
2014/12/10 Javascript
果断收藏9个Javascript代码高亮脚本
2016/01/06 Javascript
ECharts仪表盘实例代码(附源码下载)
2016/02/18 Javascript
JS实现拖拽的方法分析
2016/12/20 Javascript
详解微信小程序审核不通过的解决方法
2018/01/17 Javascript
vue中$set的使用(结合在实际应用中遇到的坑)
2018/07/10 Javascript
如何在JS文件中获取Vue组件
2020/09/16 Javascript
Vue使用v-viewer实现图片预览
2020/10/21 Javascript
python zip文件 压缩
2008/12/24 Python
Python使用tablib生成excel文件的简单实现方法
2016/03/16 Python
答题辅助python代码实现
2018/01/16 Python
python用户评论标签匹配的解决方法
2018/05/31 Python
python学生信息管理系统(完整版)
2020/04/05 Python
Django objects的查询结果转化为json的三种方式的方法
2018/11/07 Python
用Python实现大文本文件切割的方法
2019/01/12 Python
python实现AES和RSA加解密的方法
2019/03/28 Python
pymysql 开启调试模式的实现
2019/09/24 Python
Python爬取腾讯视频评论的思路详解
2019/12/19 Python
Python3.7黑帽编程之病毒篇(基础篇)
2020/02/04 Python
Python3.9.1中使用match方法详解
2021/02/08 Python
ColourPop美国官网:卡拉泡泡,洛杉矶彩妆品牌
2019/04/28 全球购物
值传递还是引用传递
2015/02/08 面试题
材料化学应届生求职信
2013/10/09 职场文书
信访工作者先进事迹
2014/01/17 职场文书
服务生自我鉴定
2014/01/22 职场文书
html实现随机点名器的示例代码
2021/04/02 Javascript
利用Python判断你的密码难度等级
2021/06/02 Python
nginx配置虚拟主机的详细步骤
2021/07/21 Servers