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

 更新时间:2017年01月17日 16:11:42   作者:mindg.cn  
ansible是一个python package,是个完全的unpack and play软件,对客户端唯一的要求是有ssh有python,并且装了python-simplejson包,部署上简单到发指。下面这篇文章就给大家主要介绍了ansible作为python模块库使用的方法实例,需要的朋友可以参考借鉴。

前言

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使用metaclass实现Singleton模式的方法

    Python使用metaclass实现Singleton模式的方法

    这篇文章主要介绍了Python使用metaclass实现Singleton模式的方法,实例分析了Python基于metaclass实现单例模式的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-05-05
  • python安装whl文件的实战步骤

    python安装whl文件的实战步骤

    whl格式本质上是一个压缩包,里面包含了py文件,以及经过编译的pyd文件,下面这篇文章主要给大家介绍了关于python安装whl文件的相关资料,文中通过图文介绍的非常详细,需要的朋友可以参考下
    2022-07-07
  • Python基于内置库pytesseract实现图片验证码识别功能

    Python基于内置库pytesseract实现图片验证码识别功能

    这篇文章主要介绍了Python基于内置库pytesseract实现图片验证码识别功能,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-02-02
  • python创建堆的方法实例讲解

    python创建堆的方法实例讲解

    在本篇文章里小编给大家整理的是一篇关于python创建堆的方法实例讲解内容,有兴趣的朋友们可以学习下。
    2021-03-03
  • python枚举类型定义与使用讲解

    python枚举类型定义与使用讲解

    在python中枚举是一种类(Enum,IntEnum),存放在enum模块中。枚举类型可以给一组标签赋予一组特定的值,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-10-10
  • .img/.hdr格式转.nii格式的操作

    .img/.hdr格式转.nii格式的操作

    这篇文章主要介绍了.img/.hdr格式转.nii格式的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-07-07
  • python单例模式原理与创建方法实例分析

    python单例模式原理与创建方法实例分析

    这篇文章主要介绍了python单例模式原理与创建方法,结合实例形式分析了Python单例模式的概念、原理、定义、使用方法及相关操作注意事项,需要的朋友可以参考下
    2019-10-10
  • python如何实现代码检查

    python如何实现代码检查

    这篇文章主要介绍了python如何实现代码检查,如果代码不规范,乱七八糟的,被小伙伴打死或者熬夜找bug事小,影响项目进度或者破坏项目结构就完蛋了,需要的朋友可以参考下
    2019-06-06
  • python如何将txt坐标批量打印到原图上

    python如何将txt坐标批量打印到原图上

    这篇文章主要介绍了python如何将txt坐标批量打印到原图上的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-08-08
  • 11个Python3字典内置方法大全与示例汇总

    11个Python3字典内置方法大全与示例汇总

    这篇文章主要给大家介绍了11个Python3字典内置方法大全与示例的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用Python具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-05-05

最新评论