Post

Ansible(1)

Ansible(1)

Ansible 소개

개요

Ansible = CODE 로 INFRA 관리(IaC)

  • 리눅스 시스템 관리와 자동화
    • 코드로 인프라 관리 하기 때문에 인적 오류 완화 가능
  • Ansible 특징
    • ansible은 python으로 만들어짐
    • 오픈소스 자동화 플랫폼
    • no agent(SSH 사용)
    • playbook을 실행하는 자동화 엔진
  • Ansible 장점
    • 다양한 플랫폼 지원(리눅스, 유닉스, 윈도우, CISCO, 클라우드사)
    • 가동성 높은 자동화(.yml,.js)
    • 동적 인벤토리 지원(자동 업데이트)
    • 다른 시스템과 쉽게 통합되는 오케스트레이션
    • 최종적인 상태가 동일==역등성
  • Ansible 개념 및 아키텍쳐
    • 제어 노드, 관리 대상 호스트
      • 제어 노드 : 앤서블
      • 관리 대상 호스트 : 관리 대상이 되는 서버
  • Ansible 사용 사례
    • 구성관리
    • 애플리케이션 배포
    • 프로비져닝
    • CI/CD
    • 보안 및 규정 준수
    • 오케스트레이션
      1. vargrant + ansible, terraform + ansible 등

Ansible 아키텍처

  • 제어노드
    1. 구성파일(ANSILBE.cfg)
    2. 인벤토리(INVENTORY)
    3. 플레이북(PLAYBOOK)
  • 관리 대상 호스트
    1. ssh 필요
    2. 공개키 인증 방식의 키 배포
    3. 종류 : linux/unix/ windows, Network, cloud(AWS,Azure,GCP)

엔서블 배포

앤서블 호스트 인벤토리(Ansible Host Inventory)

  • Ansible core
    • ansible.cfg : 앤서블 설정
    • inventory : 관리 대상 호스트 목록
    • playbook : 관리 대상 호스트에 실행할 작업(tasks) 정의
  • 인벤토리란
    1. 정적 인벤토리
      • 정적 인벤토리 파일(ini 파일)
    2. 동적 인벤토리(언어 필수)
      • 호스트(ex; web1)
      • 호스트 그룹(ex; [webservers])
      • 중첩된 그룹 정의(ex; [korea:children])
      • 범위를 사용한 호스트 이름 간소화(ex; web[1:5].example.com)
    3. 인벤토리 내용 확인
      • vi inventory
      • ansible [allungroupedwebservers] –list-hosts
  • Ansible Configuration File
    1. 읽혀지는 순서
      1. ansible_config
      2. 현재 폴더 아래 ansible.cfg / .ansible.cfg
      3. /etc/ansbile/ansible.cfg
    2. ansbile.cfg
      1
      2
      3
      4
      5
      
       vi ansbile.cfg
       > [defaults]
       > inventory = ./inventory
       > [privilege_escalation]
       > become = True
      
    3. ansible.cfg 파일 내용 확인
      1
      2
      3
      
       ansible-config init --disable | egrep -v '^#|^$' | grep '검색단어'
       vi ansible.cfg
       ansible-config view
      
    • 정보 파일 형식

      iniyamlXMLjson
      Oraclegithub 블로그//ajflk;a
  • Ansible 다운로드
    • ansible-core
      1. ansible.cfg
      2. inventory
      3. playbook
    • sudo yum -y install ansible

어떤 파일의 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

  1. Ad-Hoc 명령어 형식
    • ansible HOST -m MODULE -a ‘MODULE Argument’ [-i inventory] [-b]
      • 호스트 패턴
        • inventory에 저장한 이름
      • 모듈 이름
        • asible-doc -l > 목록 확인
        • 주로 쓰는 모듈
          • -m ping
          • -m copy
          • -m lineinfile
          • -m yumdnfaptpipgem
          • 이외 기타 등등
      • 모듈 인자
        • 모듈 마다 다름
        • ansible-doc 모듈이름 > 모듈 사용법 검색
        • ansible-doc 모듈이름 | sed -n ‘/^EXAMPLES:/,$p’ | more

          줄 처음이 EXAMPLE: 인 것부터 끝까지 출력

Ansible PlayBook

[참고] Ad-Hoc CMD == Playbook

  1. 환경설정
    • $HOME/.bashrc
    • $HOME/.vimrc(or .exrc)
    • IDE - vscode
  2. 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 ```
    1. 플레이북 실행
      –> 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.