-1

I am trying to fill an ansible variable with:

  • "docker-ce" if on RedHat
  • "docker" otherwise

the following works, but is not very clean:

docker_edition: 'ce'
docker_package: docker{{ "-" if (ansible_os_family == "RedHat") else "" }}{{ docker_edition if (ansible_os_family == "RedHat") else "" }}

I would like to write this in a single ternary expression, I have tried the following:

docker_package: "docker{{ (ansible_os_family == "RedHat") | ternary("","-{{ docker_edition }}") }}"
docker_package: docker{{ (ansible_os_family == "RedHat") | ternary("","-{{ docker_edition }}") }}
docker_package: docker{{ -{{ docker_edition }} if (ansible_os_family == "RedHat") else "" }}

but I fail to get this working.

For clarification, the following would work:

{{ "docker-ce" if (ansible_os_family == "RedHat") else "docker" }}

but I want to keep the docker_edition variable separate since it is used elsewhere.

Chris Maes
  • 35,025
  • 12
  • 111
  • 136
  • You "fail to get it working" ... because? It produces an error message? If so, what error? As for combining the ternary, isn't `docker_package: docker{{ ("-" + docker_edition) if ansible_os_family == "RedHat" else "" }}` what you're after? – mdaniel Mar 18 '20 at 21:10
  • @mdaniel: It seems that, when posting the question, I accidentally posted the answer (without knowing). I edited my question to show all possible combinations I tried before. – Chris Maes Mar 19 '20 at 06:50

1 Answers1

0

the following works:

docker_package: docker{{ "-" + docker_edition if (ansible_os_family == "RedHat") else "" }}
Chris Maes
  • 35,025
  • 12
  • 111
  • 136