Error[8]: Undefined offset: 13, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 121
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

ansible高级用法(压测脚本

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

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                   # 指定默认的角色目录

[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                # 远程端口,经量不要用22

test_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[*]}
do
   echo $i
   sshpass -p '{{ ansible_ssh_pass }}' ssh $i iperf3 -s -D -p 30000 >> /dev/null  &
   sleep 20
   iperf3 -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% ];then
       echo -e "33[32m $c normal:$b%33[0m"
       echo -e "33[32m $c normal:$b%33[0m" >> /data/perfor-reports/iperf/iperf3.log
   else
       echo -e "33[31m$c Packet loss rate is $b% 33[0m" 
       echo "$c Packet loss rate is $b%" >> /data/perfor-reports/iperf/iperf3.log
   fi
   if [ $i == $k ];then
       continue
   else
       sshpass -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.txt
   fi
done

# 测试所有服务器下载上传
for i in ${IP[*]}
do
    sshpass -p '{{ ansible_ssh_pass }}' ssh $i speedtest-cli > ceshi.log
    d=`grep -E "Download|Upload" ceshi.log`
    echo -e "33[32m $i Download and Upload is $d33[0m"
    echo "$i Download and Upload is $d" >> /data/perfor-reports/iperf/iperf3.log
    rm -rf ceshi.log
done 

# 测试所有服务器ulimit
for i in ${IP[*]}
do
   sshpass -p '{{ ansible_ssh_pass }}' ssh $i ulimit -n > ceshi.log
   e=`cat ceshi.log`
   echo -e "33[32m $i ulimit is $e33[0m"
   echo "$i ulimit is $e" >> /data/perfor-reports/iperf/iperf3.log
   rm -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 facts
  set_fact: test_iperf_all = "{{ test_iperf_all }}"
  tags: test_iperf

# 下载软件包
- name: install rpm iperf3 and speedtest-cli
  yum:
    name: "{{ item }}"
  loop:
    - iperf3  
    - epel-release 
    - speedtest-cli
  when: test_iperf_all == "true" and inventory_hostname in groups ["all"]
  tags: test_iperf

# 创建脚本与文件日志的目录
- name: create iperf dir
  shell: ls /data/perfor-reports/iperf || mkdir -p /data/perfor-reports/iperf
  when: test_iperf_all == "true" and inventory_hostname in groups ["installer"]
  tags: test_iperf

# 拷贝脚本到目录
- name: copy iperf3.sh.j2
  template: 
     src: templates/iperf/iperf3.sh.j2
     dest: /data/perfor-reports/iperf/iperf3.sh
     mode: '0755'
  when: test_iperf_all == "true" and inventory_hostname in groups ["installer"]
  tags: test_iperf

# 拷贝ip文件
- name: copy groups.txt.j2
  template:
      src: templates/iperf/groups.txt.j2
      dest: /data/perfor-reports/iperf/groups.txt
  when: test_iperf_all == "true" and inventory_hostname in groups ["installer"]
  tags: test_iperf

# 执行文件
- name: deploy test iperf and speedtest-cli and ulimit
  shell: cd /data/perfor-reports/iperf/ && sh iperf3.sh
  when: 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 -e

base_DIR=$(cd `dirname 
~]# vim pot-cmd.sh
#!/bin/bash
# Author: kali
set -e
 
base_DIR=$(cd `dirname [+++]` && pwd)
cd $base_DIR
 
EXEC_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
do
  case $opt in
    s)
    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
done
 
cmd(){
   /root/ansible/${EXEC_script} -f ${CALL_FUN} -h ${hosts}
}
 
main(){
  if [ "x${EXEC_script}" == "x" ]; then
    help
  else
    cmd
  fi
}
main
~]# ./pot-cmd.sh -f xxx
show usage:
you can exec script list: 
hosts-check.sh
~]# ./pot-cmd.sh -s hosts-check.sh
` && pwd) cd $base_DIR CALL_FUN="all_func" hosts="all" help(){ echo "show usage:" echo "check_test_iperf" } while getopts ":f:h:" opt do case $opt in f) 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)书写集成

[+++]

 

)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 166, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
Error[8]: Undefined offset: 14, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 121
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

