ansible中使用lineinfile换行

Bru*_*ego 4 ansible

我发现尊重正则表达式换行符的唯一方法\n是将所有内容用双引号连接在一行中。

---
- name: Custom prompt output
  lineinfile:
    path: ~/.zshrc
    line: "\n# Custom Prompt\nlocal user=\"%{$fg[yellow]%}%n%{$fg[white]%}@%{$fg[green]%}%m%{$reset_color%}\"\nPROMPT=\"${user} ${PROMPT}\""
    state: present
Run Code Online (Sandbox Code Playgroud)

结果如我所料,是正确的,但太难看了。有办法改善这个吗?

Vla*_*tka 5

一个选择是使用blockinfile

- name: Custom prompt output
  blockinfile:
    path: ~/.zshrc
    create: yes
    block: |
      # Custom Prompt
      local user="%{$fg[yellow]%}%n%{$fg[white]%}@%{$fg[green]%}%m%{$reset_color%}"
      PROMPT="${user} ${PROMPT}"
Run Code Online (Sandbox Code Playgroud)