ansible lineinfile 轉義引號遺失

ansible lineinfile 轉義引號遺失

使用ansible 1.5.4,指令

lineinfile: dest=/etc/bash.bashrc line="bind '\"\e[B\"':history-search-forward"

新增了行

bind '"\e[B"':history-search-forward

完美地/etc/bash.bashrc罰款。

在 ansible 1.9.1 上,相同的 ansible 指令會產生以下行,這會混淆我在 lineinfile 指令中轉義的引號:

bind \e[B:history-search-forward

如何修復引號,以便添加正確的行

答案1

您通常可以透過使用多行輸入形式來避免一些轉義混亂,尤其是在輸入冒號時:

command: > curl -X POST -H 'Content-Type: application/json' --data '{"name": "{{ item.name }}", "comment": "{{ item.comment }}", "DefaultDistribution": "{{ item.default_distribution }}", "DefaultComponent": "{{ item.default_component }}" }' http://localhost:8080/api/repos

答案2

我寫了一個小小的 Ansible 劇本,我認為它可以滿足您的需求(儘管是測試檔案而不是實際的 bashrc.txt)。

---

- name: Example for serverfault.com
  hosts: all
  tasks:
  - name: Put line with quotes, backslash, and colon in a file
    lineinfile: 
      dest: /home/vagrant/testfile.txt
      create: yes
      line: "bind '\"\\e[B\"':history-search-forward"

我已經運行了這個,並在 testfile.txt 中得到了這一行:

bind '"\e[B"':history-search-forward

我正在使用 Ansible 2.1.1.0。不知道和1.9.1有什麼不同

相關內容