shell脚本安装ntp server 服务

##############################Deploy ntp server ########################
echo "start deploy ntp server"
 
yum install -y ntp
 
if [ ! -f /var/log/ntpd.log  ];then
      touch /var/log/ntpd.log
fi
 
chown ntp:ntp /var/log/ntpd.log
 
cat $basepath/package/ntp.conf > /etc/ntp.conf
 
systemctl restart ntpd
systemctl enable ntpd
 
ntppid=`ps aux|grep ntp|grep -v "grep"|awk '{print $2}'`
 
if [ "$ntppid" ];then
      echo "success ! ntp-server is running now"
fi
 

 

通过Ansible playbook 方式安装 ntp 

- hosts: 192.168.1.62
  remote_user: root
  tasks:
    - name: Install ntp
      yum:
        name: ntp
        state: present
      tags: ntp

    - name: Configure ntp file
      template:
        src: ./ntp.conf.j2
        dest: /etc/ntp.conf
    - name: restart ntp
      service: name=ntpd state=restarted

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

  

cat ntp.conf.j2 

driftfile /var/lib/ntp/drift

restrict 127.0.0.1 
restrict -6 ::1

server 192.168.1.41

includefile /etc/ntp/crypto/pw

keys /etc/ntp/keys

  

执行结果:

ansible-playbook ntp_setup.yml 
/usr/lib/python2.7/site-packages/requests/__init__.py:91: RequestsDependencyWarning: urllib3 (1.25.3) or chardet (2.2.1) doesn't match a supported version!
  RequestsDependencyWarning)

PLAY [192.168.1.62] *******************************************************************************************************************************************

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

TASK [Install ntp] ********************************************************************************************************************************************
ok: [192.168.1.62]

TASK [Configure ntp file] *************************************************************************************************************************************
changed: [192.168.1.62]

TASK [restart ntp] ********************************************************************************************************************************************
changed: [192.168.1.62]

TASK [Start the ntp service] **********************************************************************************************************************************
ok: [192.168.1.62]

PLAY RECAP ****************************************************************************************************************************************************
192.168.1.62               : ok=5    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   


登录 remote host 查看

ps aux|grep ntp
ntp      27579  0.0  0.0  25720  1920 ?        Ss   16:30   0:00 /usr/sbin/ntpd -u ntp:ntp -g
root     27643  0.0  0.0 112708   984 pts/0    S+   16:30   0:00 grep --color=auto ntp

  

posted @ 2018-03-19 18:03  Oops!#  阅读(1065)  评论(0)    收藏  举报