首页 > 编程语言 > 详细

动态inventory脚本的python实现

时间:2017-04-09 14:01:00      阅读:313      评论:0      收藏:0      [点我收藏+]
#!/usr/bin/env python
# -*- coding: utf-8 -*-
‘‘‘
基于python的动态inventory脚本实例
‘‘‘
import os
import sys
import argparse
try:
    import json
except ImportError:
    import simplejson as json
class ExampleInventory(object):

    def __init__(self):
        self.inventory = {}
        self.read_cli_args()
        #定义 --list 选项
        if self.args.list:
            self.inventory = self.example_inventory()
        elif self.args.host:
            self.inventory  = self.empty_inventory()
        else:
            self.inventory = self.empty_inventory()
        print json.dumps(self.inventroy);

    def eample_inventory(self):
        return {
            group:{
                 hosts:[0.0.0.0,1.1.0.1],
                 vars:{
                        ansible_ssh_user:test,
                        ansible_ssh_private_key_file:~/.test/insecure_private_key,
                        example_varialbe:value
                }
             },
              _meta:{
                   hostvars:{
                        0.0.0.10:{
                           host_specific_var:foo
                        }  ,
                        0.0.0.1: {
                            host_specific_var : bar  
                       } 
                  }
               }
        }    

      def empty_inventory(self):
            return {_meta:{hostvars:{}}}  

      def read_cli_args(self):
              parser = argparse.ArgumentParser()
              parser.add_argument(--list,action = store_true)
              parser.add_argument(--host,action = store)
              self.args = parser.parser_args()

ExampleInventory()







                    

 

动态inventory脚本的python实现

原文:http://www.cnblogs.com/jackchen001/p/6684244.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!