ansible高级用法(压测脚本

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

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                   # 指定默认的角色目录

[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                # 远程端口,经量不要用22

test_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[*]}
do
   echo $i
   sshpass -p '{{ ansible_ssh_pass }}' ssh $i iperf3 -s -D -p 30000 >> /dev/null  &
   sleep 20
   iperf3 -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% ];then
       echo -e "33[32m $c normal:$b%33[0m"
       echo -e "33[32m $c normal:$b%33[0m" >> /data/perfor-reports/iperf/iperf3.log
   else
       echo -e "33[31m$c Packet loss rate is $b% 33[0m" 
       echo "$c Packet loss rate is $b%" >> /data/perfor-reports/iperf/iperf3.log
   fi
   if [ $i == $k ];then
       continue
   else
       sshpass -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.txt
   fi
done

# 测试所有服务器下载上传
for i in ${IP[*]}
do
    sshpass -p '{{ ansible_ssh_pass }}' ssh $i speedtest-cli > ceshi.log
    d=`grep -E "Download|Upload" ceshi.log`
    echo -e "33[32m $i Download and Upload is $d33[0m"
    echo "$i Download and Upload is $d" >> /data/perfor-reports/iperf/iperf3.log
    rm -rf ceshi.log
done 

# 测试所有服务器ulimit
for i in ${IP[*]}
do
   sshpass -p '{{ ansible_ssh_pass }}' ssh $i ulimit -n > ceshi.log
   e=`cat ceshi.log`
   echo -e "33[32m $i ulimit is $e33[0m"
   echo "$i ulimit is $e" >> /data/perfor-reports/iperf/iperf3.log
   rm -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 facts
  set_fact: test_iperf_all = "{{ test_iperf_all }}"
  tags: test_iperf

# 下载软件包
- name: install rpm iperf3 and speedtest-cli
  yum:
    name: "{{ item }}"
  loop:
    - iperf3  
    - epel-release 
    - speedtest-cli
  when: test_iperf_all == "true" and inventory_hostname in groups ["all"]
  tags: test_iperf

# 创建脚本与文件日志的目录
- name: create iperf dir
  shell: ls /data/perfor-reports/iperf || mkdir -p /data/perfor-reports/iperf
  when: test_iperf_all == "true" and inventory_hostname in groups ["installer"]
  tags: test_iperf

# 拷贝脚本到目录
- name: copy iperf3.sh.j2
  template: 
     src: templates/iperf/iperf3.sh.j2
     dest: /data/perfor-reports/iperf/iperf3.sh
     mode: '0755'
  when: test_iperf_all == "true" and inventory_hostname in groups ["installer"]
  tags: test_iperf

# 拷贝ip文件
- name: copy groups.txt.j2
  template:
      src: templates/iperf/groups.txt.j2
      dest: /data/perfor-reports/iperf/groups.txt
  when: test_iperf_all == "true" and inventory_hostname in groups ["installer"]
  tags: test_iperf

# 执行文件
- name: deploy test iperf and speedtest-cli and ulimit
  shell: cd /data/perfor-reports/iperf/ && sh iperf3.sh
  when: 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 -e

base_DIR=$(cd `dirname 
~]# vim pot-cmd.sh
#!/bin/bash
# Author: kali
set -e
 
base_DIR=$(cd `dirname ` && pwd)
cd $base_DIR
 
EXEC_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
do
  case $opt in
    s)
    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
done
 
cmd(){
   /root/ansible/${EXEC_script} -f ${CALL_FUN} -h ${hosts}
}
 
main(){
  if [ "x${EXEC_script}" == "x" ]; then
    help
  else
    cmd
  fi
}
main
~]# ./pot-cmd.sh -f xxx
show usage:
you can exec script list: 
hosts-check.sh
~]# ./pot-cmd.sh -s hosts-check.sh
` && pwd) cd $base_DIR CALL_FUN="all_func" hosts="all" help(){ echo "show usage:" echo "check_test_iperf" } while getopts ":f:h:" opt do case $opt in f) 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)书写集成

[+++]

 

)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 166, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
ansible高级用法(压测脚本)_随笔_内存溢出

