ansiblef5

Make a line in an Ansible playbook optional


I have built a playbook to build a virtual server in F5. I want to make a line only execute if someone enters the variable. In this case the default_persistence_profile: line has a variable "{{ persistenceProfile }}". Sometimes the developers don't want persistence applied to their app but sometimes they do. I have found when I make the variable optional in the run task and don't select a persistence profile the task errors out. See playbook below:

    - name: Build the Virtual Server
      bigip_virtual_server:
        state: present
        partition: Common
        name: "{{ vsName  }}" 
        destination: "{{ vsIpAddress }}"
        port: "{{ vsPort }}"
        pool: "{{ poolName }}"
        default_persistence_profile: "{{ persistenceProfile }}"
        ip_protocol: tcp 
        snat: automap
        description: "{{ vsDescription }}"
        profiles:
           - tcp
           - http
           - name: "{{ clientsslName }}" 
             context: client-side
           - name: default-server-ssl
             context: server-side

Solution

  • Ansible has a mechanism for omitting parameters using the default filter, like this:

        - name: Build the Virtual Server
          bigip_virtual_server:
            state: present
            partition: Common
            name: "{{ vsName  }}" 
            destination: "{{ vsIpAddress }}"
            port: "{{ vsPort }}"
            pool: "{{ poolName }}"
            default_persistence_profile: "{{ persistenceProfile|default(omit) }}"
            ip_protocol: tcp 
            snat: automap
            description: "{{ vsDescription }}"
            profiles:
               - tcp
               - http
               - name: "{{ clientsslName }}" 
                 context: client-side
               - name: default-server-ssl
                 context: server-side
    

    If persistenceProfile is unset, the default_persistence_profile parameter should not be passed to the bigip_virtual_server module.