3

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.

José Maia
  • 310
  • 5
  • 21
  • https://stackoverflow.com/questions/47101789/dealing-with-a-password-with-special-characters-in-ansible – error404 Jun 12 '19 at 13:44
  • Try encapsulating the jinja2 templates with quotes: ```...="{{ poller_username }}"``` – Nick Jun 12 '19 at 14:06
  • Encapsulating with quotes won't work, as the environment variables themselves will get interpreted as if they have "", i.e. the password will be assumed to be the 9 character password "abc$123". – José Maia Jun 12 '19 at 14:23

2 Answers2

1

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.

José Maia
  • 310
  • 5
  • 21
0

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

something: !unsafe "{{ variable }}"

couldn't find a better way.

Ntwobike
  • 2,406
  • 1
  • 21
  • 27