Ansible(1)
Ansible(1)
Ansible 소개
개요
Ansible = CODE 로 INFRA 관리(IaC)
- 리눅스 시스템 관리와 자동화
- 코드로 인프라 관리 하기 때문에 인적 오류 완화 가능
- Ansible 특징
- ansible은 python으로 만들어짐
- 오픈소스 자동화 플랫폼
- no agent(SSH 사용)
- playbook을 실행하는 자동화 엔진
- Ansible 장점
- 다양한 플랫폼 지원(리눅스, 유닉스, 윈도우, CISCO, 클라우드사)
- 가동성 높은 자동화(.yml,.js)
- 동적 인벤토리 지원(자동 업데이트)
- 다른 시스템과 쉽게 통합되는 오케스트레이션
- 최종적인 상태가 동일==역등성
- Ansible 개념 및 아키텍쳐
- 제어 노드, 관리 대상 호스트
- 제어 노드 : 앤서블
- 관리 대상 호스트 : 관리 대상이 되는 서버
- 제어 노드, 관리 대상 호스트
- Ansible 사용 사례
- 구성관리
- 애플리케이션 배포
- 프로비져닝
- CI/CD
- 보안 및 규정 준수
- 오케스트레이션
- vargrant + ansible, terraform + ansible 등
Ansible 아키텍처
- 제어노드
- 구성파일(ANSILBE.cfg)
- 인벤토리(INVENTORY)
- 플레이북(PLAYBOOK)
- 관리 대상 호스트
- ssh 필요
- 공개키 인증 방식의 키 배포
- 종류 : linux/unix/ windows, Network, cloud(AWS,Azure,GCP)
엔서블 배포
앤서블 호스트 인벤토리(Ansible Host Inventory)
- Ansible core
- ansible.cfg : 앤서블 설정
- inventory : 관리 대상 호스트 목록
- playbook : 관리 대상 호스트에 실행할 작업(tasks) 정의
- 인벤토리란
- 정적 인벤토리
- 정적 인벤토리 파일(ini 파일)
- 동적 인벤토리(언어 필수)
- 호스트(ex; web1)
- 호스트 그룹(ex; [webservers])
- 중첩된 그룹 정의(ex; [korea:children])
- 범위를 사용한 호스트 이름 간소화(ex; web[1:5].example.com)
- 인벤토리 내용 확인
- vi inventory
ansible [all ungrouped webservers] –list-hosts
- 정적 인벤토리
- Ansible Configuration File
- 읽혀지는 순서
- ansible_config
- 현재 폴더 아래 ansible.cfg / .ansible.cfg
- /etc/ansbile/ansible.cfg
- ansbile.cfg
1 2 3 4 5
vi ansbile.cfg > [defaults] > inventory = ./inventory > [privilege_escalation] > become = True
- ansible.cfg 파일 내용 확인
1 2 3
ansible-config init --disable | egrep -v '^#|^$' | grep '검색단어' vi ansible.cfg ansible-config view
정보 파일 형식
ini yaml XML json Oracle github 블로그 // ajflk;a
- 읽혀지는 순서
- Ansible 다운로드
- ansible-core
- ansible.cfg
- inventory
- playbook
- sudo yum -y install ansible
- ansible-core
어떤 파일의 cfg 파일이 적용된지 아는게 중요함
- 환경설정 (ansible/ansible1/ansible2/ansible3/ansible4)
- (ansible) /etc/hosts 파일 편집
- (ansible) 공개키 인증 방식
- (ansible, ansible#) PS1 변수 설정
- (ansible) SElinux 기능 permissive 설정
- (ansible) 바탕화면 아이콘
- (ansible, ansible#) Power Saving 기능 OFF
- (ansible) gnome-extensions-app 설치
- (ansible, ansible#) ansible 사용자 추가
- (ansible, ansible#) asnible 사용자 sudo 설정
엔서블은 예제가 중요함! 쉘모듈로 확인하기 좋음(리다이렉션 기호가 되어서.) 커맨드는 설정할때! 코드는 외워서 치지 말자
태그 목록
[defaults]
[privilege_escalation]
[persistent_connection]
[connection]
[colors]
[selinux]
[diff]
[galaxy]
[inventory]
[netconf_connection]
[paramiko_connection]
[jinja2]
[tags]
Ad-Hoc Command
- Ad-Hoc 명령어 형식
- ansible HOST -m MODULE -a ‘MODULE Argument’ [-i inventory] [-b]
- 호스트 패턴
- inventory에 저장한 이름
- 모듈 이름
- asible-doc -l > 목록 확인
- 주로 쓰는 모듈
- -m ping
- -m copy
- -m lineinfile
-m yum dnf apt pip gem … - 이외 기타 등등
- 모듈 인자
- 모듈 마다 다름
- ansible-doc 모듈이름 > 모듈 사용법 검색
- ansible-doc 모듈이름 | sed -n ‘/^EXAMPLES:/,$p’ | more
줄 처음이 EXAMPLE: 인 것부터 끝까지 출력
- 호스트 패턴
- ansible HOST -m MODULE -a ‘MODULE Argument’ [-i inventory] [-b]
Ansible PlayBook
[참고] Ad-Hoc CMD == Playbook
- 환경설정
- $HOME/.bashrc
- $HOME/.vimrc(or .exrc)
- IDE - vscode
- Yaml 파일 형식
- 시작 마커(start Marker) == —
- 종료 마커(End Marker) == …
- 플레이북(Playbook)은 키-값의 모음이다.
- 사전(dict)
- 목록(list)
- 주석처리 가능 ```yaml —
예제
name1: example name2: [chan, lee, kim] # 리스트 형식 name3:
- chan2
- lee2
- kim2
key1: {key2: value2, key3: value3} # 딕셔너리 형식
key2: # Key 1과 Key2는 동일하다 key2: value2 key3: value3
key3: [{k5: v5}, {k6: v6}] # 리스트 안에 딕셔너리 형식
key4: # Key3와 Key4은 동일하다
- key7: value7
- key8: value8 ```
- 플레이북 실행
–> ansible-playbook webserver.yml
- 확장자 인식 : .yaml, .yml
- yml 형식
1 2 3 4 5 6 7 8
--- - name: play to setup web server hosts: web1.example.com tasks: - name: lastest httpd version installed yum: name: httpd state: lastest
- play >= play >= task == module
- 플레이북 실행
This post is licensed under CC BY 4.0 by the author.