본문 바로가기

리눅스

[리눅스] Ansible Playboot 실행하기

반응형

Ansible Playbook 실행하기

[Control Machine]
1. Playbook examples 파일 다운로드
# cd ./etc/ansible
# git clone git://github.com/ansible/ansible.git --recursive
# git clone https://github.com/ansible/ansible-examples.git --recursive

2-1. Playbook 트리 구조 및 설정 파일
# tree
.
├── roles
│   └── common
│       ├── handlers
│       │   └── main.yml
│       ├── tasks
│       │   └── main.yml
│       └── templates
│           └── ntp.conf.j2
└── site.yml

# cat site.yml
-----
---
# This playbook deploys the whole application stack in this site.

- name: apply common configuration to all nodes
  hosts: all
  remote_user: root

  roles:
    - common
-----

# cat roles/common/handlers/main.yml
-----
---
# Handler to handle common notifications. Handlers are called by other plays.
# See http://docs.ansible.com/playbooks_intro.html for more information about handlers.

- name: restart ntp
  service: name=ntpd state=restarted
-----

# cat roles/common/tasks/main.yml
-----
---
# This playbook contains common plays that will be run on all nodes.

- name: Install ntp
  yum: name=ntp state=present
  tags: ntp

- name: Configure ntp file
  template: src=ntp.conf.j2 dest=/etc/ntp.conf
  tags: ntp
  notify: restart ntp

- name: Start the ntp service
  service: name=ntpd state=started enabled=yes
  tags: ntp

- name: test to see if selinux is running
  command: getenforce
  register: sestatus
  changed_when: false
-----

# cat roles/common/templates/ntp.conf.j2
------
driftfile /var/lib/ntp/drift

restrict 127.0.0.1
restrict -6 ::1

server {{ ntpserver }}

includefile /etc/ntp/crypto/pw

keys /etc/ntp/keys
------

2-2. Playbook 실행(ntp 패키지 설치 및 ntp.conf 배포)
# ansible-playbook site.yml
-----
PLAY [apply common configuration to all nodes] *********************************

TASK [setup] *******************************************************************
ok: [192.168.0.252]

TASK [common : Install ntp] ****************************************************
ok: [192.168.0.252]

TASK [common : Configure ntp file] *********************************************
changed: [192.168.0.252]

TASK [common : Start the ntp service] ******************************************
changed: [192.168.0.252]

TASK [common : test to see if selinux is running] ******************************
ok: [192.168.0.252]

RUNNING HANDLER [common : restart ntp] *****************************************
changed: [192.168.0.252]

PLAY RECAP *********************************************************************
192.168.0.252              : ok=6    changed=3    unreachable=0    failed=0  
-----

[Managed Node]
3-1. Node 서버 패키지 설치된 패키지 확인
# rpm -qa | grep ntp
ntpdate-4.2.6p5-10.el6.centos.2.x86_64
ntp-4.2.6p5-10.el6.centos.2.x86_64

3-1. Node 서버 ntp.conf 파일 확인
# cat /etc/ntp.conf
-----
driftfile /var/lib/ntp/drift

restrict 127.0.0.1
restrict -6 ::1

server 192.168.1.2

includefile /etc/ntp/crypto/pw

keys /etc/ntp/keys
-----

 

728x90
반응형