前言:

时间服务器部署,这样可以防止网络带来的阻塞和延迟导致服务器的时间不一致,时间的不一致容易导致各种错误。假如监控时间报错了,你老大会怎么想

部署:

1.安装ntp

yum -y install ntp
[root@nfs01 ~]# rpm -qa ntpntp-4.2.6p5-10.el6.centos.2.x86_64

2.配置文件

[root@m01 ~]# grep -Ev "#|^$" /etc/ntp.conf driftfile /var/lib/ntp/drift
restrict default nomodify
restrict 127.0.0.1 restrict -6 ::1server ntp1.aliyun.com
server time.nist.gov
includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys

步骤,此处略low,可以使用直推推送配置文件的方法

sed -i 's#server 3.centos.pool.ntp.org iburst##g' /etc/ntp.confsed -i 's#server 2.centos.pool.ntp.org iburst##g' /etc/ntp.confsed -i 's#server 1.centos.pool.ntp.org iburst##g' /etc/ntp.confsed -i 's#server 0.centos.pool.ntp.org iburst##g' /etc/ntp.confsed -i 's#restrict default kod nomodify notrap nopeer noquery##g'sed -i 's#restrict -6 default kod nomodify notrap nopeer noquery##g'echo -e "server ntp1.aliyun.com\nserver time.nist.gov\nrestrict default nomodify" >> /etc/ntp.conf

3.启动并查看

/etc/init.d/ntpd startntpq -p

剧本

---- hosts: 172.16.1.61  tasks:  - name: install ntp    yum:      name: ntp      state: installed  - name: update cfg    shell: sed -i 's#server 3.centos.pool.ntp.org iburst##g' /etc/ntp.conf;sed -i 's#server 2.centos.pool.ntp.org iburst##g' /etc/ntp.conf;sed -i 's#server 1.centos.pool.ntp.org iburst##g' /etc/ntp.conf;sed -i 's#server 0.centos.pool.ntp.org iburst##g' /etc/ntp.conf;sed -i 's#restrict default kod nomodify notrap nopeer noquery##g' /etc/ntp.conf;sed -i 's#restrict -6 default kod nomodify notrap nopeer noquery##g /etc/ntp.conf';echo -e "server ntp1.aliyun.com\nserver time.nist.gov\nrestrict default nomodify" >> /etc/ntp.conf  - name: start ntp    service:      name: ntpd      state: started      enabled: yes- hosts: aige  tasks:  - name: cron ntp    cron:      name: sync time      minute: "*/5"      job: '/usr/sbin/ntpdate 172.16.1.61 >/dev/null 2>&1'