Skip to content

Commit cd65ee6

Browse files
committed
Merge pull request #270 from CodeNow/SAN-3296-buckshot-approach
Grab container ID using bash sorcery to pass to container_kill_start kill task.
2 parents 02aa5b8 + 965c010 commit cd65ee6

2 files changed

Lines changed: 28 additions & 2 deletions

File tree

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/env bash
2+
3+
IMAGE_NAME="$1"
4+
RUNNING_CONTAINERS=""
5+
6+
CONTAINERS=`docker ps | grep -v '^CONTAINER' | awk '{print $1}'`
7+
8+
if [ "" = "${CONTAINERS}" ] ; then
9+
exit 0
10+
else
11+
for container in ${CONTAINERS} ; do
12+
docker inspect ${container} | grep -q ${IMAGE_NAME}
13+
if [ ${?} -eq 0 ] ; then
14+
RUNNING_CONTAINERS="${container} "
15+
fi
16+
done
17+
fi
18+
19+
echo "${RUNNING_CONTAINERS}"

ansible/roles/container_kill_start/tasks/main.yml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
---
21
- name: "look for stopped {{ container_image }} containers"
32
shell: >
43
sudo docker ps -a |
@@ -13,9 +12,17 @@
1312
shell: sudo docker rm {{ item }}
1413
with_items: stopped_containers.stdout_lines
1514

15+
- name: "copy find running containers with this image tag util script"
16+
sudo: yes
17+
copy:
18+
src=findTagRunning.sh
19+
dest=/root/findTagRunning.sh
20+
mode=0700
21+
1622
# get current running container with this image
1723
- name: "look for running containers running {{ container_image }}"
18-
shell: "sudo docker ps | grep {{ container_image }}: | awk '{print $1}'"
24+
sudo: yes
25+
shell: "/root/findTagRunning.sh {{ container_image }}"
1926
register: old_containers_id
2027
changed_when: old_containers_id.stdout != ''
2128

0 commit comments

Comments
 (0)