如何在 Ansible host_vars 文件中转义 $ 符号 [英] How to escape a $ sign in an Ansible host_vars file

查看:23
本文介绍了如何在 Ansible host_vars 文件中转义 $ 符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Ansible playbook (YAML),用于部署 Web 应用程序,作为部署过程的一部分,它会创建和初始化各种数据库以及某些表中的一些数据.

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.

剧本任务:

  - 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.

我试图将它转义为双 $$,根据此链接:如何在 docker compose 文件中转义 $ 美元符号?

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.

关于如何正确地转义它以便我可以将值 Example$Example 准备好插入到我的数据库表中的任何想法?

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?

更新:

如果没有额外的解释,引用的问题不会回答我的问题,但是 Anthon 在评论中描述了下面的简化,并在这个问题中提供了更好的帮助.

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.

推荐答案

您将带有 $ 的字符串输入 bash.
在 bash 中 $ 是一个变量前缀.
$$是一个带有当前PID号的特殊变量,每次运行playbook都不一样.

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.

在 bash 中,当您使用双引号时 $ 应显示为 \$.我看到你在 YAML 中也使用了双引号,所以你也应该尝试转义斜杠,所以尝试使用 password: "Example\\$Example".

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".

这篇关于如何在 Ansible host_vars 文件中转义 $ 符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