使用flask从零构建自动化运维平台系列四

这篇博客是关于使用Flask从零构建自动化运维平台的系列第四部分,主要讲述如何构建CMDB平台的资产收集功能。通过Ansible 2.0的JSONRPC2.0接口进行资产数据收集,并定义数据存储字段。同时,介绍了如何在Flask中使用flask_marshmallow库将ORM数据转换为前端友好的JSON格式。
摘要由CSDN通过智能技术生成

使用flask从零构建自动化运维平台系列四

cmdb平台构建之资产收集

这里就需要使用到ansible

ansible2.0

2.0的API拓展性可高。我用的是别人改好的。从jumpserver里面抽取出来

from app.ansible2.runner import AdHocRunner, CommandRunner
from app.ansible2.inventory import BaseInventory

host_data = [
    {
   
        "hostname": "testserver",
        "ip": "192.168.1.121",
        "port": 22,
        "username": "shuaibo",
        "password": "123456",
    },
]
tasks = [
    {
   "action": {
   "module": "setup", "args": ""}, "name": "run_cmd"},
    {
   "action": {
   "module": "shell", "args": "whoami"}, "name": "run_whoami"},
]
inventory = BaseInventory(host_data)
runner = AdHocRunner(inventory)
ret = runner.run(tasks, "all")

print(ret.results_raw['ok']['testserver']['run_cmd']['ansible_facts'])

ansible2.0JSONRPC2.0接口
@jsonrpc.method('ansible.run(hosts=list,tasks=list)->Object', authenticated=User.check_auth)
def ansible_run(hosts, tasks):
    # host_data = [
    #     {
   
    #         "hostname": "testserver",
    #         "ip": "192.168.1.121",
    #         "port": 22,
    #         "username": "shuaibo",
    #         "password": "123456",
    #     },
    # ]
    # tasks = [
    #     {"action": {"module": "setup", "args": ""}, "name": "run_cmd"},
    #     {"action": {"module": "shell", "args": "whoami"}, "name": "run_whoami"},
    # ]

    hosts = hosts
    tasks = tasks
    inventory = BaseInventory(hosts)
    runner = AdHocRunner(inventory)
    ret = runner.run(tasks, "all")
    return ret.results_raw

定义数据存储的字段

还是使用orm去定义

class Asset(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    ip = db.Column(db.String(80))
    hostname = db.Column(db.String(80))
    port = db.Column(db.String(80))
    is_active = db.Column(db.String(80))
    public_ip = db.Column(db.String(80))
    number = db.Column(db.String(80))
    # Collect
    vendor = db.Column(db.String(80))
    model = db.Column(db.String(80))
    sn = db.Column(db.String(80))
    cpu_model = db.Column(db.String(80))
    cpu_count = db.Column(db.String(80))
    cpu_cores = db.Column(db.String(80))
    memory = db.Column(db.String(80))
    disk_total = db.Column(db.String(80))
    disk_info = db.Column(db.String(80))
    platform = db.Column(db.String(80))
    os = db.Column(db.String(80))
    os_version = db.Column(db.String(80))
    os_arch = db.Column(db.String(80))
    hostname_raw = db.Column(db.String(80))
    labels = db.Column(db.String(80))
    created_by = db.Column(db.String(80))
    date_created = db.Column(db.String(80))
    comment = db.Column(db.String(80))

添加主机

在这里插入图片描述

添加主机controller
function HostAddCtrl($scope, jsonrpc, $cookies) {
   
    $scope.get = function () {
   
        jsonrpc.request('ansible.run', {
   
            hosts: [{
   
                "hostname": $scope.ip2,
                "username": $scope.username,
                "ip": $scope.ip2,
                "port": $scope.port,
                "password": $scope.password
            }],
            tasks: [{
   "action": {
   "module": "setup", "args": ""}, "name": "asset_info"}],
            token: $cookies.get('token')
        })
            .then(function (result) {
   
                t = result['ok'][$scope.ip2]['asset_info']['ansible_facts']
                $scope.vendor = t['ansible_system_vendor']
                $scope.model = t['ansible_system']
                $scope.cpu_model = t['ansible_processor'][2]
                $scope.cpu_count = t['ansible_processor_count']
                $scope.cpu_cores = t['ansible_processor_cores']
                $scope.memory = t['ansible_memtotal_mb']
                $scope.platform = t['ansible_os_family']
                $scope.os = t['ansible_distribution']
                $scope.os_version = t['ansible_distribution_version']
                $scope.os_arch = t['ansible_machine']
                $scope.hostname_raw = t['ansible_hostname']
                // console.log(assetinfo)

            }).catch(function (reason) {
   
        })

    }
    $scope.add = function () {
   
        jsonrpc.request('asset.add', {
   
            token: $cookies.get('token'),
            assetinfo: [{
   
                hostname_raw: $scope.hostname_raw,
                ip: $scope.ip2
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值