Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace Windows-style CR/LF line endings with Linux-style endings in ansible?

Tags:

linux

sed

ansible

I tried this in my task, but doesn't seem to work

- name: Fix line endings from CRLF to LF
  local_action: replace dest={{my_dir}}/conf/{{item}} regexp='\r\n' replace='\n'

I usually do this using sed as follows and it works

sed -i 's/\r//g' file

I want to avoid using shell module to do this replacement as it throws a warning in ansible

like image 592
user330612 Avatar asked Nov 16 '25 01:11

user330612


1 Answers

You can remove the CRLF line endings with the -replace command. Your playbook might look like:

---
- hosts: all
  tasks:
    - local_action: replace dest={{my_dir}}/conf/{{item}} regexp="\r"

By not specifying the replace parameter in the - replace command, it will just remove all carriage returns. See http://docs.ansible.com/ansible/replace_module.html.

I tested this with a local file I created and it worked when testing on localhost. It also worked when I added localhost to the /etc/ansible/hosts file and had the following playbook instead:

---
- hosts: all
  tasks:
    - replace: dest={{my_dir}}/conf/{{item}} regexp="\r"

Just be sure to use the absolute filepath.

like image 131
Chris Reyes - he-him Avatar answered Nov 17 '25 20:11

Chris Reyes - he-him



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!