uri Module - Wait For Satellite Task to Complete

32 views
Skip to first unread message

Cade Lambert

unread,
Jun 12, 2019, 12:14:25 PM6/12/19
to Ansible Project
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:
      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?

Matt Martz

unread,
Jun 12, 2019, 12:38:06 PM6/12/19
to ansible...@googlegroups.com
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.

--
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 ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/6f35ca5c-1c4c-439c-bf05-42a5ad6fe478%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
Matt Martz
@sivel
sivel.net

Cade Lambert

unread,
Jun 12, 2019, 1:11:10 PM6/12/19
to Ansible Project
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.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible...@googlegroups.com.

Matt Martz

unread,
Jun 12, 2019, 1:21:23 PM6/12/19
to ansible...@googlegroups.com
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.

To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.

To post to this group, send email to ansible...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Cade Lambert

unread,
Jun 12, 2019, 1:26:26 PM6/12/19
to Ansible Project
Got it, thanks for the explanation.  I'll try that and see where it gets me.

Cade Lambert

unread,
Jun 12, 2019, 2:53:03 PM6/12/19
to Ansible Project
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.
Reply all
Reply to author
Forward
0 new messages