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

**URL:** <https://forum.ansible.com/t/need-suggestion-on-the-ansible-playbook-to-monitor-task-using-rest-api-on-the-redhat-satellite-server-v6-13/10279>\
**Category:** Get Help\
**Created:** [October 14, 2024, 4:58pm UTC](https://forum.ansible.com/t/need-suggestion-on-the-ansible-playbook-to-monitor-task-using-rest-api-on-the-redhat-satellite-server-v6-13/10279 "2024-10-14T16:58:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Deepak\_B\_K](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/deepak_b_k/32/5151_2.png) [@Deepak\_B\_K](https://forum.ansible.com/u/Deepak_B_K)\
**Post date:** [October 14, 2024, 4:58pm UTC](https://forum.ansible.com/t/need-suggestion-on-the-ansible-playbook-to-monitor-task-using-rest-api-on-the-redhat-satellite-server-v6-13/10279/1 "2024-10-14T16:58:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![anon81884396](https://avatars.discourse-cdn.com/v4/letter/a/b782af/32.png) [@anon81884396](https://forum.ansible.com/u/anon81884396)\
**Post date:** [October 14, 2024, 5:04pm UTC](https://forum.ansible.com/t/need-suggestion-on-the-ansible-playbook-to-monitor-task-using-rest-api-on-the-redhat-satellite-server-v6-13/10279/2 "2024-10-14T17:04:37Z")

</div>

[Loops — Ansible Community Documentation](https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_loops.html#retrying-a-task-until-a-condition-is-met) Ansible allows you to continue looping until a condition is met via the `until:` parameter.

---

<div class="post-metadata">

**Author:** ![Deepak\_B\_K](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/deepak_b_k/32/5151_2.png) [@Deepak\_B\_K](https://forum.ansible.com/u/Deepak_B_K)\
**Post date:** [October 15, 2024, 6:10am UTC](https://forum.ansible.com/t/need-suggestion-on-the-ansible-playbook-to-monitor-task-using-rest-api-on-the-redhat-satellite-server-v6-13/10279/3 "2024-10-15T06:10:25Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![anon81884396](https://avatars.discourse-cdn.com/v4/letter/a/b782af/32.png) [@anon81884396](https://forum.ansible.com/u/anon81884396)\
**Post date:** [October 15, 2024, 11:41am UTC](https://forum.ansible.com/t/need-suggestion-on-the-ansible-playbook-to-monitor-task-using-rest-api-on-the-redhat-satellite-server-v6-13/10279/4 "2024-10-15T11:41:08Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Deepak\_B\_K](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/deepak_b_k/32/5151_2.png) [@Deepak\_B\_K](https://forum.ansible.com/u/Deepak_B_K)\
**Post date:** [October 16, 2024, 7:36am UTC](https://forum.ansible.com/t/need-suggestion-on-the-ansible-playbook-to-monitor-task-using-rest-api-on-the-redhat-satellite-server-v6-13/10279/5 "2024-10-16T07:36:09Z")

</div>

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
