在Ansible中,如何在文件末尾添加一行? [英] In Ansible, how do I add a line to the end of a file?

查看:1821
本文介绍了在Ansible中,如何在文件末尾添加一行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望这很简单.我正在像这样使用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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