在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.
推荐答案
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屋!