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

前端 未结 5 1859
無奈伤痛
無奈伤痛 2020-12-03 06:47

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


        
相关标签:
5条回答
  • 2020-12-03 07:23

    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.

    0 讨论(0)
  • 2020-12-03 07:26

    Just keep the colon in quotes separately -

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

    0 讨论(0)
  • 2020-12-03 07:29

    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

    0 讨论(0)
  • 2020-12-03 07:29

    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
    
    0 讨论(0)
  • 2020-12-03 07:35

    It’s a string already; you don’t have to (and can’t, as seen here) escape colons inside it.

    line="elasticsearch: \" {{ elasticsearch_URL }}:{{ elasticsearch_port }} \" "
    
    0 讨论(0)
提交回复
热议问题