ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Ansible 설치 & 기본사용하기
    기타 2014. 8. 29. 09:00
    반응형

    Ansible 이란?
    시스템 환경 설정 및 애플리케이션 배포 자동화 플랫폼.
    에이전트가 없는 구조. 에이전트 관리에 신경을 쓰지 않아도 됨.
    SSH를 통해서 통신 함.


    설치
    환경 : OS X 10.9

     
    brew update
    brew install ansible
     
    



    환경 : Ubuntu

    sudo apt-get install software-properties-common
    sudo apt-add-repository ppa:ansible/ansible
    sudo apt-get update
    sudo apt-get install ansible
    


    기본개념

    Playbook
    Ansible에서 사용하는 설정, 배포 언어.
    YAML형식으로 되어 있음.
    샘플 Playbook들을 참고 할 수 있는 곳 
    https://github.com/ansible/ansible-examples

    Playbook 기본구조
    어떤 호스트에 어떤 사용자명으로 무슨 작업을 할지를 명시하고 있음.

    Playbook 실행하기

    ansible-playbook playbook.yml
    

    Playbook 기본 디렉토리 구조
    production                # inventory file for production servers
    stage                      # inventory file for stage environment
    
    group_vars/
       group1                  # here we assign variables to particular groups
       group2                 # ""
    host_vars/
       hostname1           # if systems need specific variables, put them here
       hostname2          # ""
    
    site.yml                   # master playbook
    webservers.yml       # playbook for webserver tier
    dbservers.yml          # playbook for dbserver tier
    
    roles/
        common/             # this hierarchy represents a "role"
            tasks/              #
                main.yml      #  <-- tasks file can include smaller files if warranted
            handlers/          #
                main.yml      #  <-- handlers file
            templates/        #  <-- files for use with the template resource
                ntp.conf.j2    #  <------- templates end in .j2
            files/                #
                bar.txt          #  <-- files for use with the copy resource
                foo.sh          #  <-- script files for use with the script resource
            vars/               #
                main.yml     #  <-- variables associated with this role
            meta/             #
                main.yml     #  <-- role dependencies
    
        webtier/              # same kind of structure as "common" was above, done for the webtier role
        monitoring/          # ""
        fooapp/               # ""
    



    Module
    원격 호스트에서 직접 실행됨.
    사용자 정의도 가능함.
    시스템 자원, 서비스, 패키지, 파일등을 설정할 수 있음.
    ansible에서 사용가능한 모듈 목록 : http://docs.ansible.com/modules_by_category.html


    Ansible을 이용해서 원격 장비에 환경설정해보기

    기본 환경설정
    SSH 설정
    Ansible을 이용해서 환경설정을 하려는 클라이언트쪽에 Ansible을 이용해서 ssh 접속을 할수 있도록 환경 설정이 필요함.

    Ansible을 실행하는 서버에서 ssh-keygen 명령으로 클라이언트에 추가할 key파일을 생성함
    ssh-keygen

    생성된 key파일 내용 확인
    cat ~/.ssh/id_rsa.pub


    클라이언트에 Ansible을 실행하는 서버를 신뢰할 수 있는 서버로 추가함.
    ~/.ssh/know_hosts 파일에 Ansible 서버에서 생성한 키파일에 대한 정보를 추가해 줌.

    ansible은 인벤토리라고해서 작업을 수행할 서버들에 대한 정보를 저장하고 있는 hosts 파일을 사용함.
    기본 경로는 /usr/local/etc/ansible/hosts 인데 -i 옵션을 이용해서 명시해 줘도 됨.

    Ansible용 hosts 파일 생성
    ansible-hosts 파일 생성

    [vagrant]
    192.168.33.100



    서버의 파일을 클라이언트로 복사
    filecopy.yml 파일 생성

    ---
    - hosts: vagrant
      remote_user: vagrant
      tasks:
      - name: Copy file
        copy: src=./test.txt  dest=/vagrant/ansible.txt owner=root group=root mode=0644
    

    playbook 실행.
    ansible-playbook -i ansible-hosts  filecopy.yml


    클라이언트에서 nginx 서비스 실행
    service.yml 파일 생성

    ---
    - hosts: vagrant
      remote_user: vagrant
      tasks:
      - name: Service Start
        service: name=nginx state=started
        sudo: yes
    

    playbook 실행 
    ansible-playbook -i ansible-hosts service.yml

    위의 yml파일에 있는 name:, service:등이 ansible에서 사용하는 모듈이며 모듈의 목록은 http://docs.ansible.com/list_of_all_modules.html 에서 확인 할 수 있음.


    참고 
    http://docs.ansible.com/intro_installation.html#getting-ansible
    http://docs.ansible.com/playbooks_best_practices.html

    반응형

    '기타' 카테고리의 다른 글

    Docker를 이용한 Jenkins 설정  (0) 2015.09.16
    chef cookbook 구조  (0) 2015.03.26
    Puppet을 이용한 배포  (0) 2014.08.01
    Puppet 설치 및 실행  (0) 2014.07.31
    vagrant 설치 및 실행  (0) 2014.07.24

    댓글

Designed by Tistory.