Forum Discussion

Mike_Brandt's avatar
Mike_Brandt
Icon for Nimbostratus rankNimbostratus
Jul 14, 2023

Convert iRule File to use in curl command

We store all our iRules in a git repo as regular text files.  I'd like to be able to take the file and install it to the BigIP box using curl to the REST API...what's the best way to do this?

  • Hi Mike, 

    With help of curl command you can send irule to F5 box but you need to lot of background work. Example:

    curl -sku admin:<password> -H "Content-Type: application/json" -X POST https://<BIG-IP_host>/mgmt/tm/ltm/rule -d '{"name":"<iRule_name>","apiAnonymous":"<iRule_content>"}' | jq .
    
    i.e.
    
    curl -sku admin:testing -H "Content-Type: application/json" -X POST https://LTM1.example.com/mgmt/tm/ltm/rule -d '{"name":"example_rule","apiAnonymous":"when HTTP_REQUEST {\n  if { [HTTP::uri] ends_with \"test\" } {\n    pool test_pool\n  }\n}"}' | jq .
    

    Output similar to this...

    The output looks similar to the following example:
    
    {
      "kind": "tm:ltm:rule:rulestate",
      "name": "example_rule",
      "partition": "Common",
      "fullPath": "/Common/example_rule",
      "generation": 2184,
      "selfLink": "https://BIGIP_IP/mgmt/tm/ltm/rule/~Common~example_rule?ver=15.1.0",
      "apiAnonymous": "when HTTP_REQUEST {\n  if { [HTTP::uri] ends_with \"test\" } {\n    pool test_pool\n  }\n}"
    }
    
    

    I would suggest you to use ansible playbook to copy irule to f5 bigip.

    ---
    #
    # For More Irule help - https://www.linkedin.com/in/welcomesamir/
    #
    - name: BIG-IP SETUP
      hosts: lb
      connection: local
      gather_facts: false
    
      vars:
       irules: ['testirule','test2_irule']
    
      tasks:
    
      - set_fact:
         provider:
          server: "{{private_ip}}"
          user: "{{ansible_user}}"
          password: "{{ansible_ssh_pass}}"
          server_port: 443
          validate_certs: no
    
      - name: ADD iRules
        bigip_irule:
          provider: "{{provider}}"
          module: "ltm"
          name: "{{item}}"
          content: "{{lookup('file','{{item}}')}}"
        with_items: "{{irules}}"

    thanks

    • Mike_Brandt's avatar
      Mike_Brandt
      Icon for Nimbostratus rankNimbostratus

      Thanks Samir !
      I was actually looking to see if anyone knew of some command line foo to convert the irule contents to be used as the JSON value aligned with the "apiAnonymous" key.  Is there a way to use something like sed to insert '\n' characters as well as do any necessary quotation escapes in order to push the file that way.