python将ansible配置转为json格式实例代码
作者:鸿弟 时间:2023-11-03 02:32:51
python将ansible配置转为json格式实例代码
ansible的配置文件举例如下,这种配置文件不利于在前端的展现,因此,我们用一段简单的代码将ansible的配置文件转为json格式的:
[webserver]
192.168.204.70
192.168.204.71
[dbserver]
192.168.204.72
192.168.204.73
192.168.204.75
[proxy]
192.168.204.76
192.168.204.77
192.168.204.78
[test]
192.168.204.79
192.168.204.80
[haproxy]
192.168.205.82
192.168.204.83
用python将ansible配置转为json格式,python代码如下:
import ConfigParser
import json
dict_result = {}
cf = ConfigParser.ConfigParser(allow_no_value=True)
cf.read('/etc/ansible/hosts.test')
secs = cf.sections()
for sec in secs:
dict_result[sec] = cf.options(sec)
print json.dumps(dict_result)
转换结果如下(python版本使用2.7版本的):
/usr/local/python/bin/python /tmp/test.py
{"test": ["192.168.204.79", "192.168.204.80"], "haproxy": ["192.168.205.82", "192.168.204.83"], "webserver": ["192.168.204.70", "192.168.204.71"], "proxy": ["192.168.204.76", "192.168.204.77", "192.168.204.78"], "dbserver": ["192.168.204.72", "192.168.204.73", "192.168.204.75"]}
转换成json文件就方便在前端进行展示了,使用Flask提供json格式的接口如下:
#获取ansible分组
@app.route('/web_test/ansible')
def web_test_ansible():
dict_result = {}
cf = ConfigParser.ConfigParser(allow_no_value=True)
cf.read('/etc/ansible/hosts.test')
secs = cf.sections()
for sec in secs:
dict_result[sec] = cf.options(sec)
return json.dumps(dict_result)
然后前端代码(使用bootstrap treeview)如下:
<script src="/static/js/bootstrap-treeview.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$('#btn-get-hostgroup').click(function () {
getAllCheck = $('#hostgrouptree').treeview('getChecked');
for (var i = 0; i < getAllCheck.length; i++) {
console.log(getAllCheck[i].text);
}
});
$.ajax({
url: '/web_test/ansible',
type: 'GET',
success: function (data) {
result = JSON.parse(data);
nodes = [];
for (var hostgroup in result) {
var nodeshostgroup = [];
hosts = result[hostgroup];
for (var i = 0; i < hosts.length; i++) {
nodeshostgroup.push({text: hosts[i], selectable:false});
}
nodes.push({text: hostgroup, nodes: nodeshostgroup, selectable:false});
}
$('#hostgrouptree').treeview({data: [{text: 'all', nodes: nodes, selectable:false}], showCheckbox: true, showBorder:false});
}
});
});
</script>
{% endblock %}
{% block page_content %}
<div class="col-md-4" id="hostgrouptree">
</div>
<div class="col-md-6">
<button id="btn-get-hostgroup" type="button" class="btn btn-default">获取选中的组</button>
</div>
ansible的配置文件在前端的展示如下,获取ansible选中的组,利用ansible命令就可以实现一些简单的自动化操作了:
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
标签:python,ansible,json格式
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
SQL SERVER 与ACCESS、EXCEL的数据转换方法分享
2012-02-25 20:17:22
Pycharm中安装wordcloud等库失败问题及终端通过pip安装的Python库如何添加到Pycharm解释器中(推荐)
2023-06-19 17:29:49
![](https://img.aspxhome.com/file/2023/7/127317_0s.jpg)
在matplotlib中改变figure的布局和大小实例
2022-07-09 00:45:11
![](https://img.aspxhome.com/file/2023/8/121398_0s.jpg)
基于Python实现的微信好友数据分析
2021-07-26 20:27:54
![](https://img.aspxhome.com/file/2023/5/103505_0s.jpg)
连接Python程序与MySQL的教程
2024-01-26 15:02:57
php下intval()和(int)转换使用与区别
2023-06-11 14:09:54
python爬取指定微信公众号文章
2021-03-29 02:34:39
Python OpenCV特征检测之特征匹配方式详解
2021-07-20 00:51:58
![](https://img.aspxhome.com/file/2023/0/77580_0s.jpg)
javascript中的throttle和debounce浅析
2024-05-08 10:10:27
python 如何将带小数的浮点型字符串转换为整数
2021-04-24 01:35:51
如何使用FSO搜索硬盘文件
2007-09-27 12:59:00
PHP的mysqli_sqlstate()函数讲解
2023-06-08 14:30:30
![](https://img.aspxhome.com/file/2023/6/55456_0s.png)
MS SQL SERVER 数据库日志压缩方法与代码
2024-01-13 04:24:00
基于python实现把json数据转换成Excel表格
2021-02-20 05:22:04
php 多个变量指向同一个引用($b = &$a)用法分析
2023-11-02 23:28:11
基于django ManyToMany 使用的注意事项详解
2022-10-16 00:29:33
PyCharm如何配置SSH和SFTP连接远程服务器
2022-04-27 01:57:28
![](https://img.aspxhome.com/file/2023/8/131298_0s.png)
Golang运行报错找不到包:package xxx is not in GOROOT的解决过程
2024-05-05 09:32:25
![](https://img.aspxhome.com/file/2023/2/127472_0s.jpg)
vue项目使用高德地图的定位及关键字搜索功能的实例代码(踩坑经验)
2024-05-09 15:21:01
![](https://img.aspxhome.com/file/2023/5/126465_0s.png)
python 美化输出信息的实例
2022-04-15 09:53:54
![](https://img.aspxhome.com/file/2023/7/79757_0s.jpg)