Need suggestion on the ansible playbook to monitor task using rest api on the redhat satellite server v6.13

I appreciate your advise on ansible playbook mention below i am using Redhat satellite server [GET /foreman_tasks/api/tasks] api to extract the running tasks l and then mapping the task related to publishing with key value “result”. I need to check this task until the running task result from status pending to success.
I m able to retrieve task and also specific task result value but how to loop this task until the result changes from status pending to success?
using ansible v 2.9

- name: Retrieve tasks and find the state of the task

  • name: Retrieve all the tasks
    ansible.builtin.uri:
    url: “https://{{ satellite_name }}/foreman_tasks/api/tasks”
    user: “{{ sat_user }}”
    password: “{{ sat_token }}”
    force_basic_auth: true
    method: GET
    register: tasks
  • name: Set fact to the publish task state
    ansible.builtin.set_fact:
    result: “{{ tasks.json.results | selectattr (‘label’, ‘==’,‘Actions::Katello::ContentView::Publish’) | map (attribute= ‘result’) | first }}”

Print taskid from the list

  • name: “Print task state”
    ansible.builtin.debug:
    var: result

I appreciate if any one us give me suggestion as i m struck and unable to proceed further

Thanks
Deepak B Kumar

Loops — Ansible Community Documentation Ansible allows you to continue looping until a condition is met via the until: parameter.

Thank you for reply . But I confused should i use until: loop at the set_fact task or at rest api tasks ? if i use under set_fact task its only looping set_fact and it does retry thrice and the result still would be in status “pending” its fails .
If i use at the rest api uri module it breaks as it cant find result variable value.

You would use “until” on the rest API call and check for whatever you want to happen under the registered variable you have, which you seem to have named “tasks”. I don’t have foreman or satellite so I can’t duplicate exactly what you’re expecting to see, but somewhere under the tasks registered variable would be the condition you’d look for.

I appreciate the suggestion you provided me I was able to loop using until. Now the task looks like

  • name: Get Publish task status until success
    ansible.builtin.uri:
    url: “https://{{ satellite_name }}/foreman_tasks/api/tasks/{{ task_id }}”
    method: GET
    user: “{{ sat_user }}”
    password: “{{ sat_token }}”
    force_basic_auth: true
    validate_certs: false
    register: task_status
    until: task_status.json.result == “success”
    retries: 20
    delay: 50

Thanks !
Deepak