我确保 Linux 计算机上存在具有特定内容的文件。 如何确保文件以换行符结尾?
以下任务都不会生成以换行符结尾的文件:
- name: Write a file with content
ansible.builtin.copy:
content: "{{ files_content }}"
dest: /tmp/foo.txt
- name: Write a file with content
ansible.builtin.copy:
content: "{{ files_content }}\\n"
dest: /tmp/bar.txt
- name: Write a file with content
ansible.builtin.copy:
content: "{{ files_content }}\n"
dest: /tmp/foobar.txt"
如果没有其他方法,我想我可以使用下面的任务。但我很好奇是否必须这么做。
- name: Write a file with content
ansible.builtin.copy:
content: |
{{ files_content }}
dest: /tmp/foobar.txt"
问:“如何确保文件以换行符结尾?”
只需将其添加到变量 content(字符串)中。一个最小的示例手册
---
- hosts: localhost
become: false
gather_facts: false
vars:
CONTENT: 'test'
tasks:
- name: Write a file with content
ansible.builtin.copy:
content: "{{ CONTENT ~ '\n'}}"
dest: test.txt
将生成包含请求内容的输出文件
test.txt
。