Name
Last commit
Last update
..
README.md NTP, modify

logo

Ansible의 jinja2 템플릿을 이용한 NTP 클라이언트 설정

NTP 설정을 자동화할 수 있다.

주요 기능

  • templates/ntp.conf.j2 파일을 이용해 변수에 맞게 NTP 설정을 수행할 수 있다.
  • ntp 와 chrony를 취사선택(apps 호스트 변수)하여, 설치할 수 있다.

Inventory 설정

# cat hosts
[ALL_HOSTS]
172.16.0.100 managed_ip=172.16.0.100 des="2020-12-03" apps="ntp"
172.16.0.200 managed_ip=172.16.0.200 des="2020-12-03" apps="chrony"

[ALL_HOSTS_OK]

대상 호스트는 172.16.0.100,200이며, 추가는 2020-12-03에 수행한다. 참고적으로 ALL_HOSTS_OK 호스트 그룹은 작업이 완료된 호스트의 history를 위한 그룹이다.

Host Variables 설정

# cat host_vars/172.16.0.100
ansible_ssh_host: 172.16.0.100
ansible_ssh_port: SSH_포트번호
ansible_ssh_user: 사용자 아이디
ansible_ssh_pass: "사용자 패스워드"
ansible_become: yes
ansible_become_method: su
ansible_become_user: root
ansible_become_pass: "!root_패스워드"
ansible_python_interpreter: python2.7

Playbook 설정

---
- name: NTP Client Deploy
  hosts: ALL_HOSTS
  vars:
    ntp_server1: "XXX.XXX.XXX.1"
    ntp_server2: "XXX.XXX.XXx.2
  tasks:
   - name: Install a list of packages
     apt:
       pkg:
         - ntp
       update_cache: yes
     when: apps == "ntp"

   - name: ntp.conf configuration
     template: src=templates/ntp.conf.j2 dest=/etc/ntp.conf mode=0644
     when: apps == "ntp"

   - name: ntpd Daemon Restart
     service:
       name: ntp
       state: restarted
       enabled: yes
     when: apps == "ntp"
 
   - name: Install a list of packages
     yum: name={{ item }} update_cache=yes
     with_items:
       - chrony
     when: apps == "chrony"

   - name: ntp.conf configuration
     template: src=templates/chrony.conf.j2 dest=/etc/chrony.conf mode=0644
     when: apps == "chrony"

   - name: ntpd Daemon Restart
     service:
       name: ntp
       state: restarted
       enabled: yes
     when: apps == "ntp"

   - name: chronyd Daemon Restart
     systemd:
       name: chronyd
       state: restarted
       enabled: yes
     when: apps == "chrony"

jinja2 템플릿 내역은 다음과 같다. 참고적으로 핵심적인 내역만은 명시한다.

  • ntp.conf
# cat templates/ntp.conf.j2
...중략
server {{ ntp_server1 }} iburst
server {{ ntp_server2 }} iburst
  • chrony.conf
# cat templates/chrony.conf.j2
...중략
server {{ ntp_server1 }} iburst
server {{ ntp_server2 }} iburst

NTP 클라이언트 배포에 대한 Playbook 분석

  • NTP server1/2 에 대한 변수 값이 원격 호스트에 설정된다.
  • 추가적으로 아래와 같이 ntpq / chronyc 명령을 쿼리하는 Playbook과 같이 사용하면 된다.
  • ntpq / chronyc 명령의 결과는 각 호스트별로 로컬에 파일로 기록된다.

playbook 실행

# ansible-playbook -i hosts ntp_deploy.yml

ntpq 명령의 쿼리결과 수집 Playbook

---
- name: ntpq -p command Result
  hosts: ALL_HOSTS
  vars:
    time: "{{lookup('pipe','date \"+%Y-%m-%d %H:%M:%S\"')}}"
  tasks:
    - name: ntpq command Result
      shell: ntpq -p; echo; date; echo "Deploy Server Time => {{ time }}"
      register: ntpq_result
      when: apps == "ntp"

    - name: ntpq command Result
      shell: chronyc sources -v; echo; date; echo "Deploy Server Time => {{ time }}"
      register: ntpq_result
      when: apps == "chrony"

    - name: Local Directory Create
      local_action: command mkdir -p NTP_CHECK/{{ managed_ip }}

    - name: TIME Check Execute Result
      local_action: copy content={{ ntpq_result.stdout }} dest=NTP_CHECK/{{ managed_ip }}/{{ managed_ip }}_ntpq.txt
# ansible-playbook -i hosts ntp_check.yml