Ansible: Write multiple lines to a file using local_action or another method

ansibleansible-playbook

I am using the command:

  • local_action: copy content="The installation failed" dest=~/ansible/ansible_log.txt

However, when I do it again:

  • local_action: copy content="Contact me for assistance" dest=~/ansible/ansible_log.txt

It overwrites the old text with the new text. What I want to do is append to the file instead of replacing the previous text.

I tried adding in a /n to the end of the original string to no avail.

Best Answer

What about the lineinfile module:

local_action:
    module: lineinfile
    dest: "~/ansible/ansible_log.txt"
    line: "The installation failed"
    create: yes
local_action:
    module: lineinfile
    dest: "~/ansible/ansible_log.txt"
    line: "Contact me for assistance"