[ansible] playbook 작성/실행 - 실습1

반응형

ansible.cfg 설정값

[defaults]
inventory = ./inventory
remote_user = ansible
ask_pass = false

[privilege_escalation]
#become = true
#become_method = sudo
#become_user = root
#become_ask_pass = true

 

지침

1. web 호스트 그룹 대상으로 수행
2. web_install.yml 플레이북 생성
3. yum 모듈을 사용하여 httpd 패키지 설치
4. copy 모듈을 사용하여 로컬 내 /etc/ansible/data/index.html 파일을 각 관리 호스트(webserver1, webserver2)의 /var/www/html/index.html로 복사
5. service 모듈을 사용하여 httpd 서비스 시작 및 활성화

 

playbook 작성

[ansible@ansible playbook]$ cat web_install.yml
---
- name: Install and start Apache HTTPD
  become: yes
  hosts: web
  tasks:
    - name: httpd package is present
      yum:
        name: httpd
        state: present

    - name: correct index.html is present
      copy:
        src: /etc/ansible/data/index.html
        dest: /var/www/html/index.html

    - name: httpd is started
      service:
        name: httpd
        state: started
        enabled: true
...

 

playbook 구문 체크

[ansible@ansible playbook]$ ansible-playbook --syntax-check web_install.yml 
playbook: web_install.yml

 

playbook 실행

[ansible@ansible playbook]$ ansible-playbook web_install.yml

PLAY [Install and Start Apache Httpd] *****************************************************************

TASK [Gathering Facts] ********************************************************************************
ok: [webserver1]
ok: [webserver2]

TASK [httpd package is present] ***********************************************************************
changed: [webserver2]
changed: [webserver1]

TASK [correct index.html is present] ******************************************************************
changed: [webserver2]
changed: [webserver1]

TASK [httpd is started] *******************************************************************************
changed: [webserver1]
changed: [webserver2]

PLAY RECAP ********************************************************************************************
webserver1             : ok=4    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
webserver2             : ok=4    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

 

playbook 실행 결과 테스트

[ansible@ansible playbook]$ curl http://webserver1
WEB Service Test

[ansible@ansible playbook]$ curl http://webserver2
WEB Service Test

 

반응형

댓글

Designed by JB FACTORY