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 @   Oops!#  阅读(1057)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端
点击右上角即可分享
微信分享提示