[英]How can escape colon in a string within an Ansible YAML file?
我想在安裝過程中更改文件 /var/www/kibana/config.js 中的一行代碼
elasticsearch: "http://"+window.location.hostname+":9200"
到
elasticsearch: "http://192.168.1.200:9200"
在這里,我嘗試使用 lineinfile 來做到這一點,如下所示
- name: Comment out elasticsearch the config.js to ElasticSearch server
lineinfile:
dest=/var/www/kibana/config.js
backrefs=true
regexp="(elasticsearch.* \"http.*)$"
line="elasticsearch\: \" {{ elasticsearch_URL }}:{{ elasticsearch_port }} \" "
state=present
我已將{{elasticsearch_URL}}
和{{elasticsearch_port}}
變量分別設置為http://192.168.1.200
和9200
。
這是我遇到的錯誤消息:
ERROR: Syntax Error while loading YAML script, /Users/shuoy/devops_workspace/ansible_work/logging-for-openstack/roles/kibana/tasks/Debian.yml
Note: The error may actually appear before this position: line 29, column 25
regexp="(elasticsearch.* \"http.*)$"
line="elasticsearch\: \" {{ elasticsearch_URL }}:{{ elasticsearch_port }} \" "
^
無論您有多少嵌套引號,在任何情況下都有效的解決方案是輸出冒號通過 Jinja2 表達式,它簡單地將冒號作為字符串返回:
{{ ":" }}
或者在您的完整行中:
line="elasticsearch\: \" {{ elasticsearch_URL }}{{ ":" }}{{ elasticsearch_port }} \" "
只需將冒號單獨放在引號中 -
regexp="(elasticsearch.* \\"http.*)$" line="elasticsearch':' \\" {{ elasticsearch_URL }}:{{ elasticsearch_port }} \\" "
foo=bar
是更適合單行指令的格式,但由於您的參數已經跨越了多行,只需將=
更改為:
,就不會在字符串中包含冒號而大驚小怪。
- name: Comment out elasticsearch the config.js to ElasticSearch server
lineinfile:
dest: /var/www/kibana/config.js
backrefs: true
regexp: 'elasticsearch.* "http.*$'
line: 'elasticsearch: "{{ elasticsearch_URL }}:{{ elasticsearch_port }}"'
state: present
它已經是一個字符串; 你不必(也不能,如這里所見)在其中轉義冒號。
line="elasticsearch: \" {{ elasticsearch_URL }}:{{ elasticsearch_port }} \" "
我發現在所有情況下都能始終如一地工作的是一個變量。 例如, vars/main.yml
:
fw_zone_str: 'Error: NAME_CONFLICT: new_zone():'
在tasks/foo.yml
:
failed_when: fw_zone_str not in fw_new_zone.stderr
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.