Forum Discussion

dani_martinez_2's avatar
dani_martinez_2
Icon for Nimbostratus rankNimbostratus
May 30, 2018

Activate a Policy through F5 Common Python

I tried to activate a policy, which is inactive once is created this way: policy.modify(active=True) Obviously it is worng as I have guessed from the answer, which is this one: '{"code":401,"message":"Policy must be applied and/or activated by a Task"

 

What are the tasks and I how can I have a code to do this? Thanks in advance

 

  • The F5 Python SDK Documentation is the best starting point for diving into details.

    Specifically the tasks page points out the available tasks.

    This is a quick example in python3 to create the task to apply/activate the policy from the python command line.

    For the example all policies are being printed out for comparison.

    Find the policy state and the selfLink, here the target policy is asm_http_base_2:

    _policies = mgmt.tm.asm.policies_s.get_collection()
    
    for _policy_Id in range(len(_policies)):
     print('Policy Name: {}\tPolicy Active State: {}\tPolicy Link: {}'.format(_policies[_policy_Id].name,  _policies[_policy_Id].active, _policies[_policy_Id].selfLink))
    
    
    Policy Name: asm_http_base  Policy Active State: True   Policy Link: https://localhost/mgmt/tm/asm/policies/f6ipRwR5MSYfMVgE611xRg?ver=12.1.3
    Policy Name: asm_http_base_2    Policy Active State: False  Policy Link: https://localhost/mgmt/tm/asm/policies/Fqpvw5rn1SzYqgrCR9CA1Q?ver=12.1.3
    
    

    With the selfLink from the previous command create the apply_policy task:

    mgmt.tm.asm.tasks.apply_policy_s.apply_policy.create(policyReference={"link": "https://localhost/mgmt/tm/asm/policies/Fqpvw5rn1SzYqgrCR9CA1Q"})
    

    Recollect the policy states and verify the task has made the policy active:

    _policies = mgmt.tm.asm.policies_s.get_collection()
    
    for _policy_Id in range(len(_policies)):
     print('Policy Name: {}\tPolicy Active State: {}\tPolicy Link: {}'.format(_policies[_policy_Id].name,  _policies[_policy_Id].active, _policies[_policy_Id].selfLink))
    
    
    Policy Name: asm_http_base  Policy Active State: True   Policy Link: https://localhost/mgmt/tm/asm/policies/f6ipRwR5MSYfMVgE611xRg?ver=12.1.3
    Policy Name: asm_http_base_2    Policy Active State: True   Policy Link: https://localhost/mgmt/tm/asm/policies/Fqpvw5rn1SzYqgrCR9CA1Q?ver=12.1.3
    
    

    This should get you started and let me know if you need any clarification or additional information.