728x90

앤서블 개념


정의

- 오픈 소스 IT 자동화 도구

- 시스템 구성 및 소프트웨어 배포 기능 제공

- 지속적인 배포와 다운 타임 없는 롤링 업데이트 지원

- 주요 목적은 단순성과 사용의 용이성

- YAML 언어 사용

- 분산 구조 설계

- Kerberos / LDAP 등 인증 관리 시스템에 연결이 쉬움

 

장점

- SSH를 통한 구성 (에이전트x)

- YAML 언어를 사용해 접근성이 높음

- 쉽고 단순한 구조, 가독성이 높음

- 변수 기능 사용으로 재사용성 증가

- 다른 도구보다 간소화됨

- 다양한 플랫폼 지원

 

단점

- 다른 도구에 비해 덜 강력함

- DSL을 통한 로직 수행으로 수시로 문서 확인 필요

- 변수 등록으로 인한 복잡성

- 변수 값 확인의 어려움

- 입/출력/구성 파일 간의 형식 일관성이 없음

- 성능 속도 저하

 

구성

- YAML 언어로 구성된 텍스트 파일

- 하나 이상의 작업 내용의 집합

 

동작

- 각 작업은 특정 인수와 함께 작은 코드 조각인 모듈 실행

- 시스템의 특정 측면이 특정 상태에 있는지 확인

- 특정 상태가 아닌 경우 해당 상태를 구성하기 위한 작업 실행

- 작업 실패 시 기본적으로 나머지 동작도 모두 중단

- 별도의 에이전트 없이 ssh 또는 WinRM을 이용한 연결 및 제어

 


앤서블 아키텍처


컨트롤 머신 (제어 노드)

- 설치 및 실행

- 원격으로 관리 노드 제어

- 프로젝트 파일의 사본 보관

- Python v2.6 이상 필요

 

관리 대상 호스트 (관리 노드)

- 컨트롤 머신을 통해 모듈 설치 및 작업 실행

- 인벤토리에 나열된 대상

- 단독 혹은 그룹으로 관리 가능

- Python v2.4 이상 ssh 필요

 

아키텍처

 

 


 

실습 환경


 

아키텍처

- 제어 노드(control-node)

- 관리 호스트(host1, host2, host3, host4)

 

 

 

 

앤서블 구성

 

1. vagrant 설치

https://www.vagrantup.com/

 

Vagrant by HashiCorp

Vagrant enables users to create and configure lightweight, reproducible, and portable development environments.

www.vagrantup.com

 

2. Vagrantfile 다운로드

- 공유 파일

 

