Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can escape colon in a string within an Ansible YAML file?

I want to change one line of my code in file /var/www/kibana/config.js during installation from

elasticsearch: "http://"+window.location.hostname+":9200"

to

elasticsearch: "http://192.168.1.200:9200"

Here I tried to use lineinfile to do that as show below

- 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

I have set variables of {{elasticsearch_URL}} and {{elasticsearch_port}} to http://192.168.1.200 and 9200, respectively.

Here is the error message I met:

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 }} \" "
                    ^
like image 612
chen Avatar asked Jul 19 '14 00:07

chen


People also ask

How do you use a YAML Colon?

A colon followed by a space (or newline) ": " is an indicator for a mapping. A space followed by the pound sign " #" starts a comment. …and then the colon will be preserved. The list of allowed escapes can be found in the YAML Specification under “Escape Sequences” (YAML 1.1) or “Escape Characters” (YAML 1.2).

How do you escape the special characters in Ansible?

Yes, you need to escape '$' sign with '\', and it's executed in the server, it just won't show in the resulting output. Because in some cases like 'awk' with 'print' not working with Ansible ad-hoc command and need to utilize playbooks. It will spit out the result you want.


4 Answers

The solution that will work in any case no matter how many nested quotes you might have and without forcing you to add more quotes around the whole thing (which can get tricky to impossible depending on the line you want to write) is to output the colon through a Jinja2 expression, which simply returns the colon as a string:

{{ ":" }}

Or in your complete line:

line="elasticsearch\: \" {{ elasticsearch_URL }}{{ ":" }}{{ elasticsearch_port }} \" "

Credit to this goes to github user drewp.

like image 59
udondan Avatar answered Oct 24 '22 08:10

udondan


you need to enclose the entire line in ", where : appears.

lineinfile:
'dest=/var/www/kibana/config.js
backrefs=true
regexp="(elasticsearch.* \"http.*)$"
line="elasticsearch\: \ {{ elasticsearch_URL }}:{{ elasticsearch_port }} \ "
state=present'  

See these pages:
Link-1 Link-2 Link-3

like image 35
sunbabaphu Avatar answered Oct 24 '22 08:10

sunbabaphu


Just keep the colon in quotes separately -

regexp="(elasticsearch.* \"http.*)$" line="elasticsearch':' \" {{ elasticsearch_URL }}:{{ elasticsearch_port }} \" "

like image 43
theharshest Avatar answered Oct 24 '22 09:10

theharshest


foo=bar is the more suitable format for a one-line directive, but as you're already spanning several lines with your parameters anyway, just change the = to :, and it won't fuss about having a colon in your string.

- 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
like image 1
Chris Avatar answered Oct 24 '22 10:10

Chris