记录一个ansible高级用法与shell结合_kali_yao的博客-CSDN博客_ansible shell

注:由于ansible的远程原则必须要key(也就是ssh远程测试),所以在配置文件中加host_key_checking = False即可,

1.下载asible与创建环境

~]# yum -y install ansible
~]# mkdir ansible && cd ansible

下载与ansible的介绍上面我写的链接有说我就不写了

2.基本配置

1)防火墙配置

#将防火墙关闭或设置成允许所有,selinux状态enforcing模式修改为permissive变成宽容模式
~]# setenforce 0
~]# vim /etc/selinux/config
.....
SELINUX=disabled
......
~]# service firewalld stop

2)配置文件配置

~]# cd ansible
~]# cp /etc/ansible/ansible.cfg ansible.cfg
​
# 书写配置文件
~]# vim ansible.cfg
[defaults]
inventory      = ~/ansible/hosts        # 指定主机清单
remote_user     = root                  # 连接受管机的远程用户
roles_path    = roles                   # 指定默认的角色目录
host_key_checking = False               # 设置参数为不检查key[privilege_escalation]                  # 设置用户 sudo 提权
become=True                             # 需要提权
become_method=sudo                      # 提权方式为 sudo
become_user=root                        # 提权为 root
become_ask_pass=False                   # 无需验证密码
forks = 10                              # ssh并发数量(默认是5)     

3)书写主机清单

~]# vim hosts
# ansible全局定义变量
[all:vars]
ansible_ssh_user=root          # 运程要用的用户
ansible_ssh_pass=NngnB@@2020   # 远程主机密码
ansible_port=22                # 远程端口,经量不要用22test_iperf_all='true'          # 压测开关
deploy_nginx="false"           # nginx开关[installer]                    # 只需要在控制机器上执行
172.17.0.51[nfs_all]
172.17.0.114
172.17.0.142
172.17.0.98[nfs]
172.17.0.142[nginx]
172.17.0.114[ceph]
172.17.64.7
172.17.64.6
172.17.64.5[mysql]
172.17.0.98   

4)测试

~]# ansible all -m ping
# 如测试失败注意配置文件注释那部分去掉

3.压测ansible

1)拉取角色

~]# ansible-galaxy init roles/hosts-check
- Role roles/hosts-check was created successfully

2)创建脚本

~]# cd /root/ansible/roles/hosts-check/templates
~]# mkdir iperf && cd iperf
~]# vim iperf3.sh.j2
#!/bin/bash
# 定义服务端测试ip
I=`head -1 /data/perfor-reports/iperf/groups.txt | awk -F"['']" '{for(i=1;i<=NF;i++)if(i%2==0) print $i}'`
# 定义客户端测试ip
k=`tail -1 /data/perfor-reports/iperf/groups.txt | awk -F"['']" '{for(i=1;i<=NF;i++)if(i%2==0) print $i}'`
# 定义服务端ip数组
IP=($I)# 测试丢包率
for i in ${IP[*]}
doecho $isshpass -p '{{ ansible_ssh_pass }}' ssh $i iperf3 -s -D -p 30000 >> /dev/null  &sleep 20iperf3 -c $i -u -b 100m -t 20 -p 30000 >> ceshi.log b=`grep %  ceshi.log| awk -F "[()]" '{print $2}' |awk -F"%" '{print $1}'`c=`grep host ceshi.log` rm -rf ceshi.log if [ $b% == 0% ];thenecho -e "\033[32m $c normal:$b%\033[0m"echo -e "\033[32m $c normal:$b%\033[0m" >> /data/perfor-reports/iperf/iperf3.logelseecho -e "\033[31m$c Packet loss rate is $b% \033[0m" echo "$c Packet loss rate is $b%" >> /data/perfor-reports/iperf/iperf3.logfiif [ $i == $k ];thencontinueelsesshpass -p '{{ ansible_ssh_pass }}' ssh $i ps -auxf | grep iperf >> a.txt cat a.txt | awk '{print $2}' | xargs -i sshpass -p '{{ ansible_ssh_pass }}' ssh $i kill {}rm -rf a.txtfi
done# 测试所有服务器下载上传
for i in ${IP[*]}
dosshpass -p '{{ ansible_ssh_pass }}' ssh $i speedtest-cli > ceshi.logd=`grep -E "Download|Upload" ceshi.log`echo -e "\033[32m $i Download and Upload is $d\033[0m"echo "$i Download and Upload is $d" >> /data/perfor-reports/iperf/iperf3.logrm -rf ceshi.log
done # 测试所有服务器ulimit
for i in ${IP[*]}
dosshpass -p '{{ ansible_ssh_pass }}' ssh $i ulimit -n > ceshi.loge=`cat ceshi.log`echo -e "\033[32m $i ulimit is $e\033[0m"echo "$i ulimit is $e" >> /data/perfor-reports/iperf/iperf3.logrm -rf ceshi.log
done

