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入门教程
Feb 08 Python
Python中的左斜杠、右斜杠(正斜杠和反斜杠)
Aug 30 Python
python虚拟环境virtualenv的安装与使用
Sep 21 Python
十分钟利用Python制作属于你自己的个性logo
May 07 Python
Python3.5内置模块之random模块用法实例分析
Apr 26 Python
浅谈Django+Gunicorn+Nginx部署之路
Sep 11 Python
Python列表删除元素del、pop()和remove()的区别小结
Sep 11 Python
Python argparse模块应用实例解析
Nov 15 Python
TensorFlow加载模型时出错的解决方式
Feb 06 Python
Keras中的两种模型:Sequential和Model用法
Jun 27 Python
推荐技术人员一款Python开源库(造数据神器)
Jul 08 Python
pycharm debug 断点调试心得分享
Apr 16 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
全国FM电台频率大全 - 3 河北省
2020/03/11 无线电
php下HTTP Response中的Chunked编码实现方法
2008/11/19 PHP
PHP 字符串 小常识
2009/06/05 PHP
函数中使用require_once问题深入探讨 优雅的配置文件定义方法推荐
2014/07/02 PHP
7个鲜为人知却非常实用的PHP函数
2015/07/01 PHP
php微信公众平台开发之获取用户基本信息
2015/08/17 PHP
php获取开始与结束日期之间所有日期的方法
2016/11/29 PHP
PHP检查文件是否存在,不存在自动创建及读取文件内容操作示例
2020/01/23 PHP
javascript 复杂的嵌套环境中输出单引号和双引号
2009/05/26 Javascript
Javascript基础教程之switch语句
2015/01/18 Javascript
基于bootstrap插件实现autocomplete自动完成表单
2016/05/07 Javascript
jQuery获取attr()与prop()属性值的方法及区别介绍
2016/07/06 Javascript
完美解决jQuery符号$与其他javascript 库、框架冲突的问题
2016/08/09 Javascript
JS实现图片放大缩小的方法
2017/02/15 Javascript
获取url中用&隔开的参数实例(分享)
2017/05/28 Javascript
javascript实现二叉树的代码
2017/06/08 Javascript
webpack+vuex+axios 跨域请求数据的示例代码
2018/03/06 Javascript
微信小程序使用map组件实现路线规划功能示例
2019/01/22 Javascript
微信小程序 简易计算器实现代码实例
2019/09/02 Javascript
[01:05:41]EG vs Optic Supermajor 败者组 BO3 第二场 6.6
2018/06/07 DOTA
Python中使用scapy模拟数据包实现arp攻击、dns放大攻击例子
2014/10/23 Python
Python中字符串对齐方法介绍
2015/05/21 Python
Python实现自动上京东抢手机
2018/02/06 Python
django创建css文件夹的具体方法
2020/07/31 Python
Python self用法详解
2020/11/28 Python
HTML5引入的新数组TypedArray介绍
2012/12/24 HTML / CSS
沙特阿拉伯家用电器和电子产品购物网站:Sheta and Saif
2020/04/03 全球购物
国际贸易专业个人求职信格式
2014/02/02 职场文书
教师党员个人整改措施
2014/10/27 职场文书
2014年宣传思想工作总结
2014/12/10 职场文书
中学推普周活动总结
2015/05/07 职场文书
横空出世观后感
2015/06/09 职场文书
聘任通知书
2015/09/21 职场文书
一年级下册数学教学反思
2016/02/16 职场文书
springboot集成springCloud中gateway时启动报错的解决
2021/07/16 Java/Android
Mysql的Table doesn't exist问题及解决
2022/12/24 MySQL