I would like to change the bash command prompt string from

PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '


PS1='${debian_chroot:+($debian_chroot)}\[\033[01;31m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '

Specifically, it is changing the colour from green to red, by change 32 to 31.

I would like to change the file rather than deploy/copy in a full file.

I would like to search through the file, and change the line if it matches either scenario.

I tried escaping all of the special characters, but the YAML regex complained about the \ and various characters.

  • Please show your ansible code
    – AlexD
    Jun 6, 2017 at 18:48

3 Answers 3


Even if this ticket is over two years old, I found me in the same situation.

I tried a bit and found a solution to this challenge

  - name: Set default color in bash
  path: /home/example-user/.bashrc
  regexp: "^    PS1='\\${debian_chroot:\\+\\(\\$debian_chroot\\)}\\\\\\[\\\\033\\[01;32m\\\\\\]\\\\u@\\\\h\\\\\\[\\\\033\\[00m\\\\\\]:\\\\\\[\\\\033\\[01;34m\\\\\\]\\\\w\\\\\\[\\\\033\\[00m\\\\\\]\\\\\\$ '"
  line: "    PS1='${debian_chroot:+($debian_chroot)}\\[\\033[01;32m\\]\\u\\[\\033[01;32m\\]@\\h\\[\\033[00m\\]:\\[\\033[01;34m\\]\\w\\[\\033[00m\\]\\$ '"
  backup: yes
when: ansible_os_family == "Debian"

Hope this might help others too.


Why not just set PS1 to desired value?

- lineinfile:
    create: yes
    mode: 0600
    dest: /root/.bash_aliases
    owner: root
    regexp: '^PS1='
    line: 'PS1="${debian_chroot:+($debian_chroot)}\[\033[01;31m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ "'
    state: present

I used single quotes as outer ones as it doesn't require escaping characters.

  • 1
    There are other lines in the file is with PS1=, or that even start with <tab>PS1=. The substring that is unique is PS1='${debian_chroot:+($debian_chroot)}\[
    – David
    Jun 7, 2017 at 15:38

Slightly cleaner version of @Mrk's answer:

- name: "Set Bash prompt color"
    path: "~/.bashrc"
    search_string: "    PS1='${debian_chroot:+($debian_chroot)}\\[\\033[01;32m\\]\\u@\\h\\[\\033[00m\\]:\\[\\033[01;34m\\]\\w\\[\\033[00m\\]\\$ '"
    line: "    PS1='${debian_chroot:+($debian_chroot)}\\[\\033[01;31m\\]\\u@\\h\\[\\033[00m\\]:\\[\\033[01;34m\\]\\w\\[\\033[00m\\]\\$ '"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .