一:简介

使用Ansible + Flask + Celery搭建web平台。

目录结构

.

├── ansible_api

│   ├── ansible_playbook_inventory.py

│   ├── ansible_playbook.py

│   ├── ansible_task.py

│   ├── init.py

│   └── README.md

├── app.py

├── config

│   ├── Config.ini

│   └── hosts

├── database

│   ├── controller.py

│   ├── db_controller.py

│   └── init.py

├── README.md

├── requirements.txt

└── templates

└── index.html

4 directories, 14 files

二:Ansible Api代码

Ansible Task Api 这一部分跟命令行执行Ansible命令格式一样

ansible_task.py

#!/usr/bin/env python
# coding:utf-8 import json
from collections import namedtuple
from ansible.parsing.dataloader import DataLoader
from ansible.vars import VariableManager
from ansible.inventory import Inventory
from ansible.playbook.play import Play
from ansible.executor.task_queue_manager import TaskQueueManager
from ansible.plugins.callback import CallbackBase # 这里为封装的返回信息,让我们获取返回信息更加方便进行处理
class ResultCallback(CallbackBase):
def __init__(self, *args):
super(ResultCallback, self).__init__(display=None)
self.status_ok = json.dumps({}, ensure_ascii=False)
self.status_fail = json.dumps({}, ensure_ascii=False)
self.status_unreachable = json.dumps({}, ensure_ascii=False)
self.status_playbook = ''
self.status_no_hosts = False
self.host_ok = {}
self.host_failed = {}
self.host_unreachable = {} def v2_runner_on_ok(self, result):
host = result._host.get_name()
self.runner_on_ok(host, result._result)
self.host_ok[host] = result def v2_runner_on_failed(self, result, ignore_errors=False):
host = result._host.get_name()
self.runner_on_failed(host, result._result, ignore_errors)
self.host_failed[host] = result def v2_runner_on_unreachable(self, result):
host = result._host.get_name()
self.runner_on_unreachable(host, result._result)
self.host_unreachable[host] = result class Task():
def __init__(self, module, command):
self.module = module
self.command = command
def run(self):
# 这里跟ansible.cfg文件中的配置一样
Options = namedtuple('Options',
['listtags',
'listtasks',
'listhosts',
'syntax',
'connection',
'module_path',
'forks',
'remote_user',
'private_key_file',
'ssh_common_args',
'ssh_extra_args',
'sftp_extra_args',
'scp_extra_args',
'become',
'become_method',
'become_user',
'verbosity',
'check']
)
variable_manager = VariableManager()
loader = DataLoader()
options = Options(listtags=False,
listtasks=False,
listhosts=False,
syntax=False,
connection='smart',
module_path='/usr/lib/python2.6/site-packages/ansible/modules/',
forks=100,
remote_user='root',
private_key_file=None,
ssh_common_args=None,
ssh_extra_args=None,
sftp_extra_args=None,
scp_extra_args=None,
become=False,
become_method=None,
become_user='root',
verbosity=None,
check=False
)
passwords = dict(vault_pass='secret')
# 传入的hosts文件
inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list='config/hosts')
variable_manager.set_inventory(inventory) # 要执行的task
play_source = dict(
name = "Ansible Play",
hosts = 'all',
gather_facts = 'no',
tasks = [
dict(action=dict(module=self.module, args=self.command), register='shell_out'),
#dict(action=dict(module='shell', args='id'), register='shell_out'),
#dict(action=dict(module='shell', args=dict(msg='{{shell_out.stdout}}')))
]
)
play = Play().load(play_source, variable_manager=variable_manager, loader=loader) tqm = None
try:
tqm = TaskQueueManager(
inventory=inventory,
variable_manager=variable_manager,
loader=loader,
options=options,
passwords=passwords,
)
self.results_callback = ResultCallback()
tqm._stdout_callback = self.results_callback
result = tqm.run(play)
finally:
if tqm is not None:
tqm.cleanup() def get_result(self):
# 执行任务
self.run()
result_all = {'success': {}, 'failed': {}, 'unreachable': {}}
for host, result in self.results_callback.host_ok.items():
info = {}
info['stdout'] = result._result['stdout']
info['delta'] = result._result['delta']
result_all['success'][host] = info for host, result in self.results_callback.host_failed.items():
if 'msg' in result._result:
result_all['failed'][host] = result._result['msg'] for host, result in self.results_callback.host_unreachable.items():
if 'msg' in result._result:
result_all['unreachable'][host] = result._result['msg'] return json.dumps(result_all, ensure_ascii=False,sort_keys=True, indent=2) if __name__ =='__main__':
res=Task('shell', 'hostname')
print res.get_result()

