Ansible 운영 - 3. Role 활용 및 예제 > Ansible 자료실

본문 바로가기

사이트 내 전체검색

뒤로가기 Ansible 자료실

운영 Ansible 운영 - 3. Role 활용 및 예제

페이지 정보

작성자 AnsibleM 작성일 20-10-22 15:19 조회 14,998 댓글 0

본문

1. Role

불필요하게 중복되는 소스를 제거하고 자주 사용하는 기능을 재사용하기 위해 Role로 생성 > 호출만 하면 됨

[Role 구조 예시]
README.md
defaults/
main.yml
files/
handlers/
main.yml
meta/
main.yml
tasks/
main.yml
templates/
tests/
inventory
test.yml
vars/
main.yml

- tasks : 해당 role에서 수행할 task 목록. 해당 role을 수행시키는 메인파일은 main.yml
- handlers : handler 정의
- defaults : 기본 변수 설정
- vars : task 수행 시 사용할 변수 정의 (defaults에서 정의한 변수보다 우선순위가 높음)
- files : 서버에 복사해야 할 파일이나 스크립트 파일 위치
- templates : jinja2 template
- meta : roles에 대한 정보, 호환 버전 등 메타 데이터 정의


2. Ansible-Role 사용 방법

1
2
3
$ mkdir roles
$ cd roles
$ ansible-galaxy init [ Role_name ]
cs



3. 예제

1) OS 버전별 task 수행

1
2
3
4
5
6
7
8
9
10
11
.
├── main_project.yml
├── roles
│   └── regular_inspection
│      ├── tasks
│      │   ├── 01-rhel5_monthly_regular_inspection.yml
│      │   ├── 02-rhel6_monthly_regular_inspection.yml
│      │   ├── 03-rhel7_monthly_regular_inspection.yml
│      │   ├── main.yml
│      └── vars
│          └── main.yml
cs
> roles 디렉토리 아래 사용하지 않는 폴더는 삭제 가능


1
2
3
4
5
6
7
8
9
10
11
$ cat ./main.yml
---
- name: main_os_monthly_regular_inspection
  hosts: all
  gather_facts: false
  pre_tasks:
   - setup:
       gather_subset: min
  roles:
    - role: os-monthly_regular_inspection
...
cs
> 실행할 role 지정


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$ cat ./roles/os-monthly_regular_inspection/tasks/main.yml
- name: OS-Check ( RHEL 5 )
  include_tasks: 01-rhel5_monthly_regular_inspection.yml
  when:
    - ansible_distribution_major_version == "5"
 
- name: OS-Check ( RHEL 6 )
  include_tasks: 02-rhel6_monthly_regular_inspection.yml
  when:
    - ansible_distribution_major_version == "6"
 
- name: OS-Check ( RHEL 7 )
  include_tasks: 03-rhel7_monthly_regular_inspection.yml
  when:
    - ansible_distribution_major_version == "7"
cs
> when 옵션을 활용해 OS 버전별 task 수행

댓글목록 0

등록된 댓글이 없습니다.

Copyright © 소유하신 도메인. All rights reserved.

사이트 정보

회사명 : (주)리눅스데이타시스템 / 대표 : 정정모
서울본사 : 서울특별시 강남구 봉은사로 114길 40 홍선빌딩 2층 / tel : 02-6207-1160
대전지사 : 대전광역시 유성구 노은로174 도원프라자 5층 / tel : 042-331-1161

PC 버전으로 보기