1

I have an ansible task that fails about 20% of the time. It almost always succeeds if retried a couple of times. I'd like to use until to loop until the task succeeds and store the output of each attempt to a separate log file on the local machine. Is there a good way to achieve this?

For example, my task currently looks like this:

- name: Provision
  register: prov_ret
  until: prov_ret is succeeded
  retries: 2
  command: provision_cmd

I can see how to store the log output from the last retry when it succeeds, but I'd like to store it from each retry. To store from the last attempt to run the command I use:

- name: Write Log
  local_action: copy content={{ prov_ret | to_nice_json }} dest="/tmp/ansible_logs/provision.log"

1 Answer 1

2

It's not possible as of 2.9. The until loop doesn't preserve results as loop does. Once a task terminates all variables inside this task will be gone except the register one.

To see what's going on in the loop write a log inside the command at the remote host. For example, the command provision_cmd writes a log to /scratch/provision_cmd.log. Run it in the block and display the log in the rescue section.

    - block:
        - name: Provision
          command: provision_cmd
          register: prov_ret
          until: prov_ret is succeeded
          retries: 2
      rescue:
        - name: Display registered variable
          debug:
            var: prov_ret
        - name: Read the log
          slurp:
            src: /scratch/provision_cmd.log
          register: provision_cmd_log
        - name: Display log
          debug:
            msg: "{{ msg.split('\n') }}"
          vars:
            msg: "{{ provision_cmd_log.content|b64decode }}"
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! That's exactly what I needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.