可以使用Ansible的register功能来捕获shell命令的输出,并将其存储在变量中,然后将变量传递给lineinfile模块。这样就可以避免出现无法产生新行的问题。
示例代码:
name: use register to capture shell output shell: cat /etc/hosts register: cat_output
name: append cat output to hosts file lineinfile: path: /etc/hosts line: "{{ cat_output.stdout_lines }}" insertafter: EOF