1. Introduction to ansible inventory;
Ansible Inventory includes two parts: static Inventory and dynamic Inventory. Static Inventory refers to the host and group specified in the file. Dynamic Inventory refers to obtaining the host list through external script and returning it to ansilbe command according to the format required by ansible. In this part, the host information is generally obtained by combining the CMDB asset management system and cloud computing platform. Because the host resources will be increased or decreased dynamically, and these systems will be updated intelligently. We can return the list of hosts through the API provided by these tools or the receipt query.
2. The configuration of flask ﹣ Sqlalchemy;
(1).mysqlconfig.py (mysql Connector) class MysqlConfig(object): DIALECT = mysql DRIVER = pymysql USERNAME = "root" PASSWORD = "test" HOST = "192.168.1.1" PORT = "3306" DATABASE = getApolloconf("DATABASE") SQLALCHEMY_DATABASE_URI = "{}+{}://{}:{}@{}:{}/{}?charset=utf8".format(DIALECT, DRIVER, USERNAME, PASSWORD, HOST, PORT, DATABASE) SQLALCHEMY_TRACK_MODIFICATIONS = False
(2.1).models.py (Database field design) # -*- coding: utf-8 -*- import datetime from flask_sqlalchemy import SQLAlchemy db = SQLAlchemy() class ansible_hosts(db.Model): __tablename__ = 'ansible_hosts' id = db.Column(db.Integer, primary_key=True, autoincrement=True) #Primary key self growth host = db.Column(db.String(16), nullable=False) ###Can not be empty username = db.Column(db.String(32), nullable=False) ###Can not be empty password = db.Column(db.String(64), nullable=False) ###Can not be empty port = db.Column(db.String(8), nullable=False) create_time = db.Column(db.DateTime(timezone=False), default=datetime.datetime.now()) def to_dict(self): return {"id": self.id, "host": self.host, "username": self.username, "password": self.passsword, "port": self.port} (2.2).Database table initialization file from models import db from __init__ import create_app app = create_app() with app.app_context(): db.create_all() (2.3)Initialization table; python36 table.py (2.4).View database table structure; MySQL [ansible]> desc ansible_hosts; +-------------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------------+-------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | host | varchar(16) | NO | | NULL | | | username | varchar(32) | NO | | NULL | | | password | varchar(64) | NO | | NULL | | | port | varchar(8) | NO | | NULL | | | create_time | datetime | YES | | NULL | | +-------------+-------------+------+-----+---------+----------------+ (2.5).Dynamic host script; #!/usr/bin/python #-*- coding: UTF-8 -*- from __future__ import print_function import argparse import json from collections import defaultdict from contextlib import contextmanager from models import db from models import ansible_hosts def to_json(in_dict): return json.dumps(in_dict, sort_keys=True, indent=2) def parse_args(): parser = argparse.ArgumentParser(description='OpenStack Inventory Module') group = parser.add_mutually_exclusive_group(required=True) group.add_argument('--list', action='store_true', help='List active servers') group.add_argument('--host', help='List details about the specific host') return parser.parse_args() def ansiblehostsSelect(): import json from models import ansible_hosts queryhostData = ansible_hosts.query.filter_by(username='host').first() querygroupData = ansible_hosts.query.filter_by(username='group').first() return Response(json.dumps({"code": 0, "total": len(queryData), "host": [i.to_dict() for i in queryhostData ], "group":[i.to_dict() for i in querygroupData]}), mimetype='application/json') def main(): parser = parse_args() if parser.list: hosts = list_all_hosts(conn) print(to_json(hosts)) else: details = get_host_detail(conn, parser.host) print(to_json(details)) if __name__ == '__main__': main()
(3).flask init Initialization from flask import Flask from tools.config import MysqlConfig def create_app(): app = Flask(__name__) app.config.from_object(MysqlConfig) from models import db db.init_app(app) app.register_blueprint(opsapp,url_prefix='/opsapp') return app
(4).flask Boot configuration; from __init__ import create_app app = create_app() if __name__ == '__main__': app.run( host="127.0.0.1", port=5000, debug=True, )