[英]Git - Use Linux-style Line Endings on Linux and Windows-style Line Endings on Windows
[英]How to replace Windows-style CR/LF line endings with Linux-style endings in ansible?
我在我的任務中試過這個,但似乎沒有用
- name: Fix line endings from CRLF to LF
local_action: replace dest={{my_dir}}/conf/{{item}} regexp='\r\n' replace='\n'
我通常使用sed這樣做,它的工作原理
sed -i 's/\r//g' file
我想避免使用shell模塊進行此替換,因為它會在ansible中發出警告
您可以使用-replace
命令刪除CRLF行結尾。 你的劇本可能看起來像:
---
- hosts: all
tasks:
- local_action: replace dest={{my_dir}}/conf/{{item}} regexp="\r"
通過不在- replace
命令中指定replace
參數,它將只刪除所有回車符。 請參見http://docs.ansible.com/ansible/replace_module.html 。
我使用我創建的本地文件測試了這個,並且在localhost上測試時它可以工作。 當我將localhost添加到/etc/ansible/hosts
文件並使用以下playbook時,它也有效:
---
- hosts: all
tasks:
- replace: dest={{my_dir}}/conf/{{item}} regexp="\r"
請務必使用絕對文件路徑。
你可以這樣做:
set_fact:
my_content: "{{ lookup('file', "{{my_dir}}/conf/{{item}}" ) | replace('\r\n', '\n')}}"
在此之后,您可以使用內容或保存在磁盤中。
以下內容使用Jinja2模板引擎轉換行結尾。 在ansible機器( delegate_to: localhost
)上的源文件的開頭插入一個行結束指令。 然后可以通過將template
或win_template
應用於文件來完成將文件發送到下游服務器。
它處理具有任何行結尾的源文件,如果您正在處理來自多個源的文件列表,這可能很有用。
- name: prepare to add line endings
lineinfile:
insertbefore: BOF
dest: '{{ src_file }}'
line: '#jinja2: newline_sequence:"\n"'
#for Linux to Windows: #line: '#jinja2: newline_sequence:"\r\n"'
delegate_to: localhost
- name: copy changed file with correct line-endings
template: # win_template for Linux to Windows
src: '{{ src_file }}'
dest: '{{ dest_file }}'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.