소개
이 실습에서는 Red Hat Enterprise Linux(RHEL) 시스템에 Ansible Core를 설치하는 방법을 배웁니다. Ansible은 시스템 관리 및 구성, 애플리케이션 배포, 복잡한 IT 워크플로우 오케스트레이션을 수행할 수 있는 강력한 자동화 도구입니다.
sudo 권한이 있는 dnf 패키지 관리자를 사용하여 핵심 Ansible 엔진과 명령줄 도구를 제공하는 ansible-core 패키지를 설치합니다. 설치 후에는 버전 확인 및 기본 명령 실행을 통해 Ansible이 올바르게 작동하는지 검증합니다.
이는 Red Hat Enterprise Linux 시스템을 다루는 시스템 관리자와 DevOps 엔지니어에게 필수적인 기술입니다.
dnf를 사용하여 Ansible Core 설치하기
이 단계에서는 dnf 패키지 관리자를 사용하여 ansible-core 패키지를 설치합니다. Ansible Core는 자동화 작업에 필요한 ansible, ansible-playbook 및 기타 핵심 명령줄 도구를 포함한 필수 Ansible 엔진을 제공합니다.
dnf (Dandified YUM) 패키지 관리자는 Red Hat Enterprise Linux에서 소프트웨어 패키지를 관리하는 표준 도구입니다. 소프트웨어 설치에는 관리자 권한이 필요하므로 sudo 명령을 사용해야 합니다.
다음 명령을 실행하여 자동 확인 옵션과 함께 Ansible Core를 설치합니다.
sudo dnf install ansible-core -y
-y 플래그는 모든 프롬프트에 자동으로 "yes"라고 응답하여 설치 과정을 비대화형으로 진행합니다. 시스템은 템플릿 처리를 위한 Jinja2와 YAML 처리를 위한 PyYAML을 포함하여 ansible-core와 그 Python 의존성 패키지들을 다운로드하고 설치합니다.
패키지 해결 및 설치 진행 상황을 보여주는 다음과 유사한 출력이 표시됩니다.
Updating Subscription Management repositories.
Last metadata expiration check: ...
Dependencies resolved.
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
ansible-core noarch 2.16.x-x.el9 rhel-9-appstream xx M
Installing dependencies:
python3-jinja2 noarch x.x.x-x.el9 rhel-9-appstream xxx k
python3-yaml x86_64 x.x.x-x.el9 rhel-9-appstream xxx k
...
Transaction Summary
================================================================================
Install XX Packages
Complete!
Ansible 설치 확인하기
Ansible Core 설치가 완료되었으므로, 버전을 확인하고 필수 명령줄 도구를 사용할 수 있는지 확인하여 설치가 성공했는지 검증해 보겠습니다.
먼저 다음 명령을 실행하여 Ansible 버전을 확인합니다.
ansible --version
이 명령은 핵심 버전, Python 버전 및 다양한 구성 요소의 위치를 포함하여 Ansible 설치에 대한 자세한 정보를 표시합니다. 다음과 같은 출력이 나타나야 합니다.
ansible [core 2.14.18]
config file = /etc/ansible/ansible.cfg
configured module search path = ['/home/labex/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python3.9/site-packages/ansible
ansible collection location = /home/labex/.ansible/collections:/usr/share/ansible/collections
executable location = /usr/bin/ansible
python version = 3.9.21 (main, Feb 10 2025, 00:00:00) [GCC 11.5.0 20240719 (Red Hat 11.5.0-5)] (/usr/bin/python3)
jinja version = 3.1.2
libyaml = True
각 항목의 의미는 다음과 같습니다.
- ansible [core 2.14.18]: 설치된 Ansible Core 버전
- config file: 기본 설정이 포함된 메인 Ansible 구성 파일 경로
- configured module search path: Ansible이 사용자 지정 모듈을 찾는 디렉토리
- ansible python module location: 핵심 Ansible Python 코드가 설치된 위치
- ansible collection location: Ansible 컬렉션(패키지화된 모듈 및 플러그인)이 저장된 디렉토리
- executable location: ansible 명령 바이너리의 실제 위치
- python version: Ansible이 사용하는 Python 인터프리터 버전
- jinja version: 동적 콘텐츠를 위해 Ansible이 사용하는 템플릿 엔진 버전
- libyaml = True: 더 나은 성능을 위해 빠른 YAML 파서를 사용할 수 있음을 확인
이는 Ansible이 올바르게 설치되어 사용할 준비가 되었음을 의미합니다. 다음으로 ansible-playbook 명령도 사용할 수 있는지 확인합니다.
ansible-playbook --version
Ansible 플레이북을 실행하는 데 필수적인 ansible-playbook 도구에 대해서도 유사한 버전 정보가 표시되어야 합니다.
간단한 명령으로 Ansible 테스트하기
이 단계에서는 로컬 시스템을 대상으로 간단한 명령을 실행하여 Ansible 설치를 테스트합니다. 프로젝트 디렉토리로 이동하여 미리 구성된 인벤토리 파일을 사용하여 Ansible 임시(ad-hoc) 명령을 실행합니다.
프로젝트 디렉토리로 이동하여 기본적인 ping 기능을 테스트합니다. 결과를 다시 확인하고 단계 검증기가 결과를 안정적으로 확인할 수 있도록 출력을 파일로 저장합니다.
cd /home/labex/project
ansible localhost -m ping | tee ping-output.txt
ping 모듈은 실제로 ICMP 패킷을 보내는 것이 아니라, Ansible이 대상에 연결하여 Python 코드를 실행할 수 있는지 확인합니다. 성공적인 응답은 다음과 같습니다.
localhost | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": false,
"ping": "pong"
}
출력 내용을 분석해 보겠습니다.
- localhost | SUCCESS: localhost 대상에서 명령이 성공적으로 실행되었음을 나타냅니다.
- ansible_facts: 실행 중에 발견된 시스템 정보가 포함됩니다.
- discovered_interpreter_python: Ansible이 찾아낸 사용할 Python 인터프리터 경로입니다.
- changed: false: 시스템에 변경 사항이 없음을 나타냅니다(ping은 읽기 전용 작업).
- ping: "pong": Ansible 연결이 확인되었음을 알리는 고전적인 응답입니다.
"pong" 응답은 Ansible이 올바르게 작동하며 대상 시스템과 통신할 수 있음을 확인해 줍니다.
또한 setup 모듈을 사용하여 시스템 정보를 수집하고 해당 출력을 저장하여 테스트해 보겠습니다.
ansible localhost -m setup -a "filter=ansible_distribution*" | tee setup-output.txt
이 명령은 setup 모듈을 사용하여 시스템 팩트(facts)를 수집하며, 특히 배포판 정보로 필터링합니다. Red Hat Enterprise Linux 시스템에 대한 세부 정보가 포함된 출력이 표시되어야 합니다.
localhost | SUCCESS => {
"ansible_facts": {
"ansible_distribution": "RedHat",
"ansible_distribution_file_parsed": true,
"ansible_distribution_file_path": "/etc/redhat-release",
"ansible_distribution_file_search_string": "Red Hat",
"ansible_distribution_file_variety": "RedHat",
"ansible_distribution_major_version": "9",
"ansible_distribution_release": "Plow",
"ansible_distribution_version": "9.6",
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": false
}
시스템 팩트 출력의 의미:
- ansible_distribution: Linux 배포판 이름 (RedHat)
- ansible_distribution_file_parsed: Ansible이 배포판 파일을 성공적으로 읽었는지 여부
- ansible_distribution_file_path: 배포판 정보가 포함된 파일
- ansible_distribution_file_search_string: 배포판을 식별하는 데 사용된 텍스트 패턴
- ansible_distribution_file_variety: 배포판 계열 (RedHat 계열)
- ansible_distribution_major_version: 메이저 버전 번호 (9)
- ansible_distribution_release: 릴리스 코드명 (Plow)
- ansible_distribution_version: 전체 버전 번호 (9.6)
- discovered_interpreter_python: Ansible이 발견한 Python 인터프리터
이는 Ansible이 대상 호스트로부터 시스템 정보를 성공적으로 수집할 수 있음을 확인시켜 주며, 이는 시스템 특성에 따른 조건부 자동화를 생성하는 데 필수적입니다.
사용 가능한 Ansible 모듈 탐색하기
Ansible은 다양한 자동화 작업을 위한 수백 개의 내장 모듈을 제공합니다. 설치 직후 사용할 수 있는 기능을 이해하기 위해 사용 가능한 모듈을 살펴보겠습니다.
사용 가능한 모듈 목록을 보려면 다음 명령을 실행하고 처음 몇 줄을 파일로 저장합니다.
ansible-doc -l | head -20 | tee module-list.txt
ansible-doc -l 명령은 사용 가능한 모든 모듈을 나열하며, head -20을 사용하여 처음 20개 모듈을 확인합니다. 이를 통해 Ansible의 광범위한 자동화 기능을 파악할 수 있습니다. 다음과 유사한 출력이 표시됩니다.
ansible.builtin.add_host Add a host (and alternatively a grou...
ansible.builtin.apt Manages apt-packages
ansible.builtin.apt_key Add or remove an apt key
ansible.builtin.apt_repository Add and remove APT repositories
ansible.builtin.assemble Assemble configuration files from fr...
ansible.builtin.assert Asserts given expressions are true
ansible.builtin.async_status Obtain status of asynchronous task
ansible.builtin.blockinfile Insert/update/remove a text block su...
ansible.builtin.command Execute commands on targets
ansible.builtin.copy Copy files to remote locations
ansible.builtin.cron Manage cron.d and crontab entries
ansible.builtin.debconf Configure a .deb package
ansible.builtin.debug Print statements during execution
ansible.builtin.dnf Manages packages with the `dnf' pack...
ansible.builtin.dpkg_selections Dpkg package selection selections
ansible.builtin.expect Executes a command and responds to p...
ansible.builtin.fail Fail with custom message
ansible.builtin.fetch Fetch files from remote nodes
ansible.builtin.file Manage files and file properties
ansible.builtin.find Return a list of files based on spec...
모듈 목록 형식 이해:
- ansible.builtin.: Ansible Core와 함께 제공되는 내장 모듈임을 나타냅니다.
- Module name: 플레이북이나 임시 명령에서 모듈을 호출할 때 사용하는 이름입니다.
- Description: 모듈이 수행하는 작업에 대한 간략한 설명입니다.
자주 사용하는 중요한 모듈:
- command: 대상 시스템에서 셸 명령 실행
- copy: 제어 노드에서 원격 호스트로 파일 복사
- dnf: Red Hat 시스템에서 패키지 설치, 업데이트 또는 제거
- file: 디렉토리 생성, 권한 설정 또는 파일 속성 관리
- debug: 문제 해결을 위해 플레이북 실행 중 메시지 출력
특정 모듈에 대한 자세한 문서를 보려면 모듈 이름과 함께 ansible-doc 명령을 사용할 수 있습니다. 명령이 비대화형으로 유지되고 나중에 검토할 수 있도록 출력을 파일로 리디렉션합니다. 예를 들어 copy 모듈에 대해 알아보려면 다음과 같이 합니다.
ansible-doc copy | tee copy-doc.txt
이 명령은 예제와 매개변수 설명을 포함하여 copy 모듈에 대한 포괄적인 문서를 표시하고 copy-doc.txt 파일에 저장합니다. ansible-doc 명령은 모든 Ansible 모듈에 대한 상세 문서를 제공하므로 다양한 자동화 기능을 쉽게 학습할 수 있습니다.
요약
이 실습에서는 Red Hat Enterprise Linux 시스템에 Ansible Core를 설치하고 확인하는 방법을 성공적으로 배웠습니다. 수행한 작업은 다음과 같습니다.
Ansible Core 설치:
sudo dnf install ansible-core -y명령을 사용하여 공식 Red Hat 저장소에서 핵심 Ansible 패키지와 의존성 패키지를 설치했습니다.설치 확인:
ansible및ansible-playbook명령의 버전을 확인하여 Ansible이 올바르게 설치되었는지 검증했습니다.기본 기능 테스트: 다음을 포함하여 설치가 올바르게 작동하는지 확인하기 위해 간단한 Ansible 명령을 실행했습니다.
ping모듈을 사용하여 연결 테스트setup모듈을 사용하여 시스템 팩트 수집
사용 가능한 모듈 탐색:
ansible-doc을 사용하여 방대한 Ansible 모듈 라이브러리를 찾고 문서를 읽는 방법을 배웠습니다.
이제 RHEL에 완전히 작동하는 Ansible 환경을 갖추었으며 인프라 자동화를 시작하는 데 필요한 기본 명령을 이해하게 되었습니다. 이 기초를 바탕으로 플레이북 작성, 인벤토리 관리, 복잡한 자동화 워크플로우 구현과 같은 더 고급 Ansible 주제로 나아갈 수 있습니다.


