Ubuntu에서 Ansible을 설치하고 구성하는 방법은 무엇입니까?
더욱 향상된 환경 프로비저닝 및 구성 관리를 위해 Ubuntu에서 Ansible을 시작하는 방법을 안내합니다.
구성 관리는 DevOps 생명 주기에서 핵심적인 부분으로, IT 인프라의 자동화 및 오케스트레이션을 촉진합니다.
Puppet, Ansible, Chef, 그리고 SaltStack과 같은 여러 구성 관리 도구가 존재합니다. 그중에서도 Ansible은 DevOps 분야에서 가장 각광받는 도구 중 하나이며, 수많은 서버와 복잡한 IT 인프라를 효율적으로 관리할 수 있게 해줍니다.
이 글에서는 다음과 같은 내용을 상세히 다룹니다.
- Ansible 설치 과정
- SSH 키 교환 설정
- Ansible 클라이언트 구성
- Ansible 작동 테스트
Ansible 설치

실습의 편의를 위해 두 대의 서버를 이용하여 Ansible을 설정해보겠습니다. 하나는 Ansible 서버 역할을 하며, 다른 하나는 Ansible 클라이언트 역할을 합니다.
- Ansible 서버 – 10.0.0.1
- Ansible 클라이언트 – 10.0.0.25
설치 과정은 간단합니다. Ansible을 사용하고자 하는 모든 서버에서 아래 단계를 따라야 합니다. 여기서는 두 서버 모두에 적용합니다.
- Ansible 설치에 필요한 기본 소프트웨어를 설치합니다.
sudo apt install software-properties-common
- Ansible 패키지 저장소를 추가합니다.
sudo apt-add-repository --yes --update ppa:ansible/ansible
- APT 패키지 관리자를 업데이트합니다.
sudo apt update
- 마지막으로, Ansible을 설치합니다.
sudo apt install ansible
패키지 설치는 몇 분 안에 완료됩니다.
설치된 Ansible의 버전은 어떻게 확인할 수 있을까요?
간단합니다. `--version` 옵션을 사용하여 다음과 같이 확인할 수 있습니다.
ansible --version
ansible 2.8.1
config file = /etc/ansible/ansible.cfg
configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python2.7/dist-packages/ansible
executable location = /usr/bin/ansible
python version = 2.7.15+ (default, Nov 27 2018, 23:36:35) [GCC 7.3.0]
위 결과를 통해 Ansible 2.8.1 버전이 설치되었으며 설정 파일 위치, 파이썬 모듈 등 추가 정보를 확인할 수 있습니다.
다음 단계에서는 서버와 클라이언트 간의 원활한 통신을 위해 SSH 키 교환을 설정합니다.
SSH 키 교환
Ansible은 SSH(Secure Shell) 프로토콜을 사용하여 클라이언트와 연결합니다.
먼저 Ansible 서버에서 공개 키를 생성한 후, 해당 키를 Ansible 클라이언트에 복사해야 합니다.
루트 사용자로 로그인되어 있는지 확인합니다.
- `ssh-keygen` 명령을 사용하여 키를 생성합니다.
ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:cDapZBESo+8XcbXupbtILkFrklUSpwa70Y1c7yH5K1A [email protected]
The key's randomart image is:
+---[RSA 2048]----+
| =.+oo . |
| . B.B.= . |
| . o @oE + |
| . *oO * . |
| o++.S + . |
| .o +o . + |
| .o..o + |
| ..o o . |
| .o o. |
+----[SHA256]-----+
생성된 공개 키는 `.ssh` 폴더 안에 저장되며, 전체 경로는 `/root/.ssh/id_rsa.pub`입니다.
주의: 개인 키와 공개 키 파일은 다른 사용자가 읽을 수 없도록 권한 설정을 확인해야 합니다. 아래 명령으로 파일 목록을 확인하여 권한 설정을 확인할 수 있습니다.
cd /root/.ssh
ls -l
-rw------- 1 root root 1679 Jun 19 00:37 id_rsa
-rw------- 1 root root 404 Jun 19 00:37 id_rsa.pub
만약 권한 설정이 잘못되어 있다면 `chmod` 명령어를 사용하여 수정할 수 있습니다.
예:
chmod 400 id_rsa
chmod 400 id_rsa.pub
이제 생성된 공개 키를 Ansible 클라이언트(IP 주소: 10.0.0.25)에 복사해 보겠습니다.
ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '10.0.0.25 (10.0.0.25)' can't be established.
ECDSA key fingerprint is SHA256:eXduPrfV0mhxUcpsZWg+0oXDim7bHb90caA/Rt79cIs.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
[email protected]'s password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh '[email protected]'"
and check to make sure that only the key(s) you wanted were added.
위 결과에서 1개의 키가 성공적으로 추가된 것을 확인할 수 있습니다. 이는 SSH 키 교환이 성공적으로 완료되었음을 의미합니다.
다음으로 Ansible 클라이언트 설정을 진행합니다.
Ansible 클라이언트 설정
이전 단계에서 Ansible 클라이언트 서버에 Ansible 설치를 완료했다고 가정합니다.
클라이언트 또는 호스트 설정은 Ansible 서버가 클라이언트를 인식하도록 하는 과정입니다. 이를 위해서는 다음과 같이 진행합니다.
- Ansible 서버에 로그인합니다.
- `/etc/ansible` 디렉토리로 이동합니다.
- 선호하는 편집기를 사용하여 `hosts` 파일에 다음 내용을 추가합니다.
[Client]
node1 ansible_ssh_host=10.0.0.25
Ansible 테스트
모든 단계를 올바르게 수행했다면 Ansible 서버에서 다음 명령을 실행했을 때 `SUCCESS` 메시지를 받을 수 있습니다.
ansible -m ping Client
node1 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": false,
"ping": "pong"
}
위 ping 테스트는 클라이언트와의 연결을 확인하여 통신 상태가 양호한지 검증합니다.
결론
이 가이드가 Ansible 설치 및 사용을 시작하는 데 도움이 되기를 바랍니다. 더 많은 Ansible 관련 튜토리얼을 찾아보시거나 Udemy의 Mastering Ansible 강좌를 확인해 보십시오.