Power of tmsh commands using Ansible

Why is data important

Having accurate data has become an integral part of decision making. The data could be for making simple decisions like purchasing the newest electronic gadget in the market or for complex decisions on what hardware and/or software platform works best for your highly demanding application which would provide the best user experience for your customer. In either case research and data collection becomes essential.

Using what kind of F5 hardware and/or software in your environment follows the same principals where your IT team would require data to make the right decision. Data could vary from CPU, Throughput and/or Memory utilization etc. of your F5 gear. It could also be data just for a period of a day, a month or a year depending the application usage patterns.

Ansible to the rescue

Your environment could have 10's or maybe 100 or even 1000's of F5 BIG-IP's in your environment, manually logging into each one to gather data would be a highly inefficient method. One way which is a great and simple way could be to use Ansible as an automation framework to perform this task, relieving you to perform your other job functions. Let's take a look at some of the components needed to use Ansible.

An inventory file in Ansible defines the hosts against which your playbook is going to run. Below is an example of a file defining F5 hosts which can be expanded to represent your 10'/100's or 1000's of BIG-IP's.

Inventory file: 'inventory.yml'

[f5]
ltm01 password=admin server=10.192.73.xxx user=admin validate_certs=no server_port=443
ltm02 password=admin server=10.192.73.xxx user=admin validate_certs=no server_port=443
ltm03 password=admin server=10.192.73.xxx user=admin validate_certs=no server_port=443
ltm04 password=admin server=10.192.73.xxx user=admin validate_certs=no server_port=443
ltm05 password=admin server=10.192.73.xxx user=admin validate_certs=no server_port=443

A playbook defines the tasks that are going to be executed. In this playbook we are using the bigip_command module which can take as input any BIG-IP tmsh command and provide the output. Here we are going to use the tmsh commands to gather performance data from the BIG-IP's. The output from each of the BIG-IP's is going to be stored in a file that can be referenced after the playbook finished execution.

Playbook: 'performance-data/yml'

---
- name: Create empty file
  hosts: localhost
  gather_facts: false

  tasks:

  - name: Creating an empty file
    file:
     path: "./{{filename}}"
     state: touch

- name: Gather stats using tmsh command
  hosts: f5
  connection: local
  gather_facts: false
  serial: 1

  tasks:

  - name: Gather performance stats
    bigip_command:
     provider:
      server: "{{server}}"
      user: "{{user}}"
      password: "{{password}}"
      server_port: "{{server_port}}"
      validate_certs: "{{validate_certs}}"
     commands:
      - show sys performance throughput historical
      - show sys performance system historical
    register: result

  - lineinfile:
     line: "\n###BIG-IP hostname => {{ inventory_hostname }} ###\n"
     insertafter: EOF
     dest: "./{{filename}}"

  - lineinfile:
     line: "{{ result.stdout_lines }}"
     insertafter: EOF
     dest: "./{{filename}}"

  - name: Format the file
    shell:
     cmd: sed 's/,/\n/g' ./{{filename}} > ./{{filename}}_formatted

  - pause:
     seconds: 10

- name: Delete file
  hosts: localhost
  gather_facts: false

  tasks:

  - name: Delete extra file created (delete file)
    file:
     path: ./{{filename}}
     state: absent

Execution:

The execution command will take as input the playbook name, the inventory file as well as the filename where the output will be stored. (There are different ways of defining and passing parameters to a playbook, below is one such example)

ansible-playbook performance_data.yml -i inventory.yml  --extra-vars "filename=perf_output"

Snippet of expected output:

###BIG-IP hostname => ltm01 ###

