C:\Users\johnlee>cd c:\HashiCorp c:\HashiCorp>vagrant init https://app.vagrantup.com/boxes/search?provider=virtualbox => vagrant에서 contos 사용을 위해 하시코프사에서 만들어 놓은 이미지(vargrant에서는 이미지를 Box라고 함) c:\HashiCorp>notepad Vagrantfile config.vm.box = "centos/7" => centos7을 사용하기 위함 config.vm.network "public_network" c:\HashiCorp>vagrant up c:\HashiCorp>vagrant ssh c:\HashiCorp>vagrant destroy
--- 어느 경로에 있든 vagrant 명령어를 사용할 수 있게 환경변수 설정하기
%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\ 추가vagrant notepad 파일cmd에서 ssh 접속
첫 번째 랜카드 : NAT, 두 번째 랜카드 : public_network 활성화 시켜서 추가한 랜카드
-- 플레이북
present(=install, add), absent(=remove, delete)
-- 모듈
- 플레이북에서 task가 어떻게 수행될지를 정의하는 요소
- 타깃 서버들 안에 있는 명령어들을 구동하기 위해 yum 모듈이라는 개념 사용
-- 앤서블 서버 설치
# yum install epel-release -y => 저장소에 대한 정보를 업데이트하는 명령어 => epel(Extra Packages for Enterprise Linux) => 공식적인 리눅스 패키지 외에 추가적인 패키지들이 모여 있는 저장소 # yum --enablerepo=epel -y install ansible => 저장소를 enable 해주고 epel 저장소에 있는 ansible을 설치 # ansible --version 앤서블 문서 사이트 https://docs.ansible.com/ansible/latest/index.html 앤서블 모듈 사이트 https://docs.ansible.com/ansible/2.9/modules/list_of_all_modules.html
-- 앤서블의 애드혹 (AD-HOC(라틴어): 특별한 목적을 위해)
# vi /etc/ansible/hosts => 앤서블 인벤토리
[centos] 192.168.0.241 192.168.0.245
[ubuntu] 192.168.0.211 192.168.0.251 vi /etc/ansible/hosts : 맨 마지막 줄에 centos와 ubuntu 정보 추가 # ansible all -m ping => all : hosts 파일 안의 모든 IP로 ping => icmp 프로토콜을 통한 ping이 아니라, python이 잘 실행되고 있는 여부를 판단하는 ping => ping이라는 명령어 안에는 ssh도구도 포함되어 있음
known host에 등록되지 않았다는 오류와 등록하는 과정 known host에 ecdsa라는 키와 함께 등록된 것 확인
키가 없는 상태에서 password를 기입하지 않았다는 애러, 패스워드를 찍으려면 옵션이 필요함 # ansible all -m ping -k => k : ask password 라는 의미로 패스워드를 입력하기 위한 옵션
패스워드를 입력하여 root계정으로 접근 가능, ping이 나간 모습 : 파이썬의 상태(버전, 손상유무 등)를 보는 ping
# ansible centos -m ping -k => centos 그룹 안의 서버로만 ping # ansible ubuntu -m ping -k # echo "192.168.0.140" >> inventory.list # echo "192.168.0.143" >> inventory.list => 임시로 inventory 파일을 만들어서 임의의 서버만 관리해보고자 할 때 # ansible all -i inventory.list -m ping -k # ansible 192.168.0.140 -i inventory.list -m ping -k
inventory에 정의되어 있는 서버 두 대와만 ping 통신
특정 IP를 이용하는 것도 가능 # ansible all --list-hosts default 인벤토리 내에 있는 서버들의 리스트를 보여주는 것
직접 만든 inventory.list의 host도 확인 가능 # ansible all -m shell -a "uptime" -k => shell 모듈 : df -h(디스크 공간 확인), free -h(메모리 확인), uptime(구동된 시간)과 같이 흔히 사용하는 명령어를 그대로 사용할 수 있게 해줌 # ansible all -m shell -a "df -h" -k # ansible all -m shell -a "free -h" -k # ansible all -m user -a "name=kosa" -k => account를 만드는 명령어 # ansible all -m shell -a "tail -n 1 /etc/passwd" -k tail : kosa 사용자가 추가되었는지 확인하는 명령어, 1 : passwd파일의 맨 마지막에서 첫 번째 줄 # ansible all -m user -a "name=kosa state=absent" -k kosa 사용자를 삭제하기 위한 명령어
# ansible all -m shell -a "tail -n 1 /etc/passwd" -k kosa 사용자가 삭제된 것을 확인
--- 센토스 아파치 애드혹 관리
# ansible centos -m yum -a "name=httpd state=present" -k => 여기서 name은 사용자가 아닌 패키지 네임 httpd를 의미 # curl https://www.nginx.com/ -o index.html -o : output으로 nginx의 index.html 을 추출하는 것
# ansible centos -m copy -a "src=index.html dest=/var/www/html/index.html" -k => 현재 위치 index.html 파일을 원하는 경로로 복사하는 명령어 # ansible centos -m service -a "name=httpd state=started" -k => yum으로 설치할 때의 name은 패키지 이름, 여기서 name은 서비스의 이름 => systemctl enable --now httpd 와 같은 표현 # ansible centos -m shell -a "systemctl status firewalld" -k # ansible centos -m shell -a "systemctl start firewalld" -k 이제 방화벽이 시작되어 접속 안 될 것, 80포트를 오픈해줘야 함 # ansible centos -m shell -a "firewall-cmd --permanent --zone=public --add-service=http" -k # ansible centos -m shell -a "firewall-cmd --reload" -k 방화벽 서비스에 http를 추가 # ansible centos -m service -a "name=httpd state=stopped" -k # ansible centos -m shell -a "systemctl stop firewalld" -k # ansible centos -m yum -a "name=httpd state=absent" -k
** 쉘 스크립트는 한 번 실행한 명령어를 다시 실행했을 때, 건너뛰지 않고 다시 실행됨
** 노란색 글자로 나오면 change가 생겼다는 의미고, 녹색 글자는 그냥 넘어간다는 의미,
** 쉘 스크립트는 멱등성이 없기 때문에 또 수행되어 노란색 글자로 나옴
** 멱등성이 없으면 중복 작업을 건너 뛰지 못해 서버의 퍼포먼스가 떨어짐
--- 우분투 아파치 애드혹 관리
# ansible ubuntu -m apt -a "name=apache2 state=present" -k # curl https://www.nginx.com/ -o index.html # ansible ubuntu -m copy -a "src=index.html dest=/var/www/html/index.html" -k # ansible ubuntu -m service -a "name=apache2 state=stopped" -k # ansible ubuntu -m service -a "name=apache2 state=started" -k # ansible ubuntu -m apt -a "name=apache2 state=absent" -k
--- 멱등성 - 앤서블은 멱등성(Idempotency)이란 특징을 가짐 - 이는 여러 번 적용해도 결과가 변경되지 않음 - 수정된 부분이 있다면 그 부분만 새롭게 반영
# echo "172.16.0.100 " >> inventory.list # cat inventory.list # echo "172.16.0.100" >> inventory.list # cat inventory.list => >는 덮어쓰기지만, >>는 중복된 내용이라도 삽입하므로 멱등성이 없다고 볼 수 있음 # ansible localhost -c local -m lineinfile -a "path=inventory.list line=172.16.0.200" => localhost는 ansible 자체의 서버를 의미 => -c local : connection local : 연결방식을 local로 하여 ssh를 이용하지 않고, 로컬 안에서 앤서블 서버 자체에 대한 작업 => lineinfile : file 안에 line을 넣겠다는 의미 # cat inventory.list # ansible localhost -c local -m lineinfile -a "path=inventory.list line=172.16.0.200" # cat inventory.list => 172.16.0.200은 중복되어 삽입되지 않으므로 멱등성이 있다고 할 수 있음
--- ssh key 활용
# ssh-keygen -t rsa => 키를 생성한 후 # ssh-copy-id root@192.168.0.140 => ssh로 키를 이용해 ansible all --list해서 나오는 앤서블 서버가 관리하는 모든 서버에 키를 복사해주면 됨 이제 -k 옵션으로 패스워드를 넣지 않아도, 키를 통해 바로 접근 가능함
--- 플레이북 구조
YAML 형식으로 작성된 각각의 Playbook들은 하나 이상의 Play를 가지며, 각각의 Play는 하나 이상의 task(앤서블 모듈)을 실행한다
- name: Playbook Tutorial # - 로 시작하는 3줄이 하나의 블록이다. hosts: all # ":" 으로 구분해 항목명과 값을 설정한 키-값 형식으로 되어 있다. tasks: # 항목명의 위치가 정렬되어 있다. yaml은 들여쓰기가 데이터 구조의 깊이가 된다.
--- 센토스, 우분투 아파치 설치 플레이북
# vi apache_install.yml - name: Install apache on centos hosts: centos => 인벤토리 내에 있는 인벤토리 그룹의 이름을 넣어줌 gather_facts: no => facts라는 변수, magic이라는 변수를 앤서블이 미리 만들어 놓음, tasks에서 사용할 것이 아니기에 facts 변수 수집X => facts 변수를 수집하는 데 레이턴시가 있기 때문에, facts를 사용하지 않으면 그만큼 작업시간 절감됨
tasks: - name: install apache web server => 항목 별로 작업 내용을 적어 놓으면, playbook을 실행할 때 작업내역이 표시되기에 다른 사람의 이해에 도움됨 yum: name=httpd state=present - name: upload default index.html for web server get_url: url=https://www.nginx.com dest=/var/www/html/ mode=0644 - name: start apache web server service: name=httpd state=started enabled=yes
- name: Install apache on ubuntu hosts: ubuntu gather_facts: no
tasks: - name: install apache web server apt: name=apache2 state=present - name: upload default index.html for web server get_url: url=https://www.nginx.com dest=/var/www/html/ mode=0644 => mode : 0644로 퍼미션을 변경 - name: start apache web server service: name=apache2 state=started
# ansible-playbook apache_install.yml
--- 센토스, 우분투 아파치 삭제 플레이북
# vi apache_remove.yml - name: Remove apache on centos hosts: centos gather_facts: no
tasks: - name: remove apache web server yum: name=httpd state=absent
- name: Remove apache on ubuntu hosts: ubuntu gather_facts: no
tasks: - name: remove apache web server apt: name=apache2 state=absent