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

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

问题描述

我有一个Ansible剧本(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.

Playbook任务:

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

关于如何正确地对此进行转义的任何想法,这样我就可以将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编号的特殊变量,每次运行剧本时都会不同.

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天全站免登陆