-
Ansible) Ansible 환경 구성해보기 4편Cloud/Ansible 2022. 9. 23. 14:15
실습 환경 : AWS Console (2022/09), Visual Studio Code
실습 목적 : Playbook을 활용해서 Managed Node 관리
[Playbook] 아주 간단한 웹 환경 구현
- 이전에 ansible 명령으로 구현한 내용을 Playbook과 ansible-playbook 명령으로 구현해보기
- 이번 구현에서는 /home/ec2-user/work-ansible 디렉토리에서 작업하였습니다.
이전 웹서버 환경 초기화
# remove-webserver.yml --- - name: remove web server hosts: public gather_facts: False become: yes tasks: # managed node httpd stop - name: stop httpd service: name: httpd state: stopped # managed node delete httpd - name: remove httpd yum: name: httpd state: absent # managed node remove www directory - name: remove home directory file: path: /var/www state: absent
1. Playbook에 httpd 중지[service 모듈], httpd 삭제[yum 모듈], index파일 삭제[file 모듈] 를 위한 task입력
2. ansible-playbook remove-webserver.yml -i hosts 로 실행
Playbook으로 웹서버 환경 구성하기
# init_webserver.yml --- - name: install and running web server hosts: public gather_facts: False become: yes tasks: # managed node install httpd - name: install httpd yum: name: httpd state: present # managed node create html directory - name: create file file: path: /var/www/html state: directory # managed node copy index.html file - name: copy file copy: src: "{{file_path}}" dest: /var/www/html/index.html # managed node start httpd - name: start httpd systemd: name: httpd state: started register: result ignore_errors: True - name: state httpd debug: var: result
1. Playbook에 httpd 설치[yum 모듈], html 디렉토리 생성[file 모듈], index파일 복사[copy모듈], http 실행[systemd 모듈]를 위한 task입력
2. ansible-playbook init_webserver.yml -i hosts -e "file_path=../index.html" 로 실행[변수를 활용하여 Playbook 동작을 동적으로 수행하기]
- -e 옵션을 사용하면 코드에서 playbook에 파라미터를 넘겨줄수 있다.
- -e 옵션 사용시, 각파라미터는 스페이스바로 구분하여 작성한다.
public 그룹에 지정된 managed node의 퍼블릭 주소로 접속하면, index.html의 화면이 정상적으로 출력된다.
'Cloud > Ansible' 카테고리의 다른 글
Ansible) Playbook 2편 (0) 2022.09.25 Ansible) Playbook 1편 (0) 2022.09.22 Ansible) YAML (1) 2022.09.21 Ansible) Ansible 환경 구성해보기 3편 (0) 2022.09.21 Ansible) 모듈 (1) 2022.09.21