要解决Ansible的lineinfile模块总是将现有行压缩而不是附加文本的问题,可以使用以下方法:
- name: Append text to existing line
lineinfile:
path: /path/to/file
regexp: '^.+$'
backrefs: yes
line: ' text_to_append'
这将匹配以
开头的现有行,并将text_to_append
附加到现有行。
- name: Append text to existing line
lineinfile:
path: /path/to/file
insertafter: '^'
line: 'text_to_append'
这将在以
开头的行之后插入text_to_append
。
- name: Append text to existing line
lineinfile:
path: /path/to/file
regexp: '^(\s*).*$'
backrefs: yes
line: '\1 text_to_append'
这将匹配以
开头的行,并将text_to_append
附加到现有行。
请根据您的需求选择适合您的解决方案,并相应地调整参数和正则表达式。