[ANSIBLE] Apache 설치 Role 만들고 실행하기

Posted by Albert 285Day 22Hour 3Min 7Sec ago [2024-07-08]

1. 우선 롤부터 생성한다.(arole) 

[root@asimblecontroller ansible]' ansible-galaxy role init arole
- Role arole was created successfully
[root@asimblecontroller ansible]' tree arole
arole
├── README.md
├── defaults
│   └── main.yml
├── files
├── handlers
│   └── main.yml
├── meta
│   └── main.yml
├── tasks
│   └── main.yml
├── templates
├── tests
│   ├── inventory
│   └── test.yml
└── vars
└── main.yml

2. Role 변수설정

[root@asimblecontroller ansible]' vi arole/vars/main.yml
---
' vars file for arole
service_name: httpd
src_file_path: ../files/index.html
dest_file_path: /var/www/html
httpd_packages:
- httpd-tools
- httpd
supported_distors:
- CentOS
- RedHat

3. apache 재시작 handlers 만들기

[root@asimblecontroller ansible]' vi arole/handlers/main.yml
---
' handlers file for arole

- name: restart service
ansible.builtin.service:
name: "{{ service_name }}"
state: restarted

4. default 변수 생성 

---
' defaults file for arole
service_title: "Apache web service"

5. index.html 파일 생

[root@asimblecontroller ansible]' vi arole/files/index.html

내용은 

Hello albert

6. 수행할 task 생성

(수행할 작업은 우선 http yum 설치 실행후 files/index.html 파일을 /var/www/html 폴더로 복사이동처리후 apache 서비스를 재시작시크는 작업이다.)

[root@asimblecontroller ansible]' vi arole/tasks/main.yml

' tasks file for arole

- name: install service {{ service_title }}
ansible.builtin.yum:
name: "{{ item }}"
state: latest
loop: "{{ httpd_packages }}"
when: ansible_facts.distribution in supported_distors

- name: copy html file
ansible.builtin.copy:
src: "{{ src_file_path }}"
dest: "{{ dest_file_path }}"
notify:
- restart service

7. role을 실행할 playbook만들기

- hosts: web

tasks:
- name: Print start play
ansible.builtin.debug:
msg: "Play Apache setup role"

- name: Install service by role
ansible.builtin.import_role:
name: arole

8. 실행결과

[root@asimblecontroller ansible]' ansible-playbook httpdSetup.yml

PLAY [web] **********************************************************************************

TASK [Gathering Facts] **********************************************************************
ok: [tnode1]

TASK [Print start play] *********************************************************************
ok: [tnode1] => {
"msg": "Start role play"
}

TASK [arole : install service Apache web service] *******************************************
ok: [tnode1] => (item=httpd-tools)
ok: [tnode1] => (item=httpd)

TASK [arole : copy html file] ***************************************************************
changed: [tnode1]

RUNNING HANDLER [arole : restart service] ***************************************************
changed: [tnode1]

PLAY RECAP **********************************************************************************
tnode1 : ok=5 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0


tnode1 서버에서 http://localhost 로 접속시 조금전 만든 index.html 내용이 출력되는부분 확인할수있다.

 




LIST

Copyright © 2014 visionboy.me All Right Reserved.