[ANSIBLE] - PLAYBOOK WITH WEBSERVER


playbook 구조

작동 싸이클


webserver.yml

1
2
3
4
5
6
7
8
9
10
11
12
13

---
- hosts: webservers
remote_user: root
become: no
vars:
git_user : nkyoon@xxx.com
git_password :
webapp_name: webserver
webapp_path: /home/WebServer
roles:
- yum_packages
- app.source

yum_packages/tasks/main.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
- name: nodejs repo
shell: "curl --silent --location https://rpm.nodesource.com/setup_10.x | sudo bash -"

- name: "Installing PKG"
yum:
state: present
name:
- git
- gcc-c++
- make
- nodejs

- name: install pm2
npm: name=pm2 global=true production=true

app.source/tasks/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
- name: pull from git
git:
repo: https://{{ git_user | urlencode }}:{{ git_password | urlencode }}@git.xxxxx.com/xxx/WebServer.git
dest: "{{ webapp_path }}"
update: yes
force: yes
version: develop

## npm 의존성 모듈을 설치
- name: install dependencies
shell: "cd {{ webapp_path }} && npm install "

## npm build -> dist
- name: npm build
shell: "cd {{ webapp_path }} && npm run-script build"

## 앱의 설치 상태를 확인
- name: check for webapp
shell: "pm2 show {{ webapp_name }}"
register: webapp_result
ignore_errors: true
become: true

## 기존의 앱을 종료
- name: stop webapp
shell: "pm2 stop {{ webapp_name }}"
when: "webapp_status.rc == 0"
ignore_errors: true
become: true

## 앱 실행
- name: start webapp
shell: "cd {{ webapp_path }} && pm2 start pm2-config.json"
become: true

배포 테스트

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
[root@tcid ansible]# ansible-playbook webserver.yml
PLAY [webservers] ******************************************************************************************************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************************************************************************************************
ok: [xxx.xxx.xxx.xxx]

TASK [yum_packages : nodejs repo] **************************************************************************************************************************************************************************
[WARNING]: Consider using the get_url or uri module rather than running 'curl'. If you need to use command because get_url or uri is insufficient you can add 'warn: false' to this command task or set
'command_warnings=False' in ansible.cfg to get rid of this message.

changed: [xxx.xxx.xxx.xxx]

TASK [yum_packages : Installing PKG] ***********************************************************************************************************************************************************************
ok: [xxx.xxx.xxx.xxx]

TASK [yum_packages : install pm2] **************************************************************************************************************************************************************************
ok: [xxx.xxx.xxx.xxx]

TASK [app.source : pull from git] **************************************************************************************************************************************************************************
changed: [xxx.xxx.xxx.xxx]

TASK [app.source : install dependencies] *******************************************************************************************************************************************************************
changed: [xxx.xxx.xxx.xxx]

TASK [app.source : npm build] ******************************************************************************************************************************************************************************

TASK [app.source : check for webapp] ***********************************************************************************************************************************************************************
changed: [xxx.xxx.xxx.xxx]

TASK [app.source : stop webapp] ****************************************************************************************************************************************************************************
fatal: [xxx.xxx.xxx.xxx]: FAILED! => {"msg": "The conditional check 'webapp_status.rc == 0' failed. The error was: error while evaluating conditional (webapp_status.rc == 0): 'webapp_status' is undefined\n\nThe error appears to have been in '/etc/ansible/roles/app.source/tasks/main.yml': line 25, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n## 기존의 앱을 종료\n- name: stop webapp\n ^ here\n"}
...ignoring

TASK [app.source : start webapp] ***************************************************************************************************************************************************************************
changed: [xxx.xxx.xxx.xxx]

PLAY RECAP *************************************************************************************************************************************************************************************************
xxx.xxx.xxx.xxx : ok=10 changed=6 unreachable=0 failed=0