ansible作为python模块库使用的方法实例
作者:mindg.cn 时间:2022-07-15 08:01:02
前言
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=(',', ': '))
来源:https://www.mindg.cn/?p=1650
标签:ansible,python,模块库
0
投稿
猜你喜欢
python argparse的使用步骤(全网最全)
2023-06-14 01:13:45
Oracle 当前用户下所有表的记录总数
2009-07-14 21:34:00
Python制作动态词频条形图的全过程
2021-04-25 11:14:52
Bootstrap编写一个兼容主流浏览器的受众门户式风格页面
2024-05-02 17:32:01
深入理解Python虚拟机中字典(dict)的实现原理及源码剖析
2022-03-10 20:57:06
python+opencv实现论文插图局部放大并拼接效果
2023-12-07 17:29:12
OpenCV实现机器人对物体进行移动跟随的方法实例
2023-04-20 08:50:49
table 行转列的sql详解
2024-01-27 00:44:57
Django模板标签{% for %}循环,获取制定条数据实例
2023-02-25 02:27:59
python unittest实现api自动化测试
2022-12-05 09:52:37
Python使用pptx实现复制页面到其他PPT中
2021-07-13 01:28:16
asp查询xml的代码实现无刷新 模糊查询
2008-04-30 15:39:00
python基础之局部变量和全局变量
2021-10-23 06:50:31
一个jquery日期选取插件源码
2009-12-23 19:15:00
python游戏库pygame经典教程(推荐!)
2022-10-02 06:26:11
Python中的三目(元)运算符详解
2023-12-06 01:20:49
一文搞懂Python中subprocess模块的使用
2023-12-05 16:35:05
SQL解决未能删除约束问题drop constraint
2024-01-24 20:08:09
Python3爬虫mitmproxy的安装步骤
2022-08-26 12:23:26
Python中map,reduce,filter和sorted函数的使用方法
2023-04-04 14:01:48