3)创建要测试的ip文件

~]# groups.txt.j2
{{ groups ['all'] }}
{{ groups ['installer'] }}

4)书写ansible

~]# cd /root/ansible/roles/hosts-check/tasks
~]# vim check-iperf3.yml
# deploy iperf3 and ulimit set
# 定义开关与标签
- name: set deploy_nginx factsset_fact: test_iperf_all = "{{ test_iperf_all }}"tags: test_iperf# 下载软件包
- name: install rpm iperf3 and speedtest-cliyum:name: "{{ item }}"loop:- iperf3  - epel-release - speedtest-cliwhen: test_iperf_all == "true" and inventory_hostname in groups ["all"]tags: test_iperf# 创建脚本与文件日志的目录
- name: create iperf dirshell: ls /data/perfor-reports/iperf || mkdir -p /data/perfor-reports/iperfwhen: test_iperf_all == "true" and inventory_hostname in groups ["installer"]tags: test_iperf# 拷贝脚本到目录
- name: copy iperf3.sh.j2template: src: templates/iperf/iperf3.sh.j2dest: /data/perfor-reports/iperf/iperf3.shmode: '0755'when: test_iperf_all == "true" and inventory_hostname in groups ["installer"]tags: test_iperf# 拷贝ip文件
- name: copy groups.txt.j2template:src: templates/iperf/groups.txt.j2dest: /data/perfor-reports/iperf/groups.txtwhen: test_iperf_all == "true" and inventory_hostname in groups ["installer"]tags: test_iperf# 执行文件
- name: deploy test iperf and speedtest-cli and ulimitshell: cd /data/perfor-reports/iperf/ && sh iperf3.shwhen: test_iperf_all == "true" and inventory_hostname in groups ["installer"]tags: test_iperf

5)调用yml文件

~]# cd /root/ansible/roles/hosts-check/tasks
~]# vim main.yml
---
# tasks file for roles/hosts-check
- include: check-iperf3.yml

6)书写ansible-playbook调用roles

~]# cd /root/ansible
~]# mkdir playbook && cd playbook
~]# vim hosts-check.yml
---
- hosts: '{{ hosts }}'     gather_facts: True     # 当语句有错误时继续执行environment:PATH: "{{ ansible_env.PATH }}:/usr/local/bin"become: yes            # 配置文件有写roles:                 # 调用角色- hosts-check

7)测试

~]# ansible-playbook playbook/hosts-check.yml

8)书写单个脚本函数

~]# cd /root/ansible
~]# vim hosts-check.sh
#!/bin/bash
# Author: kali
set -eBASE_DIR=$(cd `dirname $0` && pwd)
cd $BASE_DIRCALL_FUN="all_func"
hosts="all"help(){echo "show usage:"echo "check_test_iperf"
}while getopts ":f:h:" opt
docase $opt inf)CALL_FUN="${OPTARG}";;h)hosts="${OPTARG}";;?)echo "unkown args! just suport -f[call function] and -h[ansible hosts group] arg!!!"exit 0;;esac
done# check system and kernal version
# test iperf
check_test_iperf (){echo "###### test iperf ulmit down load ######"ansible-playbook -f 10 -i ../hosts --tags test_iperf ../playbooks/hosts-check/hosts-check.yml \--extra-vars "hosts=${hosts}"
}
# execute all function
all_func(){check_test_iperf
}main(){$CALL_FUN || help
}
main-f FORKS, --forks=FORKS#specify number of parallel processes to use(default=5)#并行任务数。FORKS被指定为一个整数,默认是5
-i INVENTORY, --inventory-file=INVENTORY#specify inventory host path (default=/etc/ansible/hosts) or comma separated host list.#指定要读取的Inventory文件
-tags           #available tags#指定可用的tags
-e EXTRA_VARS, --extra-vars=EXTRA_VARS#set additional variables as key=value or YAML/JSON#在Playbook中引入外部参数变量## 测试
~]# ./hosts-check.sh

9)书写集成

~]# vim pot-cmd.sh
#!/bin/bash
# Author: kali
set -eBASE_DIR=$(cd `dirname $0` && pwd)
cd $BASE_DIREXEC_SCRIPT=""
CALL_FUN="all_func"
hosts="all"help(){echo "show usage:"echo "you can exec script list: "echo `ls /root/ansible/shell`exit 0
}while getopts ":s:f:h:" opt
docase $opt ins)EXEC_SCRIPT="${OPTARG}";;f)CALL_FUN="${OPTARG}";;h)hosts="${OPTARG}";;?)echo "unkown args! just suport -s[mgr-scripts's script] -f[call function] and -h[ansible hosts group] arg!!!"exit 0;;esac
donecmd(){/root/ansible/${EXEC_SCRIPT} -f ${CALL_FUN} -h ${hosts}
}main(){if [ "x${EXEC_SCRIPT}" == "x" ]; thenhelpelsecmdfi
}
main
~]# ./pot-cmd.sh -f xxx
show usage:
you can exec script list:
hosts-check.sh
~]# ./pot-cmd.sh -s hosts-check.sh

