在 Ansible 中,如何在文件末尾添加一行? [英] In Ansible, how do I add a line to the end of a file?
问题描述
我希望这很简单.我正在使用 lineinfile
模块,如下所示:
I would expect this to be pretty simple. I'm using the lineinfile
module like so:
- name: Update bashrc for PythonBrew for foo user
lineinfile:
dest=/home/foo/.bashrc
backup=yes
line="[[ -s ${pythonbrew.bashrc_path} ]] && source ${pythonbrew.bashrc_path}"
owner=foo
regexp='^'
state=present
insertafter=EOF
create=True
我遇到的问题是它将文件中的最后一行(即 fi
)替换为我的新行,而不是附加该行.这会产生语法错误.
The problem I'm having is that it's replacing the last line in the file (which is fi
) with my new line rather than appending the line. This produces a syntax error.
我的参数是否正确?我已经尝试将 regexp 设置为 '^'
和 ''
(空白).还有其他方法可以解决这个问题吗?
Do I have the parameters correct? I've tried setting regexp to both '^'
and ''
(blank). Is there another way to go about this?
我使用的是 Ansible 1.3.3.
I'm using Ansible 1.3.3.
推荐答案
显然 ansible 已经成熟,现在(版本 >2.4.0)根据 documentation, 仅指定行时的默认值会将给定的行附加到目标文件:
Apparently ansible has matured and now (version >2.4.0) according to the documentation, The defaults when only the line is specified will append a given line to the destination file:
- name: Update bashrc for PythonBrew for foo user
lineinfile:
dest: /home/foo/.bashrc
line: "[[ -s ${pythonbrew.bashrc_path} ]] && source {pythonbrew.bashrc_path}"
owner: foo
这篇关于在 Ansible 中,如何在文件末尾添加一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!