Ansible playbook,执行playbook文件

ansible_playbook.py

#!/usr/bin/python
#coding:utf8 import os
import json
from collections import namedtuple
from ansible.inventory import Inventory
from ansible.vars import VariableManager
from ansible.parsing.dataloader import DataLoader
from ansible.executor.playbook_executor import PlaybookExecutor
from ansible.plugins.callback import CallbackBase
from ansible.errors import AnsibleParserError class mycallback(CallbackBase): def __init__(self, *args):
super(mycallback, self).__init__(display=None)
self.status_ok = json.dumps({}, ensure_ascii=False)
self.status_fail = json.dumps({}, ensure_ascii=False)
self.status_unreachable = json.dumps({}, ensure_ascii=False)
self.status_playbook = ''
self.status_no_hosts = False
self.host_ok = {}
self.host_failed = {}
self.host_unreachable = {} def v2_runner_on_ok(self, result):
host = result._host.get_name()
self.runner_on_ok(host, result._result)
self.host_ok[host] = result def v2_runner_on_failed(self, result, ignore_errors=False):
host = result._host.get_name()
self.runner_on_failed(host, result._result, ignore_errors)
self.host_failed[host] = result def v2_runner_on_unreachable(self, result):
host = result._host.get_name()
self.runner_on_unreachable(host, result._result)
self.host_unreachable[host] = result def v2_playbook_on_no_hosts_matched(self):
self.playbook_on_no_hosts_matched()
self.status_no_hosts = True def v2_playbook_on_play_start(self, play):
self.playbook_on_play_start(play.name)
self.playbook_path = play.name class ansible_playbook(): # 初始化各项参数,根据需求修改
def __init__(self, playbook,
host_list='/etc/ansible/hosts',
ansible_cfg=None,
passwords={}):
self.playbook_path = playbook
self.passwords = passwords
Options = namedtuple('Options', ['listtags', 'listtasks', 'listhosts', 'syntax', 'connection','module_path','forks', 'remote_user', 'private_key_file', 'ssh_common_args', 'ssh_extra_args','sftp_extra_args', 'scp_extra_args', 'become', 'become_method', 'become_user','verbosity', 'check'])
self.options = Options(listtags=False, listtasks=False, listhosts=False, syntax=False, connection='smart',module_path='/usr/lib/python2.6/site-packages/ansible/modules/', forks=100,remote_user='root', private_key_file=None, ssh_common_args=None, ssh_extra_args=None,sftp_extra_args=None, scp_extra_args=None, become=False, become_method=None, become_user='root',verbosity=None, check=False)
if ansible_cfg != None:
os.environ["ANSIBLE_CONFIG"] = ansible_cfg
self.variable_manager = VariableManager()
self.loader = DataLoader()
self.inventory = Inventory(loader=self.loader, variable_manager=self.variable_manager, host_list=host_list) # 定义运行的方法和返回值
def run(self):
#判断playbook是否存在
if not os.path.exists(self.playbook_path):
code = 1000
results = {'playbook': self.playbook_path, 'msg': self.playbook_path + ' playbook is not exist',
'flag': False}
pbex = PlaybookExecutor(playbooks=[self.playbook_path],
inventory=self.inventory,
variable_manager=self.variable_manager,
loader=self.loader,
options=self.options,
passwords=self.passwords)
self.results_callback = mycallback()
pbex._tqm._stdout_callback = self.results_callback try:
code = pbex.run()
except AnsibleParserError:
code = 1001
results = {'playbook': self.playbook_path, 'msg': self.playbook_path + ' playbook have syntax error',
'flag': False}
return code, results
if self.results_callback.status_no_hosts:
code = 1002
results = {'playbook': self.playbook_path, 'msg': self.results_callback.status_no_hosts, 'flag': False,
'executed': False}
return code, results def get_result(self):
result_all = {'success': {}, 'failed': {}, 'unreachable': {}} for host, result in self.results_callback.host_ok.items():
result_all['success'][host] = result._result for host, result in self.results_callback.host_failed.items():
if 'msg' in result._result:
result_all['failed'][host] = result._result['msg'] for host, result in self.results_callback.host_unreachable.items():
if 'msg' in result._result:
result_all['unreachable'][host] = result._result['msg'] return json.dumps(result_all, ensure_ascii=False,sort_keys=True, indent=2) if __name__ == '__main__':
play_book = ansible_playbook(playbook='/etc/ansible/site.yml')
play_book.run()
print play_book.get_result()

