# Get targeted host from schedule/job template via api

**URL:** <https://forum.ansible.com/t/get-targeted-host-from-schedule-job-template-via-api/2153>\
**Category:** Get Help\
**Tags:** awx\
**Created:** [November 5, 2023, 7:47am UTC](https://forum.ansible.com/t/get-targeted-host-from-schedule-job-template-via-api/2153 "2023-11-05T07:47:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![MalfuncEddie](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/malfunceddie/32/428_2.png) [@MalfuncEddie](https://forum.ansible.com/u/MalfuncEddie)\
**Post date:** [November 5, 2023, 7:47am UTC](https://forum.ansible.com/t/get-targeted-host-from-schedule-job-template-via-api/2153/1 "2023-11-05T07:47:33Z")

</div>

Hi,

Is there an easy way to get the targeted hosts of a schedule/job template via the api.

Situation:  
I’ve got a lot of inventories subdivided in groups. I’ve got job templates that target hosts by filling in the “limit” (sometimes by prompt). These templates have a schedule to run during specific times in a month. The limit could be set in the schedule. some limits are easy (just one group) others use union and exclusions.

The number of schedules is steadily increasing.

Problems/questions  
Due to the increasing nature of templates/schedules it is getting hard to:

Q1:Determine what schedule is targeting what host  
Q2 :Determine if a host has been targeted by a job\_template/schedule

Manual solution:  
Q1  
Have a good naming convention so you can determine the group from the schedule name.  
Q2:  
Open the job template → see the limit → go to inventory → group …

My automated solution:  
Via api:  
Read all schedules that have a next run in the next 30 days  
Get the targeted inventory  
get the template/workflow/schedule limit  
run ansible-playbook with --list-hosts (on a script server)  
parse output (on a script server)  
get hosts (on a script server)

create an excel (shows schedule per host or host per schedule

Question: Can this be done faster/easier  
This solution works but since part of this needs to be executed with “shell ansible-playbook with --list-hosts” makes it slow

It would be nice if there was an awx api endpoint that just returns the hosts given a limit and an inventory. Does something like this exists:

/api/v2/get\_targeted\_hosts?inventory\_id=X&limit=groupA:groupB!group3

or other suggestions

---

<div class="post-metadata">

**Author:** ![TheRealHaoLiu](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/therealhaoliu/32/100_2.png) [@TheRealHaoLiu](https://forum.ansible.com/u/TheRealHaoLiu)\
**Post date:** [December 6, 2023, 7:55pm UTC](https://forum.ansible.com/t/get-targeted-host-from-schedule-job-template-via-api/2153/2 "2023-12-06T19:55:31Z")

</div>

I don’t think this is possible with normal inventory

@AlanCoding would constructed inventory fit this usecase?

---

<div class="post-metadata">

**Author:** ![MalfuncEddie](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/malfunceddie/32/428_2.png) [@MalfuncEddie](https://forum.ansible.com/u/MalfuncEddie)\
**Post date:** [December 7, 2023, 9:14am UTC](https://forum.ansible.com/t/get-targeted-host-from-schedule-job-template-via-api/2153/3 "2023-12-07T09:14:24Z")

</div>

I think a constructed inventory could do this but then you change the “limit” functionality to all and a constructed inventory.  
Also this would kind of make the host menu/list a mess since it list host multiple times (the original inventory and every constructed inventory).

I have a working solution as described in the original post. But yet I feel this is a QOL feature that could be most welcome in AWX.

Use cases:

- If a scheduled tasks would have it’s host known you could add a “future job” on the host level next to the previous jobs.

- When you launch a template you could list the targeted hosts in the preview section. I should not have to open a second tab and go to inventory → group → hosts to check if this is what I want to target.

PS: OMG its the real TheRealHaoLiu from AWX Office Hours
