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
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.
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.
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.
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.