ansible自动化测试云平台多个网络角色间带宽(shell模块调用iperf)

          云平台的一个网卡,可能承担多个网络角色,如同时走管理网流量,存储网流量等。一般部署完成后,需要测试各个网络间的带宽,看下是否有网卡掉速问题。如果网段较多,手工一个个来测试比较费时,特写了一个ansible playbook(iperf的安装未包含),调用iperf自动化测试各个网络带宽,yaml如下


---
- name: iptables and iperf server start
  hosts: all
  remote_user: root
  become: yes
  gather_facts: false
  pre_tasks:
    - name: open all hosts 5001 port    #打开防火墙
      iptables:
        action: insert
        chain: INPUT
       # match: multiport
        destination_port: 5001
        jump: ACCEPT
        state: present
        protocol: tcp
    - name: iperf dameon start
      shell:
        iperf -s -D     #启动iperf server端,ansible inventory文件需要controller组.
      when: inventory_hostname ==  groups.controller[0]

- name: begin to check the net speed
  hosts: all
  vars:
    time: 5         #iperf参数,根据实际情况调整
    interval: 1
  serial: 1
  tasks:
    - block:
      - name: iperf client
        shell:
          iperf  -c "{{ hostvars[groups.controller[0]]['ansible_br_'+item].ipv4.address }}"    -i {{ interval }}  -t  {{time}}
        with_items:      # iperf 需要测试的网络角色
          - mgmt
          - storage
          - storagepub
          - roller
          - vxlan
        register: netSpeed
      - name: echo the netIperf results     # 保存iperf结果,后面保存的路径根据实际情况调整
        shell: |
          echo -e  "{{inventory_hostname}} the net {{item.item}} iperf result" >> /root/netSpeedResult.txt
          echo -e "{{item.stdout}} " >> /root/netSpeedResult.txt
        with_items:
          "{{ netSpeed.results }}"
        delegate_to: localhost
      when: inventory_hostname not in  groups.controller[0]

- name: iptables and iperf server stop
  hosts: all
  remote_user: root
  become: yes
  gather_facts: false
  post_tasks:           #关闭防火墙和iperf 的server端
    - name: stop all hosts 5001 port
      iptables:
        action: insert
        chain: INPUT
       # match: multiport
        destination_port: 5001
        jump: ACCEPT
        state: absent
        protocol: tcp
    - name: iperf dameon  stop
      shell:
        ps aux |grep iperf|grep -v "grep"|awk '{print $2}'|xargs kill -9
      when: inventory_hostname ==  groups.controller[0]


示例inventory文件

[controller]
node-1
node-2
node-3

[compute]
node-4


收集的iperf测试结果如下图所示:



不足:
 1 iperf为单向测试,使用iperf -r进行双向测试导致iperf server端自动退出,暂未找出解决办法
2  gather_fact影响速度
3 通用性需要优化.

原文地址:https://www.cnblogs.com/360linux/p/13062088.html