python将ansible配置转为json格式实例代码

yipeiwu_com5年前Python基础

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 调用PIL库失败的解决方法

今天学习Python的时候,需要安装一个第三方库,Python Imaging Library,是Python下面一个非常强大的处理图像的工具库,不过PIL目前只支持到Python2.7...

Python包,__init__.py功能与用法分析

Python包,__init__.py功能与用法分析

本文实例讲述了Python包,__init__.py功能与用法。分享给大家供大家参考,具体如下: 包: 为了组织好模块,将多个模块组合为一个包,所以包用于存放python模块...

Python音频操作工具PyAudio上手教程详解

Python音频操作工具PyAudio上手教程详解

​ 0.引子 当需要使用Python处理音频数据时,使用python读取与播放声音必不可少,下面介绍一个好用的处理音频PyAudio工具包。 PyAudio是Python开...

python 模拟银行转账功能过程详解

python 模拟银行转账功能过程详解

首先画出流程图,流程图与现实代码有出入,因为刚开始画流程图的时候,有些东西没考虑进去,后来写着写着就慢慢能想起来并实现了。 另有一点经验推荐给新手朋友,如果说碰到一个项目无从下手的话,...

python中的print()输出

1.普通的输出: print(str)#str是任意一个字符串,数字··· 2.格式化输出: print('1,2,%s,%d'%('asd',4)) 1,2,asd,4 与C语...