uri Module - Wait For Satellite Task to Complete

I’m writing a playbook to publish new Content View versions and promote the new version to our Lifecycle Environments using the uri module. I’m trying to create a task that will query the task ID until it reports a result of ‘success’. I’ve tried the following:

  • name: Get the tasks list from Satellite
    uri:
    url: https://satelitlite-server/foreman_tasks/api/tasks
    method: GET
    body:
    order: ‘id DESC’
    organization_id: 3
    user: “{{ satellite_user }}”
    password: “{{ satellite_pass }}”
    force_basic_auth: yes
    validate_certs: no
    body_format: json
    register: tasks_list

  • name: Wait for new Content View version to finish publishing

uri:
url: “https://satellite-server/foreman_tasks/api/tasks/{{ tasks_list.json.results[4].id }}”
method: GET
status_code: 200
validate_certs: no
user: “{{ satellite_user }}”
password: “{{ satellite_pass }}”
force_basic_auth: yes
body_format: json
register: task_result
until: task_result.json.results[4].result == ‘success’
retries: 60
delay: 20

This gives me an error of:

TASK [Wait for new Content View version to finish publishing] *********************************************************************************************************************************************************************************
fatal: [localhost]: FAILED! => {“msg”: “The conditional check ‘task_result.json.results[4].result == ‘success’’ failed. The error was: error while evaluating conditional (task_result.json.results[4].result == ‘success’): ‘dict object’ has no attribute ‘results’”}

I’m guessing I can’t query a variable that’s registered in the same task? Does anyone know an alternative method other than just pausing the playbook for a few minutes to let the publish task finish?

You can, but the error is telling you that the JSON response, didn’t have a results key. You need to account for the fact, that you might not always get the response you expect.

Maybe like:

until: (task_result.json.results|default([0, 1, 2, 3, dict(result=‘NOT_SUCCESS’)))[4].result == ‘success’

In any case, your until makes a lot of assumptions about the data you expect to be there, you need to work from the perspective of not having what you expect.

Can you explain what you’re doing with the until line you posted?

So I know what’s in task_result.json.results[4].result. Here’s what I get if I debug as my next task:

  • debug:
    var: tasks_list.json.results[4].result

TASK [debug] **********************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
“tasks_list.json.results[4].result”: “success”
}

That’s why I’m confused, because according to that debug, the until line should work.

The idea, is that if results isn’t defined, to provide a default for the until logic to complete.

You are using results[4], which means it’s the 5th element (now I want to go watch a movie).

Then in the 5th element of results, you are inspecting the result key, for a value of success.

This just says when results is not defined, use the default of [0,1,2,3, dict(result=‘NOT_SUCCESS’)]

the 0-3 elements, are useless, and don’t matter, it’s just there to make sure that the 5th element ([4]) is a dict/hash that matches your expectations.

Got it, thanks for the explanation. I’ll try that and see where it gets me.

I guess I was staring at this too long and was referencing the wrong variable, or I got lost in the JSON output of the results, but the answer ended up being task_result.json.result == ‘success’, which makes perfect sense.

Now I just need to figure out how to do this, in a loop, for each lifecycle environment promotion.