2

I have a Ansible playbook (YAML) that deploys an web application and, as part of the deployment process, it creates and initializes various databases and some data in certain tables.

Playbook Task:

  - name: insert Meter Managers to mdm_connection
    shell : "psql -d {{ mdm_config_db }} -c \"insert into mdm_connection (mdm_connection_type_id, name, db_type, host, port, catalog, username, password) values (3, '{{ item.name }}', '{{ item.db_type }}', '{{ item.host }}', {{ item.port }}, '{{ item.catalog }}', '{{ item.username }}', '{{ item.password }}');\""
    with_items: "{{ meter_managers }}"
    when: item != ""
    sudo_user: postgres
    tags:
      - initDB

host_vars:

meter_managers:
  - name: SAMPLE
    db_type: ""
    host: "http://www.example.com/axis2/services/Example/"
    port: -1
    catalog: ""
    username: csa1
    password: "Example$Example"

You can ignore most of the parameters above, but the part that isn't working is the password field, as it contains a $ sign.

It comes out as Example, truncated after the $ sign.

I have tried to escape it as a double $$ instead, as per this link: How can I escape a $ dollar sign in a docker compose file?

However, that does not result in the right output. It comes out as

Example15887Example

Where the number in between is different each time I run my playbook. I have no idea where that number is coming from. It seems to be some kind of tick, or something like that, but the link seems to suggest that $$ is the way to escape a single $ and so I don't see why that's coming out like that.

I have also tried with and without enclosing " marks and also tried with or without ' marks, but to no avail.

Any idea as to how to properly escape this so that I can get the value Example$Example ready for insertion into my database table?

UPDATE:

The referenced question would not have answered my question without additional explanation, however Anthon has described the simplification below in comments and assisted far better in THIS question.

ManoDestra
  • 6,325
  • 6
  • 26
  • 50
  • did u try escaping with \ ?? – tux Jul 25 '17 at 20:37
  • @tux Yes. Tried that also. – ManoDestra Jul 25 '17 at 20:55
  • Possible duplicate of [Ansible regex escape dollar character](https://stackoverflow.com/questions/35717778/ansible-regex-escape-dollar-character) – Anthon Jul 26 '17 at 09:20
  • As I indicated in my comment on question you link to, the `$` has no special meaning in YAML. Since that dollar sign is not special (for YAML) adding single or double quotes has no effect at all. That docker expects `$$` is irrelevant when you are using ansible. – Anthon Jul 26 '17 at 09:25

2 Answers2

8

You feed the string with $ into bash.
In bash $ is a variable prefix.
And $$ is a special variable with current PID number, which is different every time you run playbook.

In bash when you use double quotes $ should appear as \$. I see you use double quotes in YAML as well, so you should try to escape slash as well, so try with password: "Example\\$Example".

Konstantin Suvorov
  • 65,183
  • 9
  • 162
  • 193
  • 1
    If you leave out the double quotes (which are not necessary here and therefore obfuscate instead of help), you don't even have to escape the backslash. – Anthon Jul 26 '17 at 09:32
  • @Anthon Thanks. That simplifies my host_vars. Much appreciated. The explanation you've given here is far better and more succinct than the other question you've linked to, which would not have corrected my issue without further explanation, but I appreciate your reference to it. The final solution for future reference for others was to simply make the parameter value thus: Example\$Example (no quotes around the value). – ManoDestra Jul 26 '17 at 18:23
1

It does not look like a YAML issue ; you are using the shell module and it seems your dollar sign is not sufficiently escaped. Try adding a \ in front of it in the host_vars or adding one more level of quoting in the shell line.

As for the numbers, they are the PID of the shell process. See this.

damienfrancois
  • 52,978
  • 9
  • 96
  • 110