ansible高级用法(压测脚本)
生活随笔
收集整理的這篇文章主要介紹了
ansible高级用法(压测脚本)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
記錄一個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 stop2)配置文件配置
~]# 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.984)測試
~]# 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 done3)創建要測試的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_iperf5)調用yml文件
~]# cd /root/ansible/roles/hosts-check/tasks ~]# vim main.yml --- # tasks file for roles/hosts-check - include: check-iperf3.yml6)書寫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-check7)測試
~]# ansible-playbook playbook/hosts-check.yml8)書寫單個腳本函數
~]# 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.sh9)書寫集成
~]# 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高级用法(压测脚本)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 系统网卡性能,丢包率与下载上传,ulim
- 下一篇: 容器技术概述