<장점>
<구성관리 / 배포도구>
구성관리
배포
<마스터 및 에이전트 유무>
마스터
에이전트
애플리케이션 및 IT 인프라를 자동화 할 수 있는 도구
컨트롤 노드
관리 노드
플러그인
인벤토리
모듈
작업(Task)
Ad-hoc 명령
플레이
플레이북
📒 모듈 < task < 플레이 < 플레이북
Ansible 아키텍쳐
<vagrant 설치 및 설정 / 실행>
vagrant plugin install vagrant-hostmanager
vagrant plugin install vagrant-disksize
vagrant plugin list
vagrant box add ubuntu/bionic64
vagrant box list
-> vagrant는 원하는 스펙(ip설정이나, cpu개수 등)의 동일한 가상머신을 아주 편리하게 몇개든 적은 용량으로 설치할 수 있다.<vagrant를 이용한 Ansible VM배포>
vagrant up
VM배포 / 실행<Ansible 서버 설정 및 접속>
ssh vagrant@192.168.56.11
원하는 가상머신 ip입력해서 ssh로 접속 (pw : vagrant입력)ssh-keygen
키 생성ssh-copy-id vagrant@localhost
자신의 ip에 대해 키 복사ssh-copy-id vagrant@ansi-node1
node1에 대해서 키 복사ssh-copy-id vagrant@ansi-node2
node2에 대해서 키 복사ssh-copy-id vagrant@ansi-node3
node3에 대해서 키 복사ssh ansi-node1
각 노드들에 대해서 암호 입력없이 ssh 접속이 된다면 키 기반 접속이 완료된 것이다.sudo apt update
sudo apt install -y software-properties-common
sudo apt-add-repository -y -u ppa:ansible/ansible
sudo apt install -y ansible
sudo apt install -y python3-argcomplete
Ansible 명령 쉘 자동완성sudo activate-global-python-argcomplete3
Ansible 명령 쉘 자동완성sudo apt install -y ansible-lint
Ansible Lint 설치 exec bash
ansible입력 후 tab키
vi .bashrc
master서버에서 bash파일 접속 후 아래 처럼 설정 변경<Ansible 구성 파일>
ansible-config view
구성파일을 직접확인해도 되지만 해당 명령어를 이용해 구성 파일 및 설정에 대해 확인할 수 있다ansible-config dump
현재 적용된 모든 구성 정보 확인ansible-config list
설정 가능한 모든 설정 항목<정적 인벤토리 생성>
Ansible은 인프라에 존재하는 여러 호스트를 관리함
호스트의 목록 또는 그룹을 지정한 인벤토리 파일이 필요하며 인벤토리가 정의되면 패턴을 사용하여 Ansible을 실행할 노드 또는 그룹을 선택함
기본 인벤토리 파일은 /etc/ansible/hosts 이며, -i 옵션을 사용하여 다른 인벤토리 파일을 지정할 수 있음
인벤토리 파일은 일반적으로 INI 파일 형식을 가지고 있으며, YAML 형식으로 지정할 수 있음
정적 인벤토리 : 사용자가 직접 INI 또는 YAML 형식으로 파일을 직접 작성
기본 그룹
여러 그룹에 속한 호스트
cp /etc/ansible/ansible.cfg ./
현재 디렉터리(ansitest)에 원본 ansible.cfg 복사vi ansible.cfg
:%d 수행
vi inventory
ansible-inventory --list
그룹에 대한 정보를 YAML형식으로 보기<패턴>
ansible <pattern> <options>
vi testhost
위에서 만든 기본 인벤토리파일이 아닌 새로운 인벤토리파일 생성ansible \* --list-hosts -i testhost
testhost 인벤토리 파일의 모든 host출력 ansible groupA:groupB --list-hosts -i testhost
testhost 인벤토리의 그룹 A,B에 포함된 호스트 출력💡 위의 기본 인벤터리도 패턴 사용 가능
<모듈>
ansible-doc -l
모듈 목록 확인ansible-doc <module_name>
특정 모듈 확인<Ad-hoc 명령>
ansible ansi-node1 -m ping
ansi-node1에 ping을 보낸다ansible AA -m ping
그룹 AA에 ping을 보낸다ansible ansi-node1 -m reboot, ansible ansi-node1 -a "/sbin/reboot/", ansible ansi-node1 -m shell -a "/sbin/reboot/"
ansi-node1을 재부팅하는 세가지 방법touch testfile
ansible AA -m copy -a "src=/home/vagrant/ansitest/testfile dest=/home/vagrant/"
AA그룹을 대상으로 testfile을 해당 디렉터리에 복사한다ssh ansi-node1 ls /home/vagrant
호스트에 해당 경로를 ls로 보면 testfile이 복사된것을 확인ansible ansi-node1 -m file -a "dest=/home/vagrant/testfile mode=600"
파일의 권한 변경 ansible AA -m file -a "dest=/home/vagrant/testfile state=absent"
이전에 만든 경로의 파일 삭제 ansible ansi-node1 -m user -a "name=suhwan password='1234'"
node1에게 suhwan이라는 유저 생성ansible ansi-node1 -m user -a "name=suhwan state=absent"
suhwan 유저 제거ansible ansi-node3 -m apt -a "name=apache2 update_cahce=yes state=present"
패키지 설치 및 업데이트 ansible ansi-node3 -m service -a "name=apache2 state=started"
서비스 시작 ansible ansi-node3 -m service -a "name=apache2 state=restarted"
서비스 재시작ansible ansi-node3 -m service -a "name=apache2 state=stopped"
서비스 중지ansible ansi-node3 -m apt -a "name=apache2 state=absent"
패키지 제거