-
Notifications
You must be signed in to change notification settings - Fork 0
/
restart_containers.yml
43 lines (31 loc) · 1.17 KB
/
restart_containers.yml
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
# Based on https://stackoverflow.com/questions/61369132/restart-multiple-docker-containers-using-ansible
---
- name: Prompt for Node or Inventory Group on which to execute
hosts: localhost
gather_facts: no
vars_prompt:
- name: targets
prompt: What node or Group should have its containers kicked?
private: false
unsafe: true
default: "cluster1.local"
tasks:
- name: "Add {{ targets }} to dynamic group"
add_host:
name: "{{ item }}"
group: dynamic_execution_group
loop: "{{ targets.split(',') }}"
changed_when: false
- hosts: dynamic_execution_group
tasks:
- name: Get Images running on Specified Host
command: docker container ls --format "{{ '{{' }}.Names {{ '}}' }} {{ '{{' }}.Image {{ '}}' }}"
register: docker_container_list
changed_when: false
- name: Restart containers
docker_container:
name: "{{ item.split(' ')[0]}}"
image: "{{ item.split(' ')[1]}}"
state: started
restart: yes
loop: "{{ docker_container_list.stdout_lines }}"