On 27.07.2017 02:08, Vijay Misra wrote:
   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"
  when: taskresult|succeeded
- debug: msg="HPQC Failed Create snapshot of a vGPU VM on ESX"
  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
  when: taskresult|succeeded
- shell: echo "HPQC Failed Create snapshot of a vGPU VM on ESX" >> results.txt
  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.

- shell: echo "HPQC {{ (hostvars[item].result | succeeded) | ternary('Passed', 'Failed') }} Create snapshot of a vGPU VM on ESX" >>results.txt
    with_items: "{{ ansible_play_hosts }}"
    delegate_to: localhost
    run_once: yes


--
Kai Stian Olstad

--
You received this message because you are subscribed to the Google Groups "Ansible 
Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/050bee11a57a65b96f4d54e4c64accac%40olstad.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to