How to deal with APIs and short live tokens

Hi all,

I have the following situation which I am not sure how to tackle with ansible.

We have a token API to retrieve auth token using my corporate credentials. Then using this token to authenticate I call another API. However, this second call/task can take some time so I am using the pattern of uri module with until,retries,delay to wait for either success or failure status. Now the token only lives for 5 minutes but waiting for the result of the second call takes usually much longer meaning that after 5 minutes the auth token expires and the task fails.

How does one deal with this scenario? Initially I thought I could use a handler to refresh the token stored in a variable but that does not seem to work.

Example playbook:

`