Ansible playbook支持动态inventory传入

ansible_playbook_inventory.py

#!/usr/bin/python
#coding:utf8 import os
import json
from collections import namedtuple
from ansible.inventory import Inventory
from ansible.vars import VariableManager
from ansible.parsing.dataloader import DataLoader
from ansible.executor.playbook_executor import PlaybookExecutor
from ansible.plugins.callback import CallbackBase
from ansible.errors import AnsibleParserError
from ansible.inventory.group import Group
from ansible.inventory.host import Host class mycallback(CallbackBase):
def __init__(self, *args):
super(mycallback, self).__init__(display=None)
self.status_ok = json.dumps({}, ensure_ascii=False)
self.status_fail = json.dumps({}, ensure_ascii=False)
self.status_unreachable = json.dumps({}, ensure_ascii=False)
self.status_playbook = ''
self.status_no_hosts = False
self.host_ok = {}
self.host_failed = {}
self.host_unreachable = {} def v2_runner_on_ok(self, result):
host = result._host.get_name()
self.runner_on_ok(host, result._result)
self.host_ok[host] = result def v2_runner_on_failed(self, result, ignore_errors=False):
host = result._host.get_name()
self.runner_on_failed(host, result._result, ignore_errors)
self.host_failed[host] = result def v2_runner_on_unreachable(self, result):
host = result._host.get_name()
self.runner_on_unreachable(host, result._result)
self.host_unreachable[host] = result def v2_playbook_on_no_hosts_matched(self):
self.playbook_on_no_hosts_matched()
self.status_no_hosts = True def v2_playbook_on_play_start(self, play):
self.playbook_on_play_start(play.name)
self.playbook_path = play.name class MyInventory(Inventory):
"""
this is my ansible inventory object.
"""
def __init__(self, resource, loader, variable_manager):
"""
resource的数据格式是一个列表字典,比如
{
"group1": {
"hosts": [{"hostname": "10.0.0.0", "port": "22", "username": "test", "password": "pass"}, ...],
"vars": {"var1": value1, "var2": value2, ...}
}
} 如果你只传入1个列表,这默认该列表内的所有主机属于my_group组,比如
[{"hostname": "10.0.0.0", "port": "22", "username": "test", "password": "pass"}, ...]
"""
self.resource = resource
self.inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list=[])
self.gen_inventory() def my_add_group(self, hosts, groupname, groupvars=None):
"""
add hosts to a group
"""
my_group = Group(name=groupname) # if group variables exists, add them to group
if groupvars:
for key, value in groupvars.iteritems():
my_group.set_variable(key, value) # add hosts to group
for host in hosts:
# set connection variables
hostname = host.get("hostname")
hostip = host.get('ip', hostname)
hostport = host.get("port")
username = host.get("username")
password = host.get("password")
ssh_key = host.get("ssh_key")
my_host = Host(name=hostname, port=hostport)
my_host.set_variable('ansible_ssh_host', hostip)
my_host.set_variable('ansible_ssh_port', hostport)
my_host.set_variable('ansible_ssh_user', username)
my_host.set_variable('ansible_ssh_pass', password)
my_host.set_variable('ansible_ssh_private_key_file', ssh_key) # set other variables
for key, value in host.iteritems():
if key not in ["hostname", "port", "username", "password"]:
my_host.set_variable(key, value)
# add to group
my_group.add_host(my_host) self.inventory.add_group(my_group) def gen_inventory(self):
"""
add hosts to inventory.
"""
if isinstance(self.resource, list):
self.my_add_group(self.resource, 'default_group')
elif isinstance(self.resource, dict):
for groupname, hosts_and_vars in self.resource.iteritems():
self.my_add_group(hosts_and_vars.get("hosts"), groupname, hosts_and_vars.get("vars")) class ansible_playbook(object):
# 初始化各项参数,根据需求修改
def __init__(self, playbook, hosts,ansible_cfg=None, passwords={}):
self.playbook_path = playbook
self.hosts = hosts
self.passwords = passwords
Options = namedtuple('Options',
['listtags',
'listtasks',
'listhosts',
'syntax',
'connection',
'module_path',
'forks',
'remote_user',
'private_key_file',
'ssh_common_args',
'ssh_extra_args',
'sftp_extra_args',
'scp_extra_args',
'become',
'become_method',
'become_user',
'verbosity',
'check'
])
self.options = Options(listtags=False,
listtasks=False,
listhosts=False,
syntax=False,
connection='smart',
module_path='/usr/lib/python2.6/site-packages/ansible/modules/',
forks=100,remote_user='root',
private_key_file=None,
ssh_common_args=None,
ssh_extra_args=None,
sftp_extra_args=None,
scp_extra_args=None,
become=False,
become_method=None,
become_user='root',
verbosity=None,
check=False
)
if ansible_cfg != None:
os.environ["ANSIBLE_CONFIG"] = ansible_cfg
self.variable_manager = VariableManager()
self.loader = DataLoader()
#self.inventory = Inventory(loader=self.loader, variable_manager=self.variable_manager, host_list=host_list)
self.inventory = MyInventory(self.hosts, self.loader, self.variable_manager).inventory
self.variable_manager.set_inventory(self.inventory) # 定义运行的方法和返回值
def run(self):
#判断playbook是否存在
if not os.path.exists(self.playbook_path):
code = 1000
results = {'playbook': self.playbook_path,
'msg': self.playbook_path + ' playbook is not exist',
'flag': False
}
pbex = PlaybookExecutor(playbooks=[self.playbook_path],
inventory=self.inventory,
variable_manager=self.variable_manager,
loader=self.loader,
options=self.options,
passwords=self.passwords
)
self.results_callback = mycallback()
pbex._tqm._stdout_callback = self.results_callback try:
code = pbex.run()
except AnsibleParserError:
code = 1001
results = {'playbook': self.playbook_path, 'msg': self.playbook_path + ' playbook have syntax error',
'flag': False}
return code, results
if self.results_callback.status_no_hosts:
code = 1002
results = {'playbook': self.playbook_path, 'msg': self.results_callback.status_no_hosts, 'flag': False,
'executed': False}
return code, results def get_result(self):
result_all = {'success': {}, 'failed': {}, 'unreachable': {}} for host, result in self.results_callback.host_ok.items():
result_all['success'][host] = result._result for host, result in self.results_callback.host_failed.items():
if 'msg' in result._result:
result_all['failed'][host] = result._result['msg'] for host, result in self.results_callback.host_unreachable.items():
if 'msg' in result._result:
result_all['unreachable'][host] = result._result['msg'] return json.dumps(result_all, ensure_ascii=False,sort_keys=True, indent=2) if __name__ == '__main__':
# 动态资产信息传入
hosts = {
"group1": {
"hosts": [{"hostname": "192.168.30.141", "port": "22", "username": "root", "password": "oracle"}],
"vars": {"var1": "value1"}
}
} play_book = ansible_playbook(playbook='/etc/ansible/playbook_yaml/user.yml', hosts = hosts)
play_book.run()
print play_book.get_result()

