[Ansible] Ansible 설치, 설정, 구성
본문 바로가기
IT 이야기/Ansible

[Ansible] Ansible 설치, 설정, 구성

by 찬찬이 아빠 2020. 6. 5.
반응형
  1. 마스터 서버(배포 서버) 설치 및 설정

 

(1) python3 설치하기

아래는 python3 설치를 위해 필요한 설치 패키지 리스트 입니다.

대부분 RHEL7.7 버전 이상의 ISO 이미지안에 있는 Packages 디렉터리에 해당 패키지 파일들이 존재합니다.

audit-2.8.5-4.el7.x86_64.rpm
perl-Text-ParseWords-3.29-4.el7.noarch.rpm
audit-libs-2.8.5-4.el7.x86_64.rpm
perl-threads-1.87-4.el7.x86_64.rpm
audit-libs-devel-2.8.5-4.el7.x86_64.rpm
perl-threads-shared-1.43-6.el7.x86_64.rpm
audit-libs-python-2.8.5-4.el7.x86_64.rpm
perl-Time-HiRes-1.9725-3.el7.x86_64.rpm
dwz-0.11-3.el7.x86_64.rpm
perl-Time-Local-1.2300-2.el7.noarch.rpm
perl-5.16.3-294.el7_6.x86_64.rpm
policycoreutils-2.5-33.el7.x86_64.rpm
perl-Carp-1.26-244.el7.noarch.rpm
policycoreutils-devel-2.5-33.el7.x86_64.rpm
perl-constant-1.27-2.el7.noarch.rpm
policycoreutils-gui-2.5-33.el7.x86_64.rpm
perl-Encode-2.51-7.el7.x86_64.rpm
policycoreutils-newrole-2.5-33.el7.x86_64.rpm
perl-Exporter-5.68-3.el7.noarch.rpm
policycoreutils-python-2.5-33.el7.x86_64.rpm
perl-File-Path-2.09-2.el7.noarch.rpm
policycoreutils-sandbox-2.5-33.el7.x86_64.rpm
perl-File-Temp-0.23.01-3.el7.noarch.rpm
python-2.7.5-86.el7.x86_64.rpm
perl-Getopt-Long-2.40-3.el7.noarch.rpm
python2-rpm-macros-3-32.el7.noarch.rpm
perl-HTTP-Tiny-0.033-3.el7.noarch.rpm
python3-3.6.8-10.el7.x86_64.rpm
perl-libs-5.16.3-294.el7_6.x86_64.rpm
python3-devel-3.6.8-10.el7.x86_64.rpm
perl-macros-5.16.3-294.el7_6.x86_64.rpm
python3-libs-3.6.8-10.el7.x86_64.rpm
perl-parent-0.225-244.el7.noarch.rpm
python3-pip-9.0.3-5.el7.noarch.rpm
perl-PathTools-3.40-5.el7.x86_64.rpm
python3-rpm-generators-6-2.el7.noarch.rpm
perl-Pod-Escapes-1.04-294.el7_6.noarch.rpm
python3-rpm-macros-3-32.el7.noarch.rpm
perl-podlators-2.5.1-3.el7.noarch.rpm
python3-setuptools-39.2.0-10.el7.noarch.rpm
perl-Pod-Perldoc-3.20-4.el7.noarch.rpm
python-devel-2.7.5-86.el7.x86_64.rpm
perl-Pod-Simple-3.28-4.el7.noarch.rpm
python-libs-2.7.5-86.el7.x86_64.rpm
perl-Pod-Usage-1.63-3.el7.noarch.rpm
python-rpm-macros-3-32.el7.noarch.rpm
perl-Scalar-List-Utils-1.27-248.el7.x86_64.rpm
python-srpm-macros-3-32.el7.noarch.rpm
perl-Socket-2.010-4.el7.x86_64.rpm
redhat-rpm-config-9.1.0-88.el7.noarch.rpm
perl-srpm-macros-1-8.el7.noarch.rpm
perl-Storable-2.45-3.el7.x86_64.rpm
zip-3.0-11.el7.x86_64.rpm

 

설치 하는 명령어는 아래와 같습니다.

# yum localinstall -y *

 

 

(2) ansible 설치하기

ansible을 설치하기 위해 필요한 파일들은 아래와 같습니다.

설치에 필요한 파일들은 구글링을 통해 모두 다운로드 할 수 있습니다.

ansible.cfg
ansible-2.9.6.tar.gz
MarkupSafe-1.1.1-cp36-cp36m-manylinux1_x86_64.whl
pycparser-2.20-py2.py3-none-any.whl
cffi-1.14.0-cp36-cp36m-manylinux1_x86_64.whl
PyYAML-5.3.1.tar.gz
cryptography-2.8-cp34-abi3-manylinux1_x86_64.whl
six-1.15.0-py2.py3-none-any.whl
Jinja2-2.11.2-py2.py3-none-any.whl

 

ansible을 설치하는 명령어는 아래와 같습니다.

# pip3 install --no-index --find-links=. Ansible

 

