我在Ansible剧本中使用了一些lineinfile,效果很好,但我最近添加的内容出现了语法错误,我不知道如何解决它。
工作代码:
community.windows.win_lineinfile:
path: C:PowerToolsConfigConfig.psd1
backrefs: yes
regex: '^(.*)apiBase(.*)$'
line: ' apiBase = "http://{{ app_server }}/dm-rest-services/api"'
失败代码:
community.windows.win_lineinfile:
path: C:PowerToolsConfigConfig.psd1
backrefs: yes
regex: '^(.*)connectionString(.*)$'
line: ' connectionString = "Data Source={{ db_server }},1433;Initial Catalog={{ db_name }};User Id={{ db_user }}; Password={{ dm_db_password['secrets'][0]['secret'] }}"'
我得到以下通用语法错误:
ERROR! We were unable to read either as JSON nor YAML, these are the errors we got from each:
JSON: Expecting value: line 1 column 1 (char 0)
Syntax Error while loading YAML.
expected <block end>, but found '<scalar>'
The error appears to be in '/root/gitlab-ansible/roles/fico_etl/tasks/powertools.yml': line 25, column 152, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
regex: '^(.*)connectionString(.*)$'
line: ' connectionString = "Data Source={{ db_server }},1433;Initial Catalog={{ db_name }};User Id={{ db_user }}; Password={{ dm_db_password['secrets'][0]['secret'] }}"'
^ here
We could be wrong, but this one looks like it might be an issue with
missing quotes. Always quote template expression brackets when they
start a value. For instance:
with_items:
- {{ foo }}
Should be written as:
with_items:
- "{{ foo }}"
任何指导都将不胜感激。
问题是在整行中使用单引号('(,但在'secrets'
和'secret'
周围的行中也使用单引号。
在您的情况下,您需要在整行周围使用双引号,并用反斜杠转义行内的双引号
community.windows.win_lineinfile:
path: C:PowerToolsConfigConfig.psd1
backrefs: yes
regex: '^(.*)connectionString(.*)$'
line: " connectionString = "Data Source={{ db_server }},1433;Initial Catalog={{ db_name }};User Id={{ db_user }}; Password={{ dm_db_password['secrets'][0]['secret'] }}""