$ 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"
19 Apr 2020 in Devops on Ansible
Role 사용법
우리는 이전 시간에 nginx를 설치하는 yaml, jinja2 template, vars를 이미 정의했었다.
--- - name: Install nginx on the nodes hosts: nodes become: yes tasks: - name: nginx for Any Linux include_tasks: "\{\ lnx_name \}\}.yml" - name: Check nginx config command: "nginx -t" register: nginx - debug: msg="\{\{ nginx.stedrr_lines \}\}" - name: Check nginx service debug: msg="\{\{lookup('template','ins_chk.j2').split('\n')\}\}" handlers: - name: Restart nginx web server service: name=nginx state=restarted
tasks는 구조화되어있으며, handlers도 정의되어있는 것을 확인할 수 있다.
\{\% if ansible_distribution == 'Ubuntu' \%\} [ OS : Ubunt ] >> dpkg -l | grep nginx OR >> service nginx status \{\% elif ansible_distribution == 'CentOS' and ansible_distribution_major_version == '7' \%\} [ OS : CentOS ver7 ] >> yum list installed | grep nginx OR >> systemctl status nginx \{\% elif ansible_distribution == 'CentOS' and ansible_distribution_major_version < '7' \%\} [ OS : CentOS ver6 ] >> yum list installed | grep nginx OR >> systemctl status nginx \{\% else \%\} >> service nginx status(* Gernally) \{\% endif \%\}위의 j2 템플릿도 이미 구현을 했었다.
vars: lnx_name: "\{\{ 'Ubuntu' if ansible_distribution == 'Ubuntu' else 'CentOS' if ansible_distribution == 'CentOS' else 'Just Linux'\}\}"
전역 변수 또한 vars로 선언해두었다.
위의 각각의 파일들을 이전 시간에 살펴본 구조 폴더의 이름의 아래에 role 형식의 맞게 구성해서 집어넣으면 role을 만들 수 있다.
해당 기능이 필요하다면 해당 디렉터리 (vars,handlers 등 외 7가지) 아래에 main.yml로 정의한다.
이렇게 구성된 role은 아래와 같이 호출할 수 있다.
--- - name: Install nginx on the nodes hosts: nodes become: yes // 특정 사용자로 전환할 때 사용 roles: - { role: ./roles/ngix }
의문
우리는 지금까지 role을 사용하는 방법에 대해서 알아보았다.
그렇다면 왜 굳이 이렇게 role을 만들고 사용해야하는 것일까? 라는 근본적인 질문을 하게 될지도 모른다.
사실 role을 사용하는 가장 큰 이유는 다른 사람이 이미 정의한 role을 그대로 가져다 사용할 수 있다는 기능이 있기 때문이다.
Reference
인프런 조훈님 강의 - 앤서블을 깊이있게 활용하기