In Ansible, how do I add a line to the end of a file?

后端 未结 2 1330
别那么骄傲
别那么骄傲 2020-12-14 05:44

I would expect this to be pretty simple. I\'m using the lineinfile module like so:

- name: Update bashrc for PythonBrew for foo user
  lineinfil         


        
相关标签:
2条回答
  • 2020-12-14 06:33

    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
    
    0 讨论(0)
  • 2020-12-14 06:35

    The Ansible discussion group helped get me sorted out on this. The problem is the regexp parameter.

    Since I only want the line appended to the file once, I need the regexp to match the line as closely as possible. This is complicated in my case by the fact that my line includes variables. But, assuming the line started [[ -s $HOME/.pythonbrew, I found the following to be sufficient:

    - 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
    
    0 讨论(0)
提交回复
热议问题