3. Vagrantfile 파일 실행

 

  1) 플러그인 설치: vagrant plugin install vagrant-hostmanager

  2) 가상 머신 배포: vagrant up

    - 에러 발생 1: undefined method `old_compare' for nil:NilClass (NoMethodError)

    - 해결

PS C:\HashiCorp\ansible> vagrant.exe plugin uninstall hostmanager
Uninstalling the 'hostmanager' plugin...
Removing hm
Successfully uninstalled hostmanager-1.2.2
Successfully uninstalled rubyhacks-0.1.5

 

- 에러 발생 2: umount: /mnt: not mounted

- 해결

https://www.devopsroles.com/vagrant-no-virtualbox-guest-additions-installation-found-fixed/

PS C:\HashiCorp\ansible> vagrant plugin uninstall vagrant-vbguest
PS C:\HashiCorp\ansible> vagrant plugin install vagrant-vbguest --plugin-version 0.21

 

  3) 실행 중인 가상 머신 확인: vagrant status

 

 

- 에러 발생 3: Vagrantfile에 네트워크 설정을 했음에도 불구하고 static 설정이 되지 않음

- 해결

# 플러그인 버전 확인
# 낮은 버전이라면 업데이트 필요

PS C:\HashiCorp\ansible> vagrant plugin update
Updating installed plugins...
Fetching vagrant-vbguest-0.30.0.gem
Successfully uninstalled vagrant-vbguest-0.21.0
Updated 'vagrant-vbguest' to version '0.30.0'!


PS C:\HashiCorp\ansible> vagrant plugin list
vagrant-hostmanager (1.8.9, global)
  - Version Constraint: > 0
vagrant-vbguest (0.30.0, global)
  - Version Constraint: > 0

 

 

4. 원격 접속

PS C:\HashiCorp\ansible> vagrant ssh control-node
[vagrant@localhost ~]$

 

5. 앤서블 설치

control-node에만 설치하면 됨

[vagrant@control ~]$ sudo yum install -y epel-release
[vagrant@control ~]$ sudo yum install -y ansible

 

6. SSH 설정 (모든 머신에 설정 필요)

기존에는 ssh 키 기반 인증으로 접속하여 패스워드 인증 사용 불가

-> 설정 변경 후에는 패스워드 인증을 사용한 원격 접속 가능

[vagrant@control ~]$ sudo vi /etc/ssh/sshd_config

# no > yes 로 변경
65 PasswordAuthentication yes

[vagrant@control ~]$ sudo systemctl restart sshd

 

7. 호스트 파일 확인

[vagrant@control-node ~]$ sudo cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
127.0.1.1 control-node control-node

## vagrant-hostmanager-start
192.168.56.100  control-node

192.168.56.110  host1

192.168.56.120  host2

192.168.56.130  host3

192.168.56.140  host4

## vagrant-hostmanager-end

 

8. 패스워드 없이 원격 접속 설정

# 모든 가상 머신에서 사용자 생성
# 사용자 생성
[vagrant@control ~]$ sudo useradd ansible

# 패스워드 설정
[vagrant@control ~]$ sudo passwd ansible

# 패스워드 입력 없이 원격 접속하도록 설정
# 1. 컨트롤 노드에서 ssh 생성
# 2. 호스트로 복사 (host1, host2, host3, host4)

[vagrant@control-node ~]$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/vagrant/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/vagrant/.ssh/id_rsa.
Your public key has been saved in /home/vagrant/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:3lhOyiezamELTdrmy5HRbx7pw02DesuW6EcwACqeJhY vagrant@control-node
The key's randomart image is:
+---[RSA 2048]----+
|    ..           |
|   .  .          |
|.E.    .         |
|..o   ..o        |
|.=   =. Soo.     |
|+   o *= Oo.o    |
|     =ooB=O+ .   |
|     .+.oXB..    |
|     .++o+=o     |
+----[SHA256]-----+


# host[1..4] 설정
[vagrant@control-node ~]$ ssh-copy-id ansible@host1
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/vagrant/.ssh/id_rsa.pub"
/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
ansible@host1's password:

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'ansible@host1'"
and check to make sure that only the key(s) you wanted were added.

[vagrant@control-node ~]$ ssh ansible@host1
Last failed login: Tue Jun  7 09:33:07 UTC 2022 from 192.168.56.100 on ssh:notty
There were 25 failed login attempts since the last successful login.
[ansible@host1 ~]$

 

host4에서 에러 발생: ansible 사용자 생성 시 디렉토리가 자동으로 생성되지 않음

[vagrant@control-node ~]$ ssh-copy-id ansible@host4
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/vagrant/.ssh/id_rsa.pub"
/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
ansible@host4's password:
Could not chdir to home directory /home/ansible: No such file or directory
sh: 1: cd: can't cd to /home/ansible
mkdir: cannot create directory ‘.ssh’: Permission denied

vagrant@host4:~$ ls -l /home
total 8
drwxr-xr-x 3 ubuntu  ubuntu  4096 Jun  7 07:36 ubuntu
drwxr-xr-x 4 vagrant vagrant 4096 Jun  7 07:51 vagrant

 

해결: 디렉토리 수동 생성

vagrant@host4:~$ sudo mkdir /home/ansible
vagrant@host4:~$ sudo chown ansible:ansible /home/ansible
vagrant@host4:~$ sudo chmod 755 /home/ansible
vagrant@host4:~$ sudo ls -l /home
total 12
drwxr-xr-x 2 ansible ansible 4096 Jun  7 09:44 ansible
drwxr-xr-x 3 ubuntu  ubuntu  4096 Jun  7 07:36 ubuntu
drwxr-xr-x 4 vagrant vagrant 4096 Jun  7 07:51 vagrant

 

 

8. sudo 설정

패스워드 없이 sudo 명령어 사용 설정

[vagrant@control-node ~]$ sudo ls /etc/sudoers.d
vagrant
[vagrant@control-node ~]$ sudo cat /etc/sudoers.d/vagrant
%vagrant ALL=(ALL) NOPASSWD: ALL
[vagrant@control-node ~]$
[vagrant@control-node ~]$ sudo grep wheel /etc/sudoers
## Allows people in group wheel to run all commands
%wheel  ALL=(ALL)       ALL
# %wheel        ALL=(ALL)       NOPASSWD: ALL


# 모든 가상 머신에서 파일 생성
[vagrant@control-node ~]$ sudo vi /etc/sudoers.d/ansible
ansible ALL=(ALL) NOPASSWD: ALL

 

ansible 파일 생성 전

[vagrant@control-node ~]$ ssh ansible@host1 "sudo id"

We trust you have received the usual lecture from the local System
Administrator. It usually boils down to these three things:

    #1) Respect the privacy of others.
    #2) Think before you type.
    #3) With great power comes great responsibility.

sudo: no tty present and no askpass program specified

 

ansible 파일 생성 후

[vagrant@control-node ~]$ ssh ansible@host1 "sudo id"
uid=0(root) gid=0(root) groups=0(root) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
[vagrant@control-node ~]$ ssh ansible@host2 "sudo id"
uid=0(root) gid=0(root) groups=0(root) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
[vagrant@control-node ~]$ ssh ansible@host3 "sudo id"
uid=0(root) gid=0(root) groups=0(root) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
[vagrant@control-node ~]$ ssh ansible@host4 "sudo id"
uid=0(root) gid=0(root) groups=0(root)
728x90

+ Recent posts