43

我希望这很简单。我正在使用这样的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

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

我的参数是否正确?我尝试将正则表达式设置为'^'''(空白)。还有其他方法可以解决这个问题吗?

我正在使用 Ansible 1.3.3。

4

2 回答 2

39

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

由于我只想将该行附加到文件一次,因此我需要正则表达式以尽可能接近地匹配该行。在我的情况下,这很复杂,因为我的行包含变量。但是,假设 line started [[ -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
于 2013-10-30T21:37:36.340 回答
25

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
于 2017-09-07T06:04:26.527 回答