RHCE 9 연습/기출문제 (EX294)
 · 약 29분
Lab환경구성
| FQDN | Description | IP Addresses | Roles | 
|---|---|---|---|
| control.lab.example.com | control | 172.25.250.254 | ansible control node | 
| classroom.lab.example.com | classroom | 172.25.250.254 | materials | 
| content.lab.example.com | content | 172.25.250.254 | YUM repo | 
| node1.lab.example.com | node1 | 172.25.250.9 | ansible managed node | 
| node2.lab.example.com | node2 | 172.25.250.10 | ansible managed node | 
| node3.lab.example.com | node3 | 172.25.250.11 | ansible managed node | 
| node4.lab.example.com | node4 | 172.25.250.12 | ansible managed node | 
| node5.lab.example.com | node5 | 172.25.250.13 | ansible managed node | 
| utility.lab.example.com | utilit | 172.25.250.220 | utility | 
1. Ansible 설치 및 구성
제어 노드 control에서 에 Ansible을 다음과 같이 구성하시오.
- 필요한 패키지 설치.
- /home/greg/ansible/inventory라는 정적 인벤토리 파일을 생성하고 다음과 같이 구성합니다:
- node1은 dev 호스트 그룹의 구성원입니다.
- node2는 test 호스트 그룹의 구성원입니다.
- node3 및 node4는 prod 호스트 그룹의 구성원입니다.
- node5는 balancers 호스트 그룹의 구성원입니다.
- prod 그룹은 webservers 호스트 그룹의 구성원입니다.
 
- 구성 파일 /home/greg/ansible/ansible.cfg 을 생성하고 다음과 같이 구성합니다:
- 호스트 인벤토리 파일은 /home/greg/ansible/inventory입니다.
- 본 컨텐츠 컬렉션 디렉토리는 /home/greg/ansible/roles입니다.
- 기본 컨텐츠 컬렉션 디렉토리는 /home/greg/ansible/mycollection입니다.
 
# control노드 접속
ssh greg@control
# ansible 패키지 설치
sudo dnf -y install ansible-automation-platform-common.noarch ansible-navigator
# 디렉토리 생성
mkdir -p /home/greg/ansible/roles
mkdir /home/greg/ansible/mycollection
cd ansible/
# 구성파일 생성
ansible-config init --disabled > /home/greg/ansible/ansible.cfg
vim ansible.cfg
[defaults]
inventory = /home/greg/ansible/inventory
remote_user = greg
host_key_checking = False
roles_path = /home/greg/ansible/roles:/usr/share/ansible/roles
collections_path = ./mycollection/:.ansible/collections:/usr/share/ansible/collections
[privilege_escalation]
become=True
# 구성파일 확인
ansible --version
ansible-galaxy list
# inventory파일 생성
vim /home/greg/ansible/inventory
[dev]
node1
[test]
node2
[prod]
node3
node4
[balancers]
node5
[webservers:children]
prod
# inventory 확인
ansible-inventory --graph
# ping테스트
ansible all -m ping
시험과정에 ansible-navigator 사용가능,ansible-navigator사용시 podman 미리 로그인해보도록
podman login utility.lab.example.com -u admin -p redhat
ansible-navigator images
ansible-navigator collections
2. YUM저장소 생성
시스템 관리자로서 관리 대상 노드에 소프트웨어를 설치해야 합니다.
다음 작업을 수행하는 /home/greg/ansible/yum_repo.yml 플레이북을 생성하세요. 각 관리되는 노드에 다음 yum 저장소를 만듭니다.
- 
저장소 1 구성: - 이름: EX294_BASE
- 설명: EX294 base software
- 기본 URL: http://content/rhel9.0/x86_64/dvd/BaseOS
- GPG 서명 확인: 활성화
- GPG 키 URL: http://content/rhel9.0/x86_64/dvd/RPM-GPG-KEY-redhat-release
- 저장소 상태: 활성화
 
- 
저장소 2 구성: - 이름: EX294_STREAM
- 설명: EX294 stream software
- 기본 URL: http://content/rhel9.0/x86_64/dvd/AppStream
- GPG 서명 확인: 활성화
- GPG 키 URL: http://content/rhel9.0/x86_64/dvd/RPM-GPG-KEY-redhat-release
- 저장소 상태: 활성화
 
# 모듈명 검색
ansible-doc -l | grep yum
# doc
ansible-doc yum_repository
# playbook
vim /home/greg/ansible/yum_repo.yml
---
- name: Configure YUM repositories
  hosts: all
  tasks:
    - name: Configure EX294_BASE repository
      yum_repository:
        file: EX294_BASE
        name: EX294_BASE
        description: "EX294 base software"
        baseurl: http://content/rhel9.0/x86_64/dvd/BaseOS
        gpgcheck: yes
        gpgkey: http://content/rhel9.0/x86_64/dvd/RPM-GPG-KEY-redhat-release
        enabled: yes
    - name: Configure EX294_STREAM repository
      yum_repository:
        file: EX294_STREAM
        name: EX294_STREAM
        description: "EX294 stream software"
        baseurl: http://content/rhel9.0/x86_64/dvd/AppStream
        gpgcheck: yes
        gpgkey: http://content/rhel9.0/x86_64/dvd/RPM-GPG-KEY-redhat-release
        enabled: yes