目前位置Ansible API三个文件准备好了,下一步开始准备数据库的信息录入。

Flask Ansible自动化平台搭建(持续更新)的更多相关文章

  1. 接口自动化平台搭建(四),自动化项目Jenkins持续集成

    一.Jenkins的优点 1.传统网站部署流程   一般网站部署的流程 这边是完整流程而不是简化的流程 需求分析—原型设计—开发代码—内网部署-提交测试—确认上线—备份数据—外网更新-最终测试 ,如果 ...

  2. 接口自动化平台搭建(二),搭建django项目与接口自动化平台的由来与功能特征

    1.创建django项目 a.使用命令创建,安装完django之后就有django-admin命令了,执行命令创建即可,命令如下: django-admin startproject my_djang ...

  3. [Mongodb] Relica Set复制集集群简单搭建--持续更新

    这里我们搭建一个没有访问控制,奇数个Set没有仲裁者的集群 1.三个配置文件添加 replication.replSetName:"Replica_test" 并启动三个实例2.连 ...

  4. 四、自动化平台搭建-Django-如何做验证码

    前提:安装包   pip install pillow==3.4.1 1.打开booktest/views.py,创建视图verify_code. from PIL import Image, Ima ...

  5. 一、自动化平台搭建-python虚拟环境安装

    主要知识点介绍: 安装django环境 创建django项目 设计模型类并利用模型类和数据库进行交互 使用django后台管理数据 编写视图函数,进行URL配置 模板的使用 图书-英雄案例完成 1.虚 ...

  6. 南京邮电大学网络攻防平台——WriteUp(持续更新)

    1.签到题 右键查看源代码直接获得flag 2.MD5collision(MD5碰撞) 观察源码发现md51等于QNKCDZO通过MD5加密的结果,使用在线解密发现结果为 0e830400451993 ...

  7. Hadoop高可用平台搭建

    文章概览: 1.机器规划和预配置 2.软件安装 3.集群文件配置 4.启动集群 5.HA验证 6.注意事项 7.小结   机器规划和预配置 主机/进程 NN DN RM NM ZK(QP) ZKFC ...

  8. 用Vagrant和Ansible搭建持续交付平台

    这是一个关于Vagrant的学习系列,包含如下文章: Vagrant入门 创建自己的Vagrant box 用Vagrant搭建Jenkins构建环境 用Vagrant和Ansible搭建持续交付平台 ...

  9. PYTHON爬虫实战_垃圾佬闲鱼爬虫转转爬虫数据整合自用二手急速响应捡垃圾平台_3(附源码持续更新)

    说明 文章首发于HURUWO的博客小站,本平台做同步备份发布. 如有浏览或访问异常图片加载失败或者相关疑问可前往原博客下评论浏览. 原文链接 PYTHON爬虫实战_垃圾佬闲鱼爬虫转转爬虫数据整合自用二 ...

