-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathauto_scale.yml
89 lines (68 loc) · 2.33 KB
/
auto_scale.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
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
---
- hosts: network
tasks:
- name: Create docker network
docker_network:
name: "{{ network_name }}"
ipam_config:
- subnet: "{{ subnet }}.0/16"
gateway: "{{ subnet }}.1"
- hosts: local
gather_facts: no
tasks:
- name: get last scale
script: ./get_cont_num.sh {{ image_name }}
register: last_scale
- set_fact:
last_scale={{ last_scale.stdout }}
- debug: var=last_scale
- name : start container
docker_container:
name: "{{ c_name }}_{{ item }}"
image : "{{ image_name }}"
pull: yes
restart_policy: always
hostname: "{{ h_name }}_{{ item }}"
networks:
- name: "{{ network_name }}"
ipv4_address: "{{ subnet }}.{{ 1+item|int }}"
purge_networks: yes
with_sequence: start=1 end="{{ scale }}"
- name : down-scaling uneeded containers
docker_container:
name: mongo_{{ item }}
state: absent
with_sequence: start="{{ last_scale|int if (last_scale|int - scale|int)|abs == 1|int or last_scale|int == scale|int or scale|int > last_scale|int else 1+scale|int }}" end="{{ last_scale }}"
# last_scale=4 scale=2
# last_scale=3 scale=3
# last_scale=3 scale=4
# last_scale=2 scale=4
when: scale|int < last_scale|int
# - name : generate env variables
# shell : ./get_name_ip.sh {{ scale }}
- name: generate ip
script: ./get_name_ip.sh {{ scale }}
register: last_scale
- set_fact:
list_ip={{ last_scale.stdout.split("\r\n")|list }}
- name : generate file
template :
src: example.domaine.com.conf.j2
dest: example.domaine.com.conf
# - name: Verify Nginx config
# become: yes
# command: nginx -t
# changed_when: false
# - name: reload Nginx config
# become: yes
# command: nginx -s reload
# changed_when: false
- hosts : clean
gather_facts : no
tasks:
- name: Removing exited containers
shell: docker ps -a -q -f status=exited | xargs --no-run-if-empty docker rm --volumes
- name: Removing untagged images
shell: docker images | awk '/^<none>/ { print $3 }' | xargs --no-run-if-empty docker rmi -f
- name: Removing volume directories
shell: docker volume ls -q --filter="dangling=true" | xargs --no-run-if-empty docker volume rm