ansible 실행 명령어의 소프트 링크 파일을 생성합니다.

# ln -s /usr/local/bin/ansible /usr/bin/ansible

 

 

(3) ansible 작업 디렉터리 생성 및 환경설정 파일 복사

# mkdir /etc/ansible
# cp ansible.cfg /etc/ansible/

 

(4) ansible 기본 환경 설정

  • ansible.cfg 파일로 기본적인 환경 설정이 가능
  • 기본 설정은 [defaults]에서 설정하고, 이 외에 inventory, ssh_connection, color 등을 설정 가능
# vim /etc/ansible/ansible.cfg
[default]
inventory		= /etc/ansible/hosts/all		← inventory 설정(기본 인벤토리 파일 설정)
forks			= 30					← 병렬 처리 시 프로세스 개수(기본값 30)
remote_user = ansible						← 원격 접속 사용자명(일반계정)
remote_port = 12400						← 원격 접속 포트 번호(사용자가 임의로 설정)​

 

 

  2. 호스트 서버 설정

(1) 호스트 서버들은 일반 계정과 비밀번호 생성

# useradd ansible
# passwd ansible

 

(2) 호스트 서버 접근 허용 설정

 - IP 대역대가 다른 경우 마스터 서버(배포 서버)에서 접근이 가능하도록 설정 필요

 - 호슽 서버의 /etc/hosts.allow에 마스터 서버 IP 주소 대역대를 설정

# vim /etc/hosts.allow
sshd:	10.xx.xxx.뒷자리 생략

 

(3) SSH 보안키 생성 및 전송

  •  마스터 서버에서 SSH 보안키 생성

  - 호스트 서버에 안전하게 접속하기 위한 보안키 생성

# ssh-keygen -t rsa

  

  - 생성된 SSH Public Key를 호스트 서버로 전송

# ssh-copy-id ansible@호스트명 또는 IP주소
ex) ssh-copy-id ansible@test-web1

 

(4) 호스트 서버 ssh 포트 번호 변경

  • ansible은 ssh 기반으로 동작하기 때문에 ssh 환경 설정에 12400 추가
  • vim /etc/ssh/sshd_config 수행 후 Port 22와 Port 12400 추가
# vinm /etc/ssh_sshd_config
Port 22
Port 12400
  • ssh 데몬 재시작
<RHEL6>
# service sshd restart

<RHEL7>
# systemctl restart sshd

 

 

  3. Inventory 구성

 

  • 마스터 서버가 제어하게 되는 호스트들에 대해서 그룹과 호스트를 정의
  • /etc/ansible/hosts 디렉터리에 지정 파일에 저장되며 서버 그룹과 호스트(IP로 설정 가능)로 구성
  • host는 /etc/hosts에 사전에 등록되어 있어야 이름을 통해 해당 IP로 찾아갈 수 있음
설정 예1)
# vim /etc/ansible/hosts/web
[Web]
10.xxx.xxx.10			← IP 주소로 등록
10.xxx.xxx.11


설정 예2)
# vim /etc/ansible/hosts/was
[was]
test-was1		 ← hostsname 등록
test-was2


설정 예3)
# vim /etc/ansible/hosts/all
[os]     		← 서버 그룹
test-web1
test-was1
test-db1

[Web]
test-web1
test-web2

[WAS]
test-was1
test-was2

[DBMS]
test-db1
test-db2

 

  • 호스트와 그룹
<일반적인 형태>
mail.example.com

[web]
abc.example.com
def.example.com

[was]
one.chanchan.com
two.chanchan.com
three.chanchan.com


<별칭 지정 및 터널을 통해 연결 하고자 할 경우>
super ansible_port = 10001 ansible_host = 10.xxx.xxx.100


<다른 형태>
[web]
www[01:50].chanchan.com

[was]
was-[a:f].example.com

[db]
localhost			ansible_connection=local
db1.chanchan.com	ansible_connection=ssh		ansible_user=chan
db2.example.com		ansible_connection=ssh		ansible_user=dba

* 파라미터 종류
http://docs.ansible.com/ansible/latest/intro_inventory.html#list-of-behavioral-inventory-parameters

 

  • 호스트에 대한 변수 지정
[chan_set]
chan1 http_port=80 maxRequestsPerchild=800
chan2 http_port=8080 maxRequestsPerchild=1024

 

  • 그룹의 그룹, 그룹에 대한 변수 지정
이해를 돕기 위해 우리나라 지역 한글명으로 설명

[충청북도]
청주
충주
제천

[충청남도]
천안
아산

[충청도:children[
충청북도
충청남도

[충청도:vars]
some_server=chan.example.com
halon_system_timeout=30
self_destruct_countdown=60
escape_pods=2


[대한민국:children]
경기도
강원도
경상도
전라도


<등록한 호스트 실행 예제>
# ansible all -i hosts/korea -l "대한민국"
# ansible all -i hosts/korea -l "경기도, 강원도"
# ansible all -i hosts/korea -l "청주 충주"
반응형

댓글