-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.yml
196 lines (175 loc) · 5.7 KB
/
main.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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# kam sa pripajame
- hosts: workshop
# mozeme definovat aj premenne
vars:
# cesta k aplikacii, ktoru budeme dev-opovat
app_path: /opt/ansible-workshop
app_http_port: 8090
# doplnime premenne pre nginx
nginx_http_port: 80
nginx_max_conns: 1024
nginx_client_max_body_size: 1M
# pod akym uzivatelom
remote_user: workshop
# definicia handlerov
handlers:
# identifikacia tasku v handleroch je 'name'
- name: restart nginx
become: yes
# v tasku pouzivame tiez moduly
service:
name: nginx
state: restarted
- name: daemon-reload
become: yes
systemd:
daemon_reload: yes
- name: restart ansible-workshop
become: yes
service:
name: ansible-workshop
state: restarted
# pred taskami
#pre_tasks:
# role: vykonanie celistvej a komplexnej podulohy
# konfig v samostatnom adresari... vnorenie ineho playbooku
#roles:
# tasky
tasks:
# volitelna polozka, pri vykonavani krokov sa zobrazi na monitor,
# co sa prave vykonava
- name: ensure all necessary packages are present
# become moze byt 'yes' alebo 'true'
become: yes
# ideme instalovat balicky
apt:
# ideme spravit loop... cez 'item'
name: '{{ item }}'
# chceme pritomne balicky, ak treba, sa aktualizuje na najnovsi
# POZOR: pre produkciu skor iba 'present', aby sa nerozbil system!
# POZOR: 'latest' skusat na testovacej masine!
state: latest
# aktualizovat cache balickov
update_cache: yes
# chceme tento taks vykonat pomocou itemov
with_items:
# definovanie, ake balicky sa maju nainstalovat
# vratane zavislosti
- apt-transport-https
- curl
- gnupg2
- python3
- python3-pip
# dalsi task, vytvorenie dir pre app
- name: ensure application directory is present
become: yes
# pouzijeme modul file: praca s fs, mame vstavane premenne k dispozicii...
file:
# premenna nemusi byt s medzerou, ale je to citatelnejsie...
path: '{{ app_path }}'
owner: workshop
group: workshop
mode: 0750
# vytvorime adresar, ale vieme aj ine...
# 'link' (potrebujeme 'source', 'destination'), 'file', 'directory'
state: directory
# task na stiahnutie app z gitu
- name: checkout app from git repo
# modul git...
git:
# skadial
repo: "https://github.com/Pytlicek/pyconsk-ansible-workshop.git"
# kam
dest: '{{ app_path }}'
clone: yes
# ak by sme sli cez ssh a nechceme potvrdzovat rucne pridanie kluca
accept_hostkey: yes
# update python balickov pre aplikaciu; balicky su definovane v subore
- name: ensure all pip packages are present
# modul pip
pip:
# v tomto cieli bude zoznam balickov pre instalaciu app
requirements: '{{ app_path }}/requirements.txt'
# tento atribut je sice default 'present', ale radsej sa uistime...
state: present
# vieme aj priamo povedat, ktory pip pouzijeme
executable: /usr/bin/pip3
# potrebujeme nainstalovat reverznu proxy nginx
# na verifikaciu balickov nginx
- name: add apt signing key for nginx repo
become: yes
# modul pre spravu klucov apt
apt_key:
url: "https://nginx.org/keys/nginx_signing.key"
state: present
# podmienene vykonany prikaz!
when:
# iba ak je cielova distro ubuntu
- ansible_distribution == "Ubuntu"
# existencia repozitara nginx
- name: ensure nginx repo is present
become: yes
# modul na spravu repozitara
apt_repository:
# odkial
repo: 'deb https://nginx.org/packages/mainline/ubuntu/ {{ ansible_distribution_release }} nginx'
state: present
# v ktorom subore bude repozitar
filename: 'nginx'
# update kesky
update_cache: yes
# medzi riadkami je logicky 'and' pri podmienkach!
when:
- ansible_distribution == "Ubuntu"
# skratene vyhodnocovanie! ked pouzivame premenne, pouuziva sa skratene vyhodnocovanie
#- var_is_true
# instalacia nginx
- name: ensure ngx pkg is present
become: yes
apt:
name: nginx
state: latest
# ak jestvuje na masine nginx, chceme ho restartnut...
- name: ensure nginx is running and enabled on boot
become: yes
# module na pracu so sluzbami
service:
name: nginx
# chceme, aby bezala nastartovana
state: started
# chceme, aby sa po kadom starte spustala
enabled: yes
# vytvorime system service pre nasu flask app
- name: create flask service from template
become: yes
# modul pre pracu s template
template:
src: ansible-workshop.service
dest: "/etc/systemd/system/ansible-workshop.service"
owner: root
group: root
# zaloha pre istotu
backup: yes
# po zmene sluzby chceme restartnut sluzbu, na to sa pouzivaju handlery
# handler moze byt aj v tomto konfiguraku, nielen v samostatnom subore, vid hore
# jeden na systemd, druhy na restartovanie aplikacie
# takze notifikujeme callbaky
notify:
- daemon-reload
- restart ansible-workshop
# task pre restart nginx
- name: create nginx config from template
become: yes
template:
src: nginx.conf.j2
dest: "/etc/nginx/nginx.conf"
owner: root
group: nginx
mode: 0640
backup: yes
# validacia pred spustenim
validate: "/usr/sbin/nginx -c %s -t"
notify:
- restart nginx
# po taskoch
post_tasks: