Is there is the way to skip ansible list parameter if provided list is empty?

Viewed 36

I'm trying to use ansible to run docker, one of parameters is ports which is a list. Is there a way to NOT SET IT UP if provided list is empty? I know that I can separate it based on when parameter, but my target is to have single common step for that: default(omit) works for dictionary, not for list

- name: Redeploy {{ service.name }} container
  docker_container:
      name: "{{ sservice.name }}"
      image: "{{ service.image['name'] }}:{{ service.image['version'] }}"
      state: stopped
      networks:
        - name: "{{ docker_network }}"
      env: "{{ sservice.config['environment'] | default(omit) }}"
      ports: "{{ service.config['ports'] | default(omit) }}"
      log_driver: "json-file"
      log_options:
          max-size: "10m"
      pull: false
      recreate: yes
      restart_policy: always
2 Answers

My problem was that in my configuration I was passing an empty list to docker_container module what leaded to en error FAILED! => {"changed": false, "msg": "Invalid port: \"\""}

When ports is not set, the value is omitted as expected. Kudos to @Zeitounator !

Related