ansible检测链路状态和切换状态
控制机
ansible.cfg
callback_plugins = /usr/share/ansible/plugins/callback:/opt/ansible/plugins/callback
[ssh_connection]
ssh_args = -F /opt/ansible/ssh/ssh_config -o ControlMaster=auto -o ControlPersist=5m
control_path = /opt/ansible/ssh/ansible-ssh-%%h-%%p-%%r
pipelining = True
ssh_config
Host 69.169.xx.xx
User ansible
Port 29922
ForwardAgent yes
StrictHostKeyChecking no
Host 10.150.1.*
User ansible
Port 29922
ProxyCommand ssh -p 29922 ansible@69.169.xx.xx -W %h:%p
ForwardAgent yes
StrictHostKeyChecking no
Host 173.45.xx.xx
User ansible
Port 29922
ForwardAgent yes
StrictHostKeyChecking no
Host 10.160.1.*
User ansible
Port 29922
ProxyCommand ssh -p 29922 ansible@173.45.xx.xx -W %h:%p
ForwardAgent yes
StrictHostKeyChecking no
inventory
[dc]
69.169.xx.xx
[la]
173.45.xx.xx
[proxy-dc]
10.150.1.219
10.150.1.220
[proxy-la]
10.160.1.219
10.160.1.220
change_link.yaml
- hosts: proxy-dc
gather_facts: no
vars:
proxy_host: '221.228.xx.xx'
tasks:
- name: update /etc/hosts
template: src=templates/hosts.j2 dest=/etc/hosts owner=root group=root mode=0644
notify: restart dnsmasq
handlers:
- name: restart dnsmasq
service: name=dnsmasq state=restarted
demo example_play.py
以ansible用户身份登录远程机房内网服务器,sudo切换root权限,返回服务器的网卡流量、连接数、hosts指向
#!/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
def play_task(host, nic):
class ResultCallback(CallbackBase):
def __init__(self, *args, **kwargs):
self.results = None
def v2_runner_on_unreachable(self, result):
ip = result._host.get_name()
self.results = (ip, '', '', '', '')
def v2_runner_on_failed(self, result, *args, **kwargs):
ip = result._host.get_name()
self.results = (ip, '', '', '', '')
def v2_runner_on_ok(self, result, *args, **kwargs):
ip = result._host.get_name()
traffic_in = result._result['link_info']['in']
traffic_out = result._result['link_info']['out']
connection = result._result['link_info']['connection']
current_link = result._result['link_info']['current_link']
self.results = (ip, traffic_in, traffic_out, connection, current_link)
Options = namedtuple('Options', ['connection', 'module_path', 'forks', 'become', 'become_method', 'become_user', 'check'])
variable_manager = VariableManager()
loader = DataLoader()
options = Options(
connection = 'ssh',
module_path = '/opt/ansible/plugins',
forks = 100,
become = True,
become_method = 'sudo',
become_user = 'root',
check = False
)
passwords = dict(vault_pass='secret')
results_callback = ResultCallback()
inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list='/opt/ansible/inventory')
variable_manager.set_inventory(inventory)
play_source = dict(
name = 'Ansible Play',
hosts = host,
gather_facts = 'no',
tasks = [
dict(action=dict(module='link_info', args='interface=%s' % nic)),
]
)
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=None,
stdout_callback=results_callback,
)
tqm.run(play)
finally:
if tqm is not None:
tqm.cleanup()
return results_callback.results
link_info模块
#!/usr/bin/env python
# -*- coding: utf-8 -*-
DOCUMENTATION = '''
---
'''
EXAMPLES = '''
---
'''
import re, socket
import datetime, time
import subprocess
from ansible.module_utils.basic import *
def unit_convert(data):
if data < 10**3:
return str(round(float(data), 2))
elif data > 10**3 and data < 10**6:
return str(round(float(data) / 10**3, 2)) + 'Kb'
elif data > 10**6 and data < 10**9:
return str(round(float(data) / 10**6, 2)) + 'Mb'
elif data > 10**9 and data < 10**12:
return str(round(float(data) / 10**9, 2)) + 'Gb'
elif data > 10**12 and data < 10**15:
return str(round(float(data) / 10**12, 2)) + 'Tb'
def get_traffic_info():
proc_stat = '/proc/net/dev'
traffic = []
with open(proc_stat, 'r') as f:
for line in f:
m = re.match('\s*(\S+):\s*(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+', line)
if m:
traffic.append((m.group(1), int(m.group(2)), int(m.group(10))))
return traffic
def get_connection_info():
command = 'netstat -n |grep ^tcp |wc -l'
p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output, error = p.communicate()
return int(output)
def get_current_link_info():
hostfile = '/etc/hosts'
with open(hostfile, 'r') as f:
for line in f:
m = re.match('(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\s+(\S+)', line)
if m and m.group(2) == 'wx.service.live.tvmining.com':
return m.group(1)
return 'unknown'
def main():
module = AnsibleModule(
argument_spec = {
'interface': { 'default': 'em2', 'type': 'str'}
},
supports_check_mode = True
)
counter = 2
first = get_traffic_info()
time.sleep(counter)
second = get_traffic_info()
traffic = map(lambda x, y: (x[0], unit_convert(abs(x[1]-y[1])*8), unit_convert(abs(x[2]-y[2])*8)), first, second)
for name, receive, transmit in traffic:
if name == module.params['interface']:
traffic_in, traffic_out = receive, transmit
connection = get_connection_info()
current_link = get_current_link_info()
message = {
'in': traffic_in,
'out': traffic_out,
'connection': connection,
'current_link': current_link,
}
module.exit_json(changed=False, link_info=message)
if __name__ == '__main__':
main()
demo example_playbook.py
以ansible身份登录远程内网服务器,sudo切换root权限,执行change_link.yaml
#!/usr/bin/env python
# -*- coding: utf-8 -*-
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
from ansible.executor.playbook_executor import PlaybookExecutor
Options = namedtuple('Options', \
['connection', 'module_path', 'forks', 'become', 'become_method', 'become_user', 'check', 'listhosts', 'listtasks', 'listtags', 'syntax'])
# 这些options项是必须的,缺少的话程序报错
options = Options(
connection = 'ssh',
module_path = '/path/to/mymodules',
forks = 100,
become = True,
become_method = 'sudo',
become_user = 'root',
check = False,
listhosts = None,
listtasks = None,
listtags = None,
syntax = None
)
loader = DataLoader()
variable_manager = VariableManager()
inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list='/opt/ansible/inventory')
variable_manager.set_inventory(inventory)
playbooks=['/opt/ansible/change_link.yaml']
pb = PlaybookExecutor(playbooks=playbooks, inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords=None)
pb.run()
ansible检测链路状态和切换状态的更多相关文章
- android fragment+ FragmentTabHost+viewpager 切换状态不保存的问题
转载请注明出处:http://blog.csdn.net/djy1992/article/details/46674169 @author dujinyang 难得有时间上来写博客. fragment ...
- Bootstrap switch 切换状态踩坑
Boostrap switch 下载地址(http://www.bootcss.com/p/bootstrap-switch/),同时配有一些简单的用例. 其中写到 Toggle State切换状态的 ...
- Animator状态快速切换问题
[Animator状态快速切换问题] 事情是这样的,我尝试在一帧内多次切换一些状态(当前状态为Idle的情况下): public Animator animator; void OnEnable() ...
- (笔记)Linux下检测网卡与网线连接状态
http://blog.chinaunix.net/space.php?uid=20357359&do=blog&cuid=1798479 Linux下检测网卡与网线连接状态,使用io ...
- (2.13)备份与还原--sql server数据库主要状态和切换路径
SQLSERVER数据库主要状态和切换路径 关键词:数据库状态 一个SQLSERVER数据库会处于很多种状态,例如 ONLINE .RESTORING .RECOVERING .RECOVERY_PE ...
- ARM 中断状态和SVC状态的堆栈切换 (异常)【转】
转自:http://blog.csdn.net/edwardlulinux/article/details/9261393 版权声明:本文为博主原创文章,未经博主允许不得转载. ARM 中断状态和SV ...
- 转载 iOS全局检测网络变化的实时状态
昨天浏览了cocoaChina,发现了一遍文章是优化Reachablity框架的出来的检测网络类,大家都知道这个Reachablity框架是用来检测网络变化的!但是也是有一点bug,事实上,基于此 ...
- Android软键盘状态的切换及其强制隐藏
MainActivity例如以下: package cc.c; import android.os.Bundle; import android.view.View; import android.v ...
- Flutter中TabBarView切换状态保存
TabBarView 类似于Android中的viewPager,但是默认是没有实现切换分页状态保存的.估计是出于节约内存的原因吧. 发现这个问题的时候,搜索了一下全网.大致就两种解决方案,1是修改源 ...
随机推荐
- Hibernate4日志及配置文件
1. 确定要使用日志的实现log4j 2. Slf4japi.jar和log4j的jar包放入classpath,(slf4j-log4j.jar) 3. 编写log4j.properties 4. ...
- 一次非常有意思的 SQL 优化经历
我用的数据库是mysql5.6,下面简单的介绍下场景 课程表 create table Course( c_id int PRIMARY KEY, name varchar(10) ) 数据100条 ...
- ylbtech-Unitity-CS:Generics
ylbtech-Unitity-CS:Generics 1.A,效果图返回顶部 Unsorted List: Raul:35 Alessandro:30 Maria:72 Hiroyuki:108 A ...
- 轻量级的.Net ORM框架介绍
轻量型 ORM 组件 FluentData 官网https://fluentdata.codeplex.com/ http://www.cnblogs.com/babietongtianta/p/43 ...
- [ActionScript 3.0] AS3 3D双圆环贴图
package { import flash.display.Bitmap; import flash.display.BitmapData; import flash.display.MovieCl ...
- C# 文件压缩与解压(ZIP格式)
在企业开发过程中经常会遇到文件的压缩与解压,虽然网上很多流行的压缩文件格式都是RAR的,但是由于RAR不是一个开放的标准,因此ZIP成了更多人的选择.如果你不想自己开发的话可以选择开源的项目,比如Sh ...
- poj 2253 Frogger dijkstra算法实现
点击打开链接 Frogger Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 21653 Accepted: 7042 D ...
- Sqoop导入MySQL数据
导入所有表: sqoop import-all-tables –connect jdbc:mysql://ip:port/dbName --username userName --password p ...
- 常见的MIME类型
超文本标记语言文本 .htm,.html text/html 普通文本 .txt text/plain GIF图形 .gif image/gif JPEG图形 .ipeg,.jpg image/jpe ...
- 反射IsGenericType
var propertyType = propertyInfo.PropertyType; if (propertyType.IsGenericType && propertyType ...