본문 바로가기

Linux/RHCE

[RHCE]System role 생성(custom) 및 실행

6. System role 생성하기 - custom

문제 및 풀이

<문제>

1. 다음 요구 사항에 따라 /home/matthew/ansible/role에 apache라는 role을 만듭니다.
- httpd 패키지를 설치하고 시작할 때 활성화 한 다음 시작합니다.
- 방화벽이 활성화되고 웹 서버에 대한 액세스를 허용하는 규칙으로 실행됩니다.
    /var/www/html/index.html 파일을 만드는데 
   사용되는 index.html.j2 템플릿 파일이 있으며 출력은 다음과 같습니다.
    Welcome to {{ FQDN }} on {{ IPADDRESS }}
   (실제 문제에서는 {{ HOSTNAME }} 으로 써있는데, 
    영어로 문제를 잘 보면 fully qualified domain name 을 입력하라고 한다. 
    즉 FQDN을 입력해야 함)
 
2. 해당 role을 실행하는 /home/matthew/ansible/newrole.yml 파일을 생성한다.
- 스크립트는 webservers 호스트 그룹의 호스트에서 실행됩니다.

 

<풀이>

ansible-galaxy init apache2

vi index.html.j2 (template 폴더안)
Welcome to {{ ansible_fqdn }} on {{ ansible_default_ipv4['address'] }}
#doc 확인
ansible-doc yum
ansible-doc service
ansible-doc firewalld
ansible-doc template

vi main.yml (Tasks 폴더안 수정)
- name: download httpd
  yum:
    name: httpd
    state: present
- name: start httpd
  service:
    name: httpd
    state: started
    enabled: yes
- name: start firewalld
  service:
    name: firewalld
    state: started
    enabled: yes
- name: permit firewalld
  firewalld:
    service: http
    state: enabled
    immediate: yes
    permanent: yes
- name: using template
  template:
    src: index.html.j2
    dest: /var/www/html/index.html
    setype: httpd_sys_content_t
#vi newrole.yml
- hosts: webservers
  roles:
    - role: apache

ansible-playbook newrole.yml
# 접속 가능한지 확인
curl http://webservers_호스트명/index.html

 


7. System role - 다운받은 role 실행하기(5번이랑 연관)

문제 및 풀이
다운받았던 2개의 role (balancer, phpinfo) 을 설치하는 roles.yml 생성 및 아래와 같이 배포한다.

- 호스트 그룹 balancers에 balancer role 배포
- 호스트 그룹 webservers에 phpinfo role 배포
vi roles.yml
- hosts: webserver
  roles:
    - role: phpinfo
- hosts: balancers
  roles:
    - role: balancer

ansible-playbook roles.yml