Reputation: 667
I'm not familiar with Ansible and struggling writing script.
In the first task, execute the query if the target job is able to terminate.
In task 2, execute another query to terminate jobs that meet the condition. However, task1 and task 2 is executed sequentially, so I don't have a solution to use the values stored in job_id_list
and query_result
in combination.
# task1
- name: Check the status of specific jobs
become: true
become_user: postgres
shell: psql -t -database -c "select count(*) from table_a a inner join table_b b on b.job_id = a.job_id where b.job_id={{ item }} and b.submitted_date > CURRENT_TIMESTAMP - interval '10 minutes';"
with_items: {{ job_id_list }}
register: query_result
# task2
- name: kill job when the result of task1 is 0
shell: psql -t database -c "update table_b set status='TERMINATED' where job_id={{ XXX }};" # here I struggle...
with_items: {{ query_result }}
become: true
become_user: postgres
when: query_result.stdout|int = 0
Upvotes: 0
Views: 287
Reputation: 414
You have job_id_list
which is a list of items you iterate over in Task#1.
Querying the database you get back count(*)
which returns number count for that job_id.
In Task#2 you want to iterate over the query result which is a number or a list of numbers(job counts) and you don'd have job_id here. You have to iterate over the same list job_id_list
but only when the query_result
is 0 in order to have the job_id
available. I hope this helps!
Edited: When I reread the query again I think your logic may be wrong. Instead of getting the count you may get the id return and then in Task#2 to iterate over the list of ids to update.
Upvotes: 1