예제 #1
0
 def result_fact(self, host, result):
     facts = result.get('ansible_facts', {})
     arch = facts.get('ansible_architecture', '')
     mem = facts.get('ansible_memtotal_mb', 0)
     cpu = facts.get('ansible_processor_vcpus', 0)
     os = facts.get('ansible_os_family', '')
     ip = facts.get('ansible_default_ipv4', {}).get('address', '')
     name = facts.get('ansible_nodename', host)
     Host.create_or_replace(name, ip, arch, os, mem, cpu)
예제 #2
0
파일: test.py 프로젝트: quanloveshui/cmdb
class Command(BaseCommand):
    def handle(self, *args, **options):
        a = '11'

    arch = "x86_64"
    mem = "100"
    cpu = "24"
    os = "Centos"
    ip = "127.0.0.1"
    p_ip = "127.0.0.1"
    name = "test"
    Host.create_or_replace(name, ip, arch, os, mem, cpu, p_ip)
예제 #3
0
    def handle(self, *args, **options):
        Options = namedtuple('Options', 
                 ['connection', 'module_path', 'forks', 'become', 'become_user', 'become_method', 'check', 'diff'])
        options = Options(connection='smart', 
                  module_path=[], forks=10, become=None, become_user=None,  become_method=None, check=False, diff=False)


        callback = ModelResultsCollector()
        loader = DataLoader()
        inventory = InventoryManager(loader=loader, sources=["etc/hosts/"])
        variable_manager = VariableManager(loader=loader, inventory=inventory)
        passwords = {}
        play_source = {
            'name' : 'fact',
            'hosts' : 'all',
            'gather_facts' : 'no',
            'tasks' : [
                {
                    'name' : 'fact',
                    'setup' : ''
                }
            ],
        }

    
        play = Play().load(play_source, variable_manager=variable_manager, loader=loader)    
        tqm = TaskQueueManager(
           inventory=inventory,
           variable_manager=variable_manager,
           loader=loader,
           options=options,
           passwords=passwords,
           stdout_callback=callback,
           )

        result = tqm.run(play)
        result_raw = {'success':{},'failed':{},'unreachable':{}}
        for host,result in callback.host_ok.items():
            result_raw['success'][host] = result._result
        for host,result in callback.host_failed.items():
            result_raw['failed'][host] = result._result
        for host,result in callback.host_unreachable.items():
            result_raw['unreachable'][host] = result._result
        

        #js = json.dumps(result_raw, sort_keys=False, indent=4)
        sucess = list(result_raw['success'].keys())
        for i in sucess:
            dic=result_raw['success'][i]["ansible_facts"]
            arch=dic.get('ansible_architecture','')
            mem=dic.get('ansible_memtotal_mb',0)
            cpu=dic.get('ansible_processor_vcpus',0)
            os=dic.get('ansible_os_family','')
            ip=dic.get('ansible_default_ipv4',{}).get('address', '')
            p_ip=i              #公网IP
            name=dic.get('ansible_nodename',i)
            os_v = dic.get('ansible_distribution_version','')
            Host.create_or_replace(name, ip, arch, os, mem, cpu,p_ip,os_v)
           

        fail = list(result_raw['failed'].keys())
        for i in fail:
            arch=''
            mem=0
            cpu=0
            os=''
            ip=i
            p_ip=i
            name=''
            os_v=''
            Host.create_or_replace(name, ip, arch, os, mem, cpu,p_ip,os_v) 


        unreachable = list(result_raw['unreachable'].keys())
        for i in unreachable:
            arch=''
            mem=0
            cpu=0
            os=''
            ip=i
            p_ip=i
            name=''
            os_v=''
            Host.create_or_replace(name, ip, arch, os, mem, cpu,p_ip,os_v)