随机推荐

  1. POI--帮助文档

    1.创建工作簿 (WORKBOOK) HSSFWorkbook wb = new HSSFWorkbook(); FileOutputStream fileOut = new FileOutputSt ...

  2. 123 A. Prime Permutation

    链接 http://codeforces.com/contest/123/problem/A 题目 You are given a string s, consisting of small Lati ...

  3. js实现键盘按键检测

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD ...

  4. NOI2001 食物链

    食物链 题目描述 动物王国中有三类动物 A,B,C,这三类动物的食物链构成了有趣的环形.A 吃 B,B 吃 C,C 吃 A. 现有 N 个动物,以 1 - N 编号.每个动物都是 A,B,C 中的一种 ...

  5. Golang时间格式化

    PHP中格式化时间很方便,只需要一个函数就搞定: date("Y-m-d H:i:s") 而在Golang中,用的是"2006-01-02 15:04:05"这 ...

  6. Eclipse的调试功能的10个小窍门[转]

    原文链接:http://www.importnew.com/6164.html 你可能已经看过一些类似“关于调试的N件事”的文章了.但我想我每天大概在调试上会花掉1个小时,这是非常多的时间了.所以非常 ...

  7. ES6知识点大汇总

    1 //1.搭建ES6的开发环境: 2 let a=1; 3 console.log(a); 4 //2.var 声明的是全局变量 全局变量会污染外部的区块 5 //let 局部变量 6 //cons ...

  8. HDU [P2819] swap

    二分图行列匹配+输出路径 经典题,当且仅当一行匹配一列的时候,符合题意. 本题的难点在于如何输出路径,我们发现这个移动的过程就是将所有匹配选择排序,在选择排序时输出路径即可 #include < ...

  9. 洛谷 [P3033] 牛的障碍

    利用二分图匹配求最大独立集 本题的边一定平行于坐标轴,且同向的线段一定不重合,这是经典的二分图建图方法,本题要求的是最大不重合的线段数,那就是求二分图的最大独立集,最大独立集=总点数-最大匹配数. 本 ...

  10. 济南清北学堂游记 Day 1.

    快住手!这根本不是暴力! 刷了一整天的题就是了..上午三道题的画风还算挺正常,估计是第一天,给点水题做做算了.. rqy大佬AK了上午的比赛! 当时我t2暴力写挂,还以为需要用啥奇怪的算法,后来发现, ...