使用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