# playbook실행
ansible-navigator run yum_repo.yml -m stdout
# 검증
ansible all -a 'yum repoinfo'
ansible all -a 'yum -y install ftp'
ansible all -a 'rpm -q ftp'
3.패키지 설치
다음 요구 사항에 따라 /home/greg/ansible/packages.yml 플레이북을 생성하고 관리 대상 노드에 소프트웨어 패키지를 설치하세요.
- php와 mariadb 패키지를 dev, test, prod 호스트 그룹의 노드에 설치합니다.
- RPM Development Tools 패키지 그룹을 dev 호스트 그룹의 노드에 설치합니다.
- dev 호스트 그룹의 모든 패키지를 최신 버전으로 업데이트합니다.
# doc
ansible-doc yum
# playbook작성
vim /home/greg/ansible/packages.yml
---
- name: Install php and mariadb
  hosts: dev,test,prod
  tasks:
    - name: Install required packages
      yum:
        name: 
          - php
          - mariadb
        state: present
- name: Install RPM Development Tools and upgrade packages
  hosts: dev
  tasks:
    - name: Install RPM Development Tools group
      yum:
        name: "@RPM Development Tools"
        state: present
    - name: Upgrade all packages to the latest version
      yum:
        name: "*"
        state: latest
# playbook실행
ansible-navigator run packages.yml -m stdout
# 검증
ansible dev,test,prod -a 'rpm -q php mariadb'
ansible dev -a 'yum grouplist'
ansible dev -a 'yum update'
4.RHEL 시스템 역할(ROLE) 사용하기
다음 요구 사항을 충족하는 /home/greg/ansible/selinux.yml 플레이북을 작성하세요:
- 모든 관리 대상 노드에서 실행됩니다.
- selinux role을 사용합니다.
- SElinux 정책을 targeted으로 구성합니다.
- SElinux 상태를 enforcing으로 설정합니다.
# 패키지 검색
yum search role
# 설치
sudo yum -y install rhel-system-roles
# 사용 가능한 system roles 확인
ansible-galaxy list
# 예제 playbook 가져다 편집하여 사용하는게 편함
cp /usr/share/doc/rhel-system-roles/selinux/example-selinux-playbook.yml /home/greg/ansible/selinux.yml
vim selinux.yml
# 행 번호 표시 및 필요 없는 내용 삭제 (버전에 따라 행 번호  가 다를 수 있으므로 직접 확인 후 삭제하도록)
:set nu
:43,51d
:11,39d
최종 내용:
---
- hosts: all
  become: true
  become_method: sudo
  become_user: root
  vars:
    # Use "targeted" SELinux policy type
    selinux_policy: targeted
    # Set "enforcing" mode
    selinux_state: enforcing
  # Prepare the prerequisites required for this playbook
  tasks:
    - name: execute the role and catch errors
      block:
        - name: Include selinux role
          include_role:
            name: rhel-system-roles.selinux
      rescue:
        # Fail if failed for a different reason than selinux_reboot_required.
        - name: handle errors
          fail:
            msg: "role failed"
          when: not selinux_reboot_required
        - name: restart managed host
          reboot:
        - name: wait for managed host to come back
          wait_for_connection:
            delay: 10
            timeout: 300
        - name: reapply the role
          include_role:
            name: rhel-system-roles.selinux
# playbook실행
# rpm 패키지로 설치된 역할은 ansible-playbook으로 실행, collection으로 설치된 역할은 ansible-navigator로 실행
ansible-playbook selinux.yml
# 검증
ansible all -m shell -a 'grep ^SELINUX= /etc/selinux/config; getenforce'
node3 | CHANGED | rc=0 >>
SELINUX=enforcing
Enforcing
node2 | CHANGED | rc=0 >>
SELINUX=enforcing
Enforcing
node5 | CHANGED | rc=0 >>
SELINUX=enforcing
Enforcing
node1 | CHANGED | rc=0 >>
SELINUX=enforcing
Enforcing
node4 | CHANGED | rc=0 >>
SELINUX=enforcing
Enforcing
5.Collection 설치
- 다음 컬렉션 아티팩트를 http://classroom/materials/ 에서 가져와 control노드에 설치하십시오.
- redhat-insights-1.0.7.tar.gz
- community-general-5.5.0.tar.gz
- redhat-rhel_system_roles-1.19.3.tar.gz
 
- 컬렉션은 기본 컬렉션 디렉토리인 /home/greg/ansible/mycollection에 설치해야 합니다.
vim requirements.yml
---
collections:
  - name: http://classroom/materials/redhat-insights-1.0.7.tar.gz
  - name: http://classroom/materials/community-general-5.5.0.tar.gz
  - name: http://classroom/materials/redhat-rhel_system_roles-1.19.3.tar.gz