Ansible?
Ansible은 오픈 소스 소프트웨어 프로비저닝, 구성 관리, 애플리케이션 전개 도구이다.
수많은 유닉스 계열 시스템에서 실행되며 유닉스 계열 운영 체제 및 마이크로소프트 윈도우의 구성이 가능하다.
시스템 구성을 기술하기 위해 자체 선언형 언어를 포함하고 있다.
위키백과 발췌
장점
멱등성 : 같은 작업을 몇 번을 반복하여도 같은 결과를 보여줌
No Agent, No Master, No Slave, Only SSH
주의사항
Ansible 의 경우 문법이 어렵지는 않으나, 줄 바꿈에 매우 민감합니다.
줄 바꿈에서 미스가 날 경우 스크립트가 구동되지 않는 문제가 있으니, VS Code 같은 IDE 툴을 사용하여 작성하시길 권하여 드립니다.
설치 방법
ansible 은 python 기반이기 때문에 python이 설치되어 있는 PC라면 어디서든 설치할 수 있습니다.
맥용
brew install ansible
pip3 버전 설치 기준 (루트 사용자로 설치 권장)
pip3 install ansible
호스트(리모트 서버) 추가하기
ansible 설치 이후 host.ini 파일을 생성 후 아래 형식대로 배포할 서버를 추가합니다.
[ubuntu:vars] #variable 선언
ansible_ssh_private_key_file=/Users/tei.chae/Downloads/godo.pem #로그인 시도 시 pem 키 사용하도록 경로 지정
[ubuntu] #그룹 선언
ubuntu-01 ansible_host=52.79.199.8 ansible_user=ubuntu #서버명 호스트IP 사용할 유저명
설정을 완료했다면, 다음 명령어를 입력하여 접속이 되는지 확인합니다.
ansible -m ping -i host.ini ubuntu-01
결과값
ubuntu-01 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
playbook을 이용한 apache 설치하기
우분투용
- name : ubuntu 초기 세팅
hosts : ubuntu-01 # 그룹 지정, 그룹이 아닌 호스트 지정도 가능
become : true # 루트 권한 사용 여부
gather_facts: false # 호스트 리소스 정보 수집하지 않음
- name : install httpd
apt :
name : httpd #설치할 패키지 이름 지정
state : latest #최신 버전을 검색하여 설치
- name : httpd service start
service :
name : httpd #시작할 서비스 이름 지정
state : started #상태에 대해선 과거형으로 사용
- name : httpd service stop
service :
name : httpd #정지할 서비스 이름 지정
state : stopped #상태에 대해서는 과거형으로 사용
CentOS용
- name : centos 초기 세팅
hosts : centos # 그룹 지정, 그룹이 아닌 호스트 지정도 가능
become : true # 루트 권한 사용 여부
gather_facts: false # 호스트 리소스 정보 수집하지 않음
tasks :
- name : install httpd
yum : name=httpd state=latest
- name : httpd service start
service : name=httpd state=started
- name : httpd service stop
service : name=httpd state=stopped
문법 방식이 두 가지이지만, OS에 따른 차이는 아니고 양쪽 모두 두 문법을 사용할 수 있습니다.
CentOS용 Apache, php 56 설치, ioncube 모듈 설치 예제
- name : centos 초기 세팅
hosts : centos-01 # 그룹 지정, 그룹이 아니라 호스트만 지정하고 싶으면 사전에 정의한 centos-01 사용
become : true # 루트 권한 사용 여부
gather_facts: false # 호스트 리소스 정보 수집하지 않음
tasks :
- name : timezone 설정
timezone : name=Asia/Seoul
- name : epel-release 설치
yum : name=epel-release state=latest
- name : yum update
yum : name=* state=latest
- name : httpd install
yum : name=httpd state=latest
- name : httpd enable
systemd : name=httpd enabled=yes
- name : selinux 설정 해제
lineinfile :
dest : /etc/sysconfig/selinux #대상 파일 지정
backrefs : yes #텍스트 변경하기
regexp : 'enforcing' #찾을 문자
line : 'disabled' #변경할 내용
- name : download remi repo rpm
get_url : url=http://rpms.famillecollet.com/enterprise/remi-release-7.rpm dest=/home/centos/
- name : install remi repo rpm
yum : name=/home/centos/remi-release-7.rpm state=present
- name : install php
yum : enablerepo=remi,remi-php56 name=php,php-mssql,추가로 설치할 모듈이름 추가해도 됨 state=latest
- name : php.ini에 timezone 수정
ini_file: dest=/etc/php.ini section=Date option=date.timezone value=Asia/Seoul
- name : php.ini에 short_open_tag 수정하기
ini_file: dest=/etc/php.ini section=PHP option=short_open_tag value=On
- name : php info 파일 만들기
file : path=/var/www/html/info.php state=touch
- name : php info 파일에 설정 추가하기
lineinfile :
path : /var/www/html/info.php
line : "<?php phpinfo(); ?>"
- name : ioncube 모듈 다운로드
get_url : url=https://downloads.ioncube.com/loader_downloads/ioncube_loaders_lin_x86-64.tar.gz dest=/home/centos/
- name : ioncube 압축 파일 풀기
unarchive : src=/home/centos/ioncube_loaders_lin_x86-64.tar.gz dest=/home/centos remote_src=yes
- name : php module 디렉토리에 ioncube 모듈 복사
copy : src=/home/centos/ioncube/ioncube_loader_lin_5.6.so dest=/usr/lib64/php/modules/ mode='0755' remote_src=yes
- name : php ini에 ioncube 모듈 위치 지정
lineinfile :
path : /etc/php.ini
line : "{{item}}"
with_items :
- '[Zend]'
- 'zend_extension="/usr/lib64/php/modules/ioncube_loader_lin_5.6.so"'
- name : httpd 서비스 시작
service : name=httpd state=started