1. 程式人生 > >使用flask從零構建自動化運維平臺系列四

使用flask從零構建自動化運維平臺系列四

使用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,
                hostname: $scope.hostname,
                port: $scope.port,
                vendor: $scope.vendor,
                model: $scope.model,
                sn: $scope.sn,
                cpu_model:$scope.cpu_model,
                cpu_count:$scope.cpu_count,
                cpu_cores:$scope.cpu_cores,
                memory:$scope.memory,
                platform:$scope.platform,
                os:$scope.os,
                os_version:$scope.os_version,
                os_arch:$scope.os_arch
            }]
        }).then(function () {

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

新增主機html

<div class="row wrapper border-bottom white-bg page-heading">
    <div class="col-lg-10">
        <h2>SmartOps | 自動化運維繫統</h2>
        <ol class="breadcrumb">
            <li>
                <a href="/">SmartOps</a>
            </li>
            <li>
                <a>主機</a>
            </li>
            <li class="active">
                <strong>新增主機</strong>
            </li>
        </ol>
    </div>
    <div class="col-lg-2">

    </div>
</div>

<div class="wrapper wrapper-content animated fadeInRight" ng-controller="HostAddCtrl">
    <div class="row">
        <div class="col-lg-4">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5>單個新增</h5>
                </div>
                <div class="ibox-content">
                    <form role="form" class="form-inline ng-pristine ng-valid" ng-submit="get()">
                        <div class="form-group">

                            <input type="text" placeholder="ip" ng-model="ip2" id="ip" ng-model="asset.ip"
                                   class="form-control">
                        </div>
                        <div class="form-group">

                            <input type="text" placeholder="使用者名稱" id="username" ng-model="username"
                                   ng-model="asset.username"
                                   class="form-control">
                        </div>
                        <div class="form-group">

                            <input type="password" placeholder="密碼" id="password" ng-model="password"
                                   ng-model="asset.password"
                                   class="form-control">
                        </div>
                        <div class="form-group">

                            <input type="text" placeholder="" id="port" ng-model="port" ng-model="asset.port"
                                   class="form-control">
                        </div>
                        <div class="form-group">

                            <button type="submit" class="btn btn-primary block full-width m-b">獲取資訊</button>
                        </div>

                    </form>
                </div>
            </div>
        </div>
        <div class="col-lg-8">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5>主機資訊</h5>
                </div>
                <div class="ibox-content">

                    <form class="form-horizontal ng-pristine ng-valid" ng-submit="add()">
                        <div class="form-group">

                            <button type="submit" class="btn btn-primary block full-width m-b">新增</button>
                        </div>

                        <div class="form-group"><label class="col-lg-2 control-label">vendor:</label>

                            <div class="col-lg-10"><input type="text" ng-model="vendor" placeholder="vendor"
                                                          class="form-control">

                            </div>
                        </div>

                        <div class="form-group"><label class="col-lg-2 control-label">model:</label>

                            <div class="col-lg-10"><input type="text" ng-model="model" placeholder="model"
                                                          class="form-control">

                            </div>
                        </div>
                        <div class="form-group"><label class="col-lg-2 control-label">sn:</label>

                            <div class="col-lg-10"><input type="text" ng-model="sn" placeholder="sn"
                                                          class="form-control">

                            </div>
                        </div>
                        <div class="form-group"><label class="col-lg-2 control-label">cpu_model:</label>

                            <div class="col-lg-10"><input type="text" ng-model="cpu_model" placeholder="cpu_model"
                                                          class="form-control">

                            </div>
                        </div>
                        <div class="form-group"><label class="col-lg-2 control-label">cpu_count:</label>

                            <div class="col-lg-10"><input type="text" ng-model="cpu_count" placeholder="cpu_count"
                                                          class="form-control">

                            </div>
                        </div>
                        <div