ansible의 플레이북(playbook)

  • 플레이북(playbook)의 원래 뜻은 각본, 작전, 계획
  • ansible에서 플레이북(playbook)은 대량의 서버에 설치 및 실행할 때의 순서를 정의
  • ansible 플레이북을 이용하여 대량의 서버에 nginx 설치하고 실행하는 순서
    1. nginx 설치
    2. 파일 전송
    3. nginx 재시작

1. ansible에서 멱등성

  • 연산을 여러 번 적용하다라도 결과가 달라지지 않는 성질

  • 멱등성이 없는 경우(단순 명령어)

    # 아래 명령어를 계속 반복하면 /etc/ansible/hosts 아래에 같은 구문이 계속 생김
    # 아래 명령어 실행할 때 마다 추가됨
    echo -e "[playbook]\\n192.168.1.13" >> /etc/ansible/hosts
  • 멱등성이 있는 경우 (playbook이용)

    $ vi playbook.yml
    ---
    - name: Ansible_vim
      host: localhost
    
      tasks:
        - name: Add ansible hosts
          blockinfile:
            path: /etc/ansible/hosts
            block: |
              [playbook]
              192.168.1.13

2. ansible에서 YAML

  • YAML은 마크업 언어가 아님을 표방해서 나왔지만 마크업 언어로 사용됨

  • 사용자가 쉽게 작성하고 이해할 수 있도록 작성

  • XML을 넘어 JSON과 유사 혹은 거의 동일

  • YAML 파일에 대한 설명

    1. name : 실행하는 playbook의 이름

    2. host : 실행되는 장소

    3. tasks : 실행할 작업들

    4. name : 작업에 대한 이름

    5. blockinfile : 모듈의 이름으로 특정 블록을 파일에 기록하는 역할을 함

    6. path : 저장되는 파일의 이름

    7. block : block을 기록하는 시작 포인터를 | 으로 사용

    8. 끝은 지정하지 않았으면 내용은 [playbook]\n192.168.1.13으로 기록

      $ vi playbook.yml
      ---
      - name: Ansible_vim
      host: localhost
      
      tasks:
        - name: Add ansible hosts
          blockinfile:
            path: /etc/ansible/hosts
            block: |
              [playbook]
              192.168.1.13



플레이북(playbook)을 이용하여 nginx 설치 및 실행

  • ansible 서버에 플레이북을 정의하여 ansible 클라이언트에 nginx 설치 및 실행을 시킴

1. nginx 설치하여 실행할 YAML 파일 생성

$ vi install_nginx.yml
---
- hosts: all
  remote_user: root
  tasks:
    - name: install epel-release
      yum: name=epel-release state=latest
    - name: install nginx web server
      yum: name=nginx state=present
    - name: Upload basic index.html for web server
      copy: src=index.html dest=/usr/share/nginx/html/ mode=0644
    - name: start nginx web server
      service: name=nginx state=started

2. ansible 서버에 nginx 기본 시작 페이지를 다운받음

  • 각 ansible 클라이언트의 기본 시작 페이지를 지정

    $ curl -o index.html <https://www.nginx.com>
      % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                     Dload  Upload   Total   Spent    Left  Speed
    100 97615    0 97615    0     0  31715      0 --:--:--  0:00:03 --:--:-- 31724
    
    $ ls
    index.html

3. 플레이북 실행

$ ansible-playbook install_nginx.yml -k
SSH password:

PLAY [all] *************************************************************************

TASK [Gathering Facts] **************************************************************
ok: [192.168.1.13]

TASK [install epel-release] *************************************************************
ok: [192.168.1.13]

TASK [install nginx web server] **********************************************************
changed: [192.168.1.13]

TASK [start nginx web server] ********************************************************
changed: [192.168.1.13]

PLAY RECAP ************************************************************************
192.168.1.13             : ok=5    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

4. ansible 클라이언트인 192.168.1.13에서 실행 결과 확인


+ Recent posts