How to safely pass variables that may contain special characters to a docker-compose file in Ansible

Viewed 1305

I am currently using an ansible script to deploy a docker-compose file (using the docker_service module), which sets a series of environment variables which are read by the .NET Core service running inside the docker container, like this:

(...)
environment:
   - Poller:Username={{ poller_username }}
   - Poller:Password={{ poller_password }}
(...)

The variables for poller_username and poller_password are being loaded from an Ansible Vault (which will be moved to a Hashicorp Vault eventually), and are interpolated into the file with no problem.

However, I have come across a scenario where this logic fails: the user has a '$' in the middle of his password. This means that instead of the environment variable being set to 'abc$123' it's instead set to 'abc', causing my application to fail.

Upon writing a debug command, I get the password output to the console correctly. If I do docker exec <container_name> env I get the wrong password.

Is there a Jinja filter I can use to ensure the password is compliant with docker-compose standards? It doesn't seem viable to me to guarantee the password will never have a $.

EDIT: {{ poller_password | replace("$","$$") }} works, but this isn't a very elegant solution to have in, potentially, every variable I use in the docker-compose module.

2 Answers

For this particular scenario, the {{ poller_password | replace("$","$$") }} solution seems to be inevitable. Thankfully, it appears to be the only case that requires this caution.

Had a similar situation was not a $ but some other character, end up using

something: !unsafe "{{ variable }}"

couldn't find a better way.

Related