# ANSIBLE: how to append a string at the end of a file

**URL:** <https://forum.ansible.com/t/ansible-how-to-append-a-string-at-the-end-of-a-file/25102>\
**Category:** Ansible Project\
**Created:** [July 27, 2017, 12:08am UTC](https://forum.ansible.com/t/ansible-how-to-append-a-string-at-the-end-of-a-file/25102 "2017-07-27T00:08:53Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Victor1](https://avatars.discourse-cdn.com/v4/letter/v/6de8d8/32.png) [@Victor1](https://forum.ansible.com/u/Victor1)\
**Post date:** [July 27, 2017, 12:08am UTC](https://forum.ansible.com/t/ansible-how-to-append-a-string-at-the-end-of-a-file/25102/1 "2017-07-27T00:08:53Z")

</div>

Hi ,

I am trying to use ansible as a test tool. at the end of each sucess/faiure i want to append a string at the end of a file and this file can be used as a test report.  
i Have tried this code but it did not log anything in the file.

```auto
- debug: msg="HPQC Create snapshot of a vGPU VM on ESX"
  when: taskresult|succeeded
- debug: msg="HPQC Failed Create snapshot of a vGPU VM on ESX"
  when: taskresult|failed

- shell: echo "HPQC Passed Create snapshot of a vGPU VM on ESX" >> results.txt
  when: taskresult|succeeded
- shell: echo "HPQC Failed Create snapshot of a vGPU VM on ESX" >> results.txt
  when: taskresult|failed

  when i run with -vvvv switch, I get the following output but nothing is appended in the file locataed at /etc/ansible/ directoery( I am running playbook from /etc/ansible directory)

 
changed: [] => {
    "changed": true, 
    "cmd": "echo \"HPQC Passed Create snapshot of a vGPU VM on ESX\" >> results.txt", 
    "delta": "0:00:00.017046", 
    "end": "2017-07-27 00:07:14.100974", 
    "invocation": {
        "module_args": {
            "_raw_params": "echo \"HPQC Passed Create snapshot of a vGPU VM on ESX\" >> results.txt", 
            "_uses_shell": true, 
            "chdir": null, 
            "creates": null, 
            "executable": null, 
            "removes": null, 
            "warn": true
        }
    }, 
    "rc": 0, 
    "start": "2017-07-27 00:07:14.083928", 
    "stderr": "", 
    "stderr_lines": [], 
    "stdout": "", 
    "stdout_lines": []
}

TASK [ESX_VM_SnapRestore : command] ************************************************************************************************************************************************************************
task path: /etc/ansible/roles/ESX_VM_SnapRestore/tasks/main.yml:41
skipping: [1] => {
    "changed": false, 
    "skip_reason": "Conditional result was False", 
    "skipped": true
}

Thanks for your help.

-VM

```

---

<div class="post-metadata">

**Author:** ![Rick\_Stokkingreef](https://avatars.discourse-cdn.com/v4/letter/r/a587f6/32.png) [@Rick\_Stokkingreef](https://forum.ansible.com/u/Rick_Stokkingreef)\
**Post date:** [July 27, 2017, 7:42am UTC](https://forum.ansible.com/t/ansible-how-to-append-a-string-at-the-end-of-a-file/25102/2 "2017-07-27T07:42:34Z")

</div>

Hi,

You should use the line in file module, only specify the path and line. This way it will append it to the file.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [July 27, 2017, 8:35am UTC](https://forum.ansible.com/t/ansible-how-to-append-a-string-at-the-end-of-a-file/25102/3 "2017-07-27T08:35:44Z")

</div>

> &nbsp;&nbsp;&nbsp;I am trying to use ansible as a test tool. at the end of each  
> sucess/faiure i want to append a string at the end of a file and this file  
> can be used as a test report.  
> i Have tried this code but it did not log anything in the file.
> 
> - debug: msg="HPQC Create snapshot of a vGPU VM on ESX"  
> &nbsp;&nbsp;when: taskresult|succeeded  
> - debug: msg="HPQC Failed Create snapshot of a vGPU VM on ESX"  
> &nbsp;&nbsp;when: taskresult|failed

Debug only writes to the stdout so you only get those on screen.

> - shell: echo "HPQC Passed Create snapshot of a vGPU VM on ESX" \>\> results.txt  
> &nbsp;&nbsp;when: taskresult|succeeded  
> - shell: echo "HPQC Failed Create snapshot of a vGPU VM on ESX" \>\> results.txt  
> &nbsp;&nbsp;when: taskresult|failed

This will store the text in the file results.txt on the remote host, not on the host you are running ansible playbook on.  
To write to a file on localhost you need to add delegate\_to: localhost

The problem occur if you run against more than one host, you'll have several processes trying to write to the same file at once, this is very error prone.  
One solution to overcome that is run\_once with a loop.

&nbsp;&nbsp;&nbsp;- shell: echo "HPQC {{ (hostvars[item].result | succeeded) | ternary('Passed', 'Failed') }} Create snapshot of a vGPU VM on ESX" \>\>results.txt  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with\_items: "{{ ansible\_play\_hosts }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;delegate\_to: localhost  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;run\_once: yes

---

<div class="post-metadata">

**Author:** ![alikins](https://avatars.discourse-cdn.com/v4/letter/a/d07c76/32.png) [@alikins](https://forum.ansible.com/u/alikins)\
**Post date:** [July 27, 2017, 6:17pm UTC](https://forum.ansible.com/t/ansible-how-to-append-a-string-at-the-end-of-a-file/25102/4 "2017-07-27T18:17:24Z")

</div>

“I am trying to use ansible as a test tool. at the end of each sucess/faiure i want to append a string at the end of a file and this file can be used as a test report.  
i Have tried this code but it did not log anything in the file.”

This sounds like a good use for a callback plugin. A custom callback plugin could append to a file on _\_ok/_\_fail callbacks.

I would base it around the python ‘logging’ module, but it could be something simpler.

The ‘junit’ callback might be a good example and could possibly be used directly if a junit.xml file is useful for your test tool.

---

<div class="post-metadata">

**Author:** ![Victor1](https://avatars.discourse-cdn.com/v4/letter/v/6de8d8/32.png) [@Victor1](https://forum.ansible.com/u/Victor1)\
**Post date:** [July 27, 2017, 11:06pm UTC](https://forum.ansible.com/t/ansible-how-to-append-a-string-at-the-end-of-a-file/25102/5 "2017-07-27T23:06:14Z")

</div>

Thanks kai !

Yes, I don’t know how i missed the localhost part after adding “delegateTo: localhost” I am able to append the line on each test case success/failure.  
One more thing i wanted to clarify nomatter what command i se for appending the line( command/sheel/lineinfile) it always fails and outpus sudo; A password is required.  
am i missing something\>

I have my playbook that contained one role and i declared “become: true” before the role as below

**//vcenter\_snaprestore.yml**

```auto
- hosts: esx1
  become: true
  roles:
     - ESX_VM_SnapRestore

```

---

<div class="post-metadata">

**Author:** ![Victor1](https://avatars.discourse-cdn.com/v4/letter/v/6de8d8/32.png) [@Victor1](https://forum.ansible.com/u/Victor1)\
**Post date:** [July 27, 2017, 11:23pm UTC](https://forum.ansible.com/t/ansible-how-to-append-a-string-at-the-end-of-a-file/25102/6 "2017-07-27T23:23:02Z")

</div>

Thanks Rick for your help !  
I am using lineinfile now and it works partially, only thing is it fails initially and outputs Sudo: a password requied". if i give permission through cmod 777 results.txt and then try to run the playbook it passes.

```auto
- lineinfile:
    path: /etc/ansible/results.txt
    line: 'HPQC1 Passed Create snapshot of a vGPU VM on ESX'
    insertafter: EOF
  when: taskresult|succeeded
  delegate_to: localhost

vcenter_snaprestore.yml
***********************************************

```

```auto
- hosts: esx1
  become: true
  roles:
     - ESX_VM_SnapRestore
**********************************

vmisra@ubuntu:/etc/ansible$ ansible-playbook vcenter_snaprestore.yml

Do you see any reason for sudo: a Password required error message.

Thanks,
Vijay

```

---

<div class="post-metadata">

**Author:** ![Victor1](https://avatars.discourse-cdn.com/v4/letter/v/6de8d8/32.png) [@Victor1](https://forum.ansible.com/u/Victor1)\
**Post date:** [July 28, 2017, 3:24am UTC](https://forum.ansible.com/t/ansible-how-to-append-a-string-at-the-end-of-a-file/25102/7 "2017-07-28T03:24:15Z")

</div>

Thanks Adrian ! I would try using callback plugin.

-Vijay

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [July 28, 2017, 1:42pm UTC](https://forum.ansible.com/t/ansible-how-to-append-a-string-at-the-end-of-a-file/25102/8 "2017-07-28T13:42:23Z")

</div>

> One more thing i wanted to clarify nomatter what command i se for appending  
> the line( command/sheel/lineinfile) it always fails and outpus sudo; A  
> password is required.  
> am i missing something\>

You have become: true in your play, so you need to provide a password if do not have passwordless sudo.  
become: true on the play means all task will runs with become.

Do esx have sudo?  
My installation do not have sudo.

Since your delegate\_to: run on a different host it will a problem if the remote host and the localhost does not have the same password.  
But in you case the lineinfile does not need sudo i guess so you can set become: false on that task.

> - hosts: esx1  
> &nbsp;&nbsp;become: true  
> &nbsp;&nbsp;roles:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- ESX\_VM\_SnapRestore
> 
> \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
> 
> - lineinfile:  
> &nbsp;&nbsp;&nbsp;&nbsp;path: /etc/ansible/results.txt  
> &nbsp;&nbsp;&nbsp;&nbsp;line: 'HPQC Passed Create snapshot of a vGPU VM on ESX'  
> &nbsp;&nbsp;&nbsp;&nbsp;insertafter: EOF  
> &nbsp;&nbsp;when: taskresult|succeeded  
> &nbsp;&nbsp;delegate\_to: localhost

By the way, when you use lineinfile it will not append to EOF if the text in line: already exist somewhere in the file.

---

<div class="post-metadata">

**Author:** ![Werner\_Flamme](https://avatars.discourse-cdn.com/v4/letter/w/c68b51/32.png) [@Werner\_Flamme](https://forum.ansible.com/u/Werner_Flamme)\
**Post date:** [July 28, 2017, 2:12pm UTC](https://forum.ansible.com/t/ansible-how-to-append-a-string-at-the-end-of-a-file/25102/9 "2017-07-28T14:12:52Z")

</div>

Vijay Misra [28.07.2017 01:23]:

> Thanks Rick for your help !  
> I am using lineinfile now and it works partially, only thing is it fails  
> initially and outputs Sudo: a password requied".

How does the corresponding line look like in /etc/sudoers? There must be  
a "NOPASSWD:" as in

nagios ALL=(ALL) NOPASSWD: /usr/local/bin/dosomething.sh

---

<div class="post-metadata">

**Author:** ![Victor1](https://avatars.discourse-cdn.com/v4/letter/v/6de8d8/32.png) [@Victor1](https://forum.ansible.com/u/Victor1)\
**Post date:** [July 29, 2017, 3:10am UTC](https://forum.ansible.com/t/ansible-how-to-append-a-string-at-the-end-of-a-file/25102/10 "2017-07-29T03:10:42Z")

</div>

Thanks kai ! setting become: false worked.

-VM