[['Sys::Performance Throughput'
 '-----------------------------------------------------------------------'
 'Throughput(bits)(bits/sec)   Current  3 hrs 24 hrs 7 days 30 days'
 '-----------------------------------------------------------------------'
 'Service             223.8K 258.8K 279.2K 297.4K  112.5K'
 'In               212.1K 209.7K 210.5K 243.6K  89.5K'
 'Out               21.4K  21.0K  21.1K  57.4K  30.1K'
 '               '
 '-----------------------------------------------------------------------'
 'SSL Transactions        Current  3 hrs 24 hrs 7 days 30 days'
 '-----------------------------------------------------------------------'
 'SSL TPS               0    0    0    0    0'
 '               '
 '-----------------------------------------------------------------------'
 'Throughput(packets)(pkts/sec) Current  3 hrs 24 hrs 7 days 30 days'
 '-----------------------------------------------------------------------'
 'Service               79   82   83   63    62'
 'In                 41   40   40   34    32'
 'Out                 41   40   40   32    34']
 ['Sys::Performance System'
 '------------------------------------------------------------'
 'System CPU Usage(%) Current 3 hrs 24 hrs 7 days 30 days'
 '------------------------------------------------------------'
 'Utilization        17   18   18   18    17'
 '          '
 '------------------------------------------------------------'
 'Memory Used(%)    Current 3 hrs 24 hrs 7 days 30 days'
 '------------------------------------------------------------'
 'TMM Memory Used      10   10   10   10    10'
 'Other Memory Used     55   55   54   54    53'
 'Swap Used         0   0    0    0    0']]

###BIG-IP hostname => ltm02 ###

[['Sys::Performance Throughput'
 '-----------------------------------------------------------------------'
 'Throughput(bits)(bits/sec)   Current  3 hrs 24 hrs 7 days 30 days'
 '-----------------------------------------------------------------------'
 'Service             202.3K 258.7K 279.2K 297.4K  112.5K'
 'In               190.8K 209.7K 210.5K 243.6K  89.5K'
 'Out               19.6K  21.0K  21.1K  57.4K  30.1K'
 '               '
 '-----------------------------------------------------------------------'
 'SSL Transactions        Current  3 hrs 24 hrs 7 days 30 days'
 '-----------------------------------------------------------------------'
 'SSL TPS               0    0    0    0    0'
 '               '
 '-----------------------------------------------------------------------'
 'Throughput(packets)(pkts/sec) Current  3 hrs 24 hrs 7 days 30 days'
 '-----------------------------------------------------------------------'
 'Service               77   82   83   63    62'
 'In                 39   40   40   34    32'
 'Out                 37   40   40   32    34']
 ['Sys::Performance System'
 '------------------------------------------------------------'
 'System CPU Usage(%) Current 3 hrs 24 hrs 7 days 30 days'
 '------------------------------------------------------------'
 'Utilization        21   18   18   18    17'
 '          '
 '------------------------------------------------------------'
 'Memory Used(%)    Current 3 hrs 24 hrs 7 days 30 days'
 '------------------------------------------------------------'
 'TMM Memory Used      10   10   10   10    10'
 'Other Memory Used     55   55   54   54    53'
 'Swap Used         0   0    0    0    0']]

The data obtained is historical data over a period of time. Sometimes it is also important to gather the peak usage of throughout/memory/cpu over time and not the average. Stay tuned as we will discuss on how to obtain that information in a upcoming article.

Conclusion

Use the output of the data to learn the traffic patterns and propose the most appropriate BIG-IP hardware/software in your environment. This could be data collected directly in your production environment or a staging environment, which would help you make the decision on what purchasing strategy gives you the most value from your BIG-IP's.

For reference: https://www.f5.com/pdf/products/big-ip-local-traffic-manager-ds.pdf

The above is one example of how you can get started with using Ansible and tmsh commands. Using this method you can potentially achieve close to 100% automation on the BIG-IP.

Published Feb 09, 2021
Version 1.0
  • Thanks for sharing.I found a lot of interesting information here. A really good post, very thankful and hopeful that you will write many more posts like this one.

     

  • Thanks. You should next for Ansible and AS3 (Application Services 3 Extension), basiclly using iApps remotely.

  • Payal_S's avatar
    Payal_S
    Ret. Employee

    Thanks for the suggestion Nikoolay. This is work in progress with some sample code placed here: https://github.com/f5devcentral/f5-bd-ansible-usecases/tree/development. Disclaimer this code is still being tested. Thanks.