Line break with lineinfile in ansible

Viewed 4872

The only way that I find to respect regex line break \n is concatenating everything in one line with double quote.

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

The result is correctly as I expected, but is so ugly. Have a way to better this?

1 Answers

An option would be to use 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}"
Related