ansible高级用法(压测脚本)相关推荐

  1. shell 压测_shell写一个压测脚本

    ab命令 ab是apache下面的一个性能压测工具 yum install -y httpd-tools ab -n 1000 -c 10 http://xxxx # -n 请求数 -c 并发数 基于 ...

  2. GUN sed高级用法,sed脚本编写

    这里举一些sed常用的高级用法例子经供参考: 一下操作都针对file.txt文件作修改 [root@QX-××× ~]# cat file.txt libgcc-4.4.7-4.el6.x86_64 ...

  3. apache ab压力测试工具-批量压测脚本

    概述 ab(Apache benchmark)是一款常用的压力测试工具.简单易用,ab的命令行一次只能支持一次测试.如果想要批量执行不同的测试方式,并自动对指标进行分析,那么单靠手工一条一条命令运行a ...

  4. shell 压测_【原】shell编写一个简单的jmeter自动化压测脚本

    在公司做压力测试也挺长时间了,每次测试前环境数据准备都需要话费较长时间,所以一直在考虑能不能将整个过程实现自动化进行,于是就抽空写了一个自动化脚本,当然这个脚本目前功能十分简陋,代码也不完善,很有很多 ...

  5. ab压力测试php脚本,ab压力测试工具-批量压测脚本

    ab(Apache benchmark)是一款经常使用的压力测试工具.简单易用,ab的命令行一次只能支持一次测试.若是想要批量执行不一样的测试方式,并自动对指标进行分析,那么单靠手工一条一条命令运行a ...

  6. Go 实现 json 格式定义 http 协议压测脚本

    原文由bugVanisher发表于TesterHome社区,点击原文链接可与作者直接交流. 前段时间,我主导推动组里实现了一套基于Locust+boomer的通用的压测平台,主要目的是满足我们组内的各 ...

  7. shell编写一个简单的jmeter自动化压测脚本

    在公司做压力测试也挺长时间了,每次测试前环境数据准备都需要话费较长时间,所以一直在考虑能不能将整个过程实现自动化进行,于是就抽空写了一个自动化脚本,当然这个脚本目前功能十分简陋,代码也不完善,很有很多 ...

  8. ab压力测试工具-批量压测脚本

    转自:https://www.cnblogs.com/exceptioneye/p/5179763.html ab(Apache benchmark)是一款常用的压力测试工具.简单易用,ab的命令行一 ...

  9. 记录一个ansible高级用法与shell结合

    未经本人同意不得转载 目录 一.ansible安装(ansible的配置与roles运用) 1.ansible的概述 2.安装python 3.ansible运用前准备 二.修改roles 1.初步修 ...

最新文章

  1. java程序运存扩容
  2. 第一章计算机基础知识作业答案,第一章 计算机基础知识.doc第一次作业
  3. 什么是Ext(ExtJs)【转载】
  4. iec60870-5-104通讯协议编程_三菱FX编程口通讯协议1——协议解读
  5. 软件设计师习题笔记-重点习题三
  6. 平台层-适配层-核心层|拆分环信ONE SDK架构
  7. 【翻译】Sencha Touch 2入门:创建一个实用的天气应用程序之一
  8. Mimics三维建模
  9. 【单片机/嵌入式】最完整学习路线
  10. Python-接口自动化(四)
  11. 【时空序列】使用3D卷积网络学习时空特征
  12. w ndows无法完成格式化,windows无法完成格式化,详细教您解决windows无法完成格式化U盘...
  13. 【朝花夕拾】Android自定义View篇之(十)移动阈值TouchSlop及滑动追踪VelocityTracker...
  14. 小米9es更新MIUI 11.0.3.0稳定版本,解决耗电问题
  15. Java集合——(通俗易懂)
  16. Vue $once 函数
  17. PHPexcel 导出身份证处理
  18. 常用文献管理软件比较
  19. 18_MySQL8其它新特性
  20. 利用nodejs对接232接口电子秤

热门文章

  1. Windows系统下搭建MPI环境
  2. Coursera课程Python for everyone:chapter3
  3. 你在发表理科学术文章过程中有哪些经验值得借鉴
  4. 多个类的DLL封装及调用
  5. 随机重命名MP3文件
  6. 自己发现的数学规律二
  7. 2013上半年-学习目录
  8. cocos2dx游戏开发——微信打飞机学习笔记(三)——WelcomeScene的搭建
  9. 软件成本度量方法及CMMI V2.0,你Get到了吗?
  10. intellij idea run configurations配置共享