-
Notifications
You must be signed in to change notification settings - Fork 0
/
awx_cleanup.yaml
167 lines (147 loc) · 8.4 KB
/
awx_cleanup.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
---
- name: AWX Cleanup
hosts: awx_controller
tasks:
- name: Run AWX Cleanup script
shell: |
echo Retrieving Project Update Jobs
projectJobs=`curl -k -s -H 'Authorization: bearer {{token}}' -H 'Content-Type: application/json' -X GET '{{url}}api/v2/projects/69/project_updates/?page_size=3200' | jq '.results[].id' 2>/dev/null`
inventoryJobs=`curl -k -s -H 'Authorization: bearer {{token}}' -H 'Content-Type: application/json' -X GET '{{url}}api/v2/inventory_updates/?page_size=3200' | jq '.results[].id' 2>/dev/null`
if [ -z "$projectJobs" ] && [ $? -eq 0 ]
then
echo No Project Update Jobs found.
else
for id in $projectJobs
do
echo deleting project update $id
curl -k -s -H 'Authorization: bearer {{token}}' -H 'Content-Type: application/json' -X DELETE {{url}}api/v2/project_updates/$id/
done
fi
if [ -z "$inventoryJobs" ] && [ $? -eq 0 ]
then
echo No Inventory Update Jobs found.
else
for id in $inventoryJobs
do
echo deleting inventory update $id
curl -k -s -H 'Authorization: bearer {{token}}' -H 'Content-Type: application/json' -X DELETE {{url}}api/v2/inventory_updates/$id/
done
fi
###############
for i in {1..10}
do
echo Failed Jobs, Workflows and Commands. Checking page $i...
failedCommands=`curl -k -s -H 'Authorization: bearer {{token}}' -H 'Content-Type: application/json' -X GET "{{url}}api/v2/ad_hoc_commands/?page_size=3200&page=$i"| jq '.results[] | select(.status|test ("failed")) | .id' 2>/dev/null`
failedJobs=`curl -k -s -H 'Authorization: bearer {{token}}' -H 'Content-Type: application/json' -X GET "{{url}}api/v2/jobs/?page_size=3200&page=$i" | jq '.results[] | select(.status|test ("failed")) | .id' 2>/dev/null`
failedWorkflows=`curl -k -s -H 'Authorization: bearer {{token}}' -H 'Content-Type: application/json' -X GET "{{url}}api/v2/workflow_jobs/?page_size=3200&page=$i" | jq '.results[] | select(.status|test ("failed")) | .id' 2>/dev/null`
if [ -z "$failedJobs" ] && [ $? -ne 0 ]
then
echo Page $i does not have any Failed Jobs. Moving to the next one.
elif [ -z "$failedJobs" ] && [ $? -eq 0 ]
then
echo Failed Jobs, page $i clean.
else
for id in $failedJobs
do
echo Deleting Failed Job $id
curl -k -s -H 'Authorization: bearer {{token}}' -H 'Content-Type: application/json' -X DELETE {{url}}api/v2/jobs/$id/
done
fi
if [ -z "$failedCommands" ] && [ $? -ne 0 ]
then
echo Page $i does not have any Failed Commands. Moving to the next one.
elif [ -z "$failedCommands" ] && [ $? -eq 0 ]
then
echo Failed Commands, page $i clean.
else
for id in $failedCommands
do
echo Deleting Failed Command $id
curl -k -s -H 'Authorization: bearer {{token}}' -H 'Content-Type: application/json' -X DELETE {{url}}api/v2/ad_hoc_commands/$id/
done
fi
if [ -z "$failedWorkflows" ] && [ $? -ne 0 ]
then
echo Page $i does not have any Failed Workflows. Moving to the next one.
elif [ -z "$failedWorkflows" ] && [ $? -eq 0 ]
then
echo Failed Workflows, page $i clean.
else
for id in $failedWorkflows
do
echo Deleting Failed Workflows $id
curl -k -s -H 'Authorization: bearer {{token}}' -H 'Content-Type: application/json' -X DELETE {{url}}api/v2/workflow_jobs/$id/
done
fi
done
###############
for i in {1..10}
do
echo Canceled Jobs, Workflows and Commands. Checking page $i
canceledCommands=`curl -k -s -H 'Authorization: bearer {{token}}' -H 'Content-Type: application/json' -X GET "{{url}}api/v2/ad_hoc_commands/?page_size=3200&page=$i"| jq '.results[] | select(.status|test ("canceled")) | .id' 2>/dev/null`
canceledJobs=`curl -k -s -H 'Authorization: bearer {{token}}' -H 'Content-Type: application/json' -X GET "{{url}}api/v2/jobs/?page_size=3200&page=$i" | jq '.results[] | select(.status|test ("canceled")) | .id' 2>/dev/null`
canceledWorkflows=`curl -k -s -H 'Authorization: bearer {{token}}' -H 'Content-Type: application/json' -X GET "{{url}}api/v2/workflow_jobs/?page_size=3200&page=$i" | jq '.results[] | select(.status|test ("canceled")) | .id' 2>/dev/null`
if [ -z "$canceledJobs" ] && [ $? -ne 0 ]
then
echo Page $i does not have any Canceled Jobs. Moving to the next one.
elif [ -z "$canceledJobs" ] && [ $? -eq 0 ]
then
echo Canceled Jobs, page $i clean.
else
for id in $canceledJobs
do
echo Deleting Canceled Job $id
curl -k -s -H 'Authorization: bearer {{token}}' -H 'Content-Type: application/json' -X DELETE {{url}}api/v2/jobs/$id/
done
fi
if [ -z "$canceledCommands" ] && [ $? -ne 0 ]
then
echo Page $i does not have any Canceled Commands. Moving to the next one.
elif [ -z "$canceledCommands" ] && [ $? -eq 0 ]
then
echo Canceled Commands, page $i clean.
else
for id in $canceledCommands
do
echo Deleting Canceled Command $id
curl -k -s -H 'Authorization: bearer {{token}}' -H 'Content-Type: application/json' -X DELETE {{url}}api/v2/ad_hoc_commands/$id/
done
fi
if [ -z "$canceledWorkflows" ] && [ $? -ne 0 ]
then
echo Page $i does not have any Canceled Workflows. Moving to the next one.
elif [ -z "$canceledWorkflows" ] && [ $? -eq 0 ]
then
echo Canceled Workflows, page $i clean.
else
for id in $canceledWorkflows
do
echo Deleting Canceled Workflows $id
curl -k -s -H 'Authorization: bearer {{token}}' -H 'Content-Type: application/json' -X DELETE {{url}}api/v2/workflow_jobs/$id/
done
fi
done
###############
echo Retrieving Project Update Jobs
projectJobs=`curl -k -s -H 'Authorization: bearer {{token}}' -H 'Content-Type: application/json' -X GET '{{url}}api/v2/projects/69/project_updates/' | jq '.results[].id' 2>/dev/null`
inventoryJobs=`curl -k -s -H 'Authorization: bearer {{token}}' -H 'Content-Type: application/json' -X GET '{{url}}api/v2/inventory_updates/' | jq '.results[].id' 2>/dev/null`
if [ -z "$projectJobs" ] && [ $? -eq 0 ]
then
echo No Project Update Jobs found.
else
for id in $projectJobs
do
echo deleting project update $id
curl -k -s -H 'Authorization: bearer {{token}}' -H 'Content-Type: application/json' -X DELETE {{url}}api/v2/project_updates/$id/
done
fi
if [ -z "$inventoryJobs" ] && [ $? -eq 0 ]
then
echo No Inventory Update Jobs found.
else
for id in $inventoryJobs
do
echo deleting inventory update $id
curl -k -s -H 'Authorization: bearer {{token}}' -H 'Content-Type: application/json' -X DELETE {{url}}api/v2/inventory_updates/$id/
done
fi