Quotes in ansible lineinfile

前端 未结 4 724
灰色年华
灰色年华 2020-12-10 11:30

When I use lineinfile in ansible it is not writing \', \" characters lineinfile: \'dest=/home/xyz state=present line=\"CACHES=\"

4条回答
  •  自闭症患者
    2020-12-10 12:23

    Ansible 1.9.2 contains a bug (https://github.com/ansible/ansible/issues/10864), which fails to insert escaped double quotes at the beginning or end of the line.

    E.g., the following

    - name: /home/core/linetest
      lineinfile: dest="/home/core/linetest" line="\"ma\"ok\"in\""
    

    will result in missing first and last double-quotes (even though you escaped it).

    #/home/core/linetest
    ma"ok"in
    

    To compensate for this bug, you could add a PREFIX to the starting and ending double quotes, and subsequently removing it.

    - name: PREFIX first and last escaped double quotes with 'KUCF'
      lineinfile: dest="/home/core/linetest" line="KUCF\"main\"KUCF"
    
    - name: remove 'KUCF' PREFIX
      replace: dest="/home/core/linetest" regexp="KUCF" replace=""
    

    which should give you

    #/home/core/linetest
    "main"
    

    Make sure that your chosen PREFIX will never be used in the context of the destination file. In general, the longer and more random the PREFIX, the less likely it will clash with existing content in your destination file.

    Alternatively, you could upgrade your Ansible to the latest branch.

提交回复
热议问题