使 Ansible 剧本中的一行可选

Make a line in an Ansible playbook optional

我已经构建了一个在 F5 中构建虚拟服务器的剧本。我想让一行只有在有人输入变量时才执行。在这种情况下,default_persistence_profile: 行有一个变量“{{ persistenceProfile }}”。有时开发人员不希望将持久性应用于他们的应用程序,但有时他们会这样做。我发现当我在 运行 任务中将变量设置为可选并且不 select 持久性配置文件时任务出错。请参阅下面的剧本:

    - 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

Ansible 有一个 omitting parameters 使用 default 过滤器的机制,像这样:

    - 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

如果未设置 persistenceProfile,则不应将 default_persistence_profile 参数传递给 bigip_virtual_server 模块。