在Ansible中,如何在文件末尾添加一行?

kle*_*ell 35 ansible

我希望这很简单.我正在使用这样的lineinfile模块:

- 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
Run Code Online (Sandbox Code Playgroud)

我遇到的问题是它fi用我的新行替换文件中的最后一行(而不是附加行).这会产生语法错误.

我的参数是否正确?我已经尝试将regexp设置为'^'''(空白).还有另一种方法可以解决这个问题吗?

我正在使用Ansible 1.3.3.

kle*_*ell 33

Ansible讨论小组帮助我解决了这个问题.问题是regexp参数.

由于我只希望将一行附加到文件中,因此我需要正则表达式尽可能地匹配该行.在我的情况下,这很复杂,因为我的行包含变量.但是,假设线路开始[[ -s $HOME/.pythonbrew,我发现以下内容足够:

- name: Update bashrc for PythonBrew for foo user
  lineinfile:
    dest: /home/foo/.bashrc
    line: "[[ -s ${pythonbrew.bashrc_path} ]] && source ${pythonbrew.bashrc_path}"
    regexp: "^\[\[ -s \\$HOME/\.pythonbrew"
    owner: foo
    state: present
    insertafter: EOF
    create: True
Run Code Online (Sandbox Code Playgroud)


shl*_*moa 16

显然,ansible已经成熟,现在(版本> 2.4.0)根据文档,只有指定行时的默认值会将给定的行附加到目标文件:

    - name: Update bashrc for PythonBrew for foo user
      lineinfile:
        dest=/home/foo/.bashrc
        line="[[ -s ${pythonbrew.bashrc_path} ]] && source {pythonbrew.bashrc_path}"
        owner=foo
Run Code Online (Sandbox Code Playgroud)