ansible高级用法(压测脚本)

ansible高级用法(压测脚本),第1张

ansible高级用法(压测脚本

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

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                   # 指定默认的角色目录

[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                # 远程端口,经量不要用22

test_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[*]}
do
   echo $i
   sshpass -p '{{ ansible_ssh_pass }}' ssh $i iperf3 -s -D -p 30000 >> /dev/null  &
   sleep 20
   iperf3 -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% ];then
       echo -e "33[32m $c normal:$b%33[0m"
       echo -e "33[32m $c normal:$b%33[0m" >> /data/perfor-reports/iperf/iperf3.log
   else
       echo -e "33[31m$c Packet loss rate is $b% 33[0m" 
       echo "$c Packet loss rate is $b%" >> /data/perfor-reports/iperf/iperf3.log
   fi
   if [ $i == $k ];then
       continue
   else
       sshpass -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.txt
   fi
done

# 测试所有服务器下载上传
for i in ${IP[*]}
do
    sshpass -p '{{ ansible_ssh_pass }}' ssh $i speedtest-cli > ceshi.log
    d=`grep -E "Download|Upload" ceshi.log`
    echo -e "33[32m $i Download and Upload is $d33[0m"
    echo "$i Download and Upload is $d" >> /data/perfor-reports/iperf/iperf3.log
    rm -rf ceshi.log
done 

# 测试所有服务器ulimit
for i in ${IP[*]}
do
   sshpass -p '{{ ansible_ssh_pass }}' ssh $i ulimit -n > ceshi.log
   e=`cat ceshi.log`
   echo -e "33[32m $i ulimit is $e33[0m"
   echo "$i ulimit is $e" >> /data/perfor-reports/iperf/iperf3.log
   rm -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 facts
  set_fact: test_iperf_all = "{{ test_iperf_all }}"
  tags: test_iperf

# 下载软件包
- name: install rpm iperf3 and speedtest-cli
  yum:
    name: "{{ item }}"
  loop:
    - iperf3  
    - epel-release 
    - speedtest-cli
  when: test_iperf_all == "true" and inventory_hostname in groups ["all"]
  tags: test_iperf

# 创建脚本与文件日志的目录
- name: create iperf dir
  shell: ls /data/perfor-reports/iperf || mkdir -p /data/perfor-reports/iperf
  when: test_iperf_all == "true" and inventory_hostname in groups ["installer"]
  tags: test_iperf

# 拷贝脚本到目录
- name: copy iperf3.sh.j2
  template: 
     src: templates/iperf/iperf3.sh.j2
     dest: /data/perfor-reports/iperf/iperf3.sh
     mode: '0755'
  when: test_iperf_all == "true" and inventory_hostname in groups ["installer"]
  tags: test_iperf

# 拷贝ip文件
- name: copy groups.txt.j2
  template:
      src: templates/iperf/groups.txt.j2
      dest: /data/perfor-reports/iperf/groups.txt
  when: test_iperf_all == "true" and inventory_hostname in groups ["installer"]
  tags: test_iperf

# 执行文件
- name: deploy test iperf and speedtest-cli and ulimit
  shell: cd /data/perfor-reports/iperf/ && sh iperf3.sh
  when: 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 -e

base_DIR=$(cd `dirname 
~]# vim pot-cmd.sh
#!/bin/bash
# Author: kali
set -e
 
base_DIR=$(cd `dirname ` && pwd)
cd $base_DIR
 
EXEC_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
do
  case $opt in
    s)
    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
done
 
cmd(){
   /root/ansible/${EXEC_script} -f ${CALL_FUN} -h ${hosts}
}
 
main(){
  if [ "x${EXEC_script}" == "x" ]; then
    help
  else
    cmd
  fi
}
main
~]# ./pot-cmd.sh -f xxx
show usage:
you can exec script list: 
hosts-check.sh
~]# ./pot-cmd.sh -s hosts-check.sh
` && pwd) cd $base_DIR CALL_FUN="all_func" hosts="all" help(){ echo "show usage:" echo "check_test_iperf" } while getopts ":f:h:" opt do case $opt in f) 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)书写集成

 

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5694965.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存