一看必会系列:ansible初始化 centos 7

来源:本站原创 Linux 超过231 views围观 0条评论

一看必会系列:ansible初始化 centos 7

—————–hosts

[initialize-group]
192.168.142.104

目录规化

├── initialize
│   ├── files
│   │   ├── Centos-7.repo
│   │   └── epel.repo
│   └── tasks
│       └── main.yaml
├── initialize.yaml
├── zabbix-agent
│   ├── handlers
│   │   └── main.yaml
│   └── tasks
│       └── main.yaml
└── zabbix-agent.yaml

———-initialize.yaml

– name: centos7 initialize.
  hosts: initialize-group
  remote_user: root
  roles:
  – initialize
  – zabbix-agent

———initialize/tasks/main.yaml
[root@ansible_master roles]# vim initialize/tasks/main.yaml

– name: copy alirepo
  copy: src=Centos-7.repo dest=/etc/yum.repos.d/Centos-7.repo
– name: copy epel
  copy: src=epel.repo dest=/etc/yum.repos.d/epel.repo
#- name: 重构源
#  yum: name=makecache
– name: 安装vim,nettools
#多项同步安装
  yum: name={{ item }} state=installed
  with_items:
     – vim
     – net-tools
– name: 安装ntp
  yum: name=chrony state=installed
– name: 开机启动ntp服务
  service: name=chronyd enabled=yes
– name: 修改时区
  shell: timedatectl set-timezone Asia/Shanghai
– name: 修改ntp配置
  shell: sed -i ‘s/server 0.centos.pool.ntp.org iburst/server 172.30.100.139 iburst/g’ /etc/chrony.conf
– name: 重启ntp服务
  service: name=chronyd state=restarted
– name: 查看同步是否正确
  shell: ‘chronyc sources -v >> /tmp/ntp.log’

———–zabbix-agent/tasks/main.yaml
[root@ansible_master roles]# vim zabbix-agent/tasks/main.yaml

#前提是开启了iptables /firewalld才使用
#- name: add iptables
#  shell: iptables -I  INPUT 1 -p tcp –dport 10050 -j ACCEPT
#- name: save iptables
#  shell: service iptables save
#前提是开启了iptables /firewalld才使用
– name: rpm i
  yum: name=http://repo.zabbix.com/zabbix/3.2/rhel/7/x86_64/zabbix-release-3.2-1.el7.noarch.rpm
– name: yum zabbix-agent
  #yum: name=zabbix-agent state=latest enable=yes
  yum: name=zabbix-agent state=latest
– name: enable service
  shell: systemctl enable zabbix-agent
– name: change server ip
  shell: sed -i ‘s/Server=127.0.0.1/Server=172.16.9.54/g’ /etc/zabbix/zabbix_agentd.conf
– name: change server active ip
  shell: sed -i ‘s/ServerActive=127.0.0.1/ServerActive=172.16.9.54/g’ /etc/zabbix/zabbix_agentd.conf
– name: change server hostname
  shell: sed -i ‘s/Hostname=Zabbix server/Hostname={{hostname}}/g’ /etc/zabbix/zabbix_agentd.conf
  notify :
      – restart zabbix-agent

——以下是思路—————
#替换成国内源
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
yum clean all
yum makecache
yum install epel-release

 

——————-备用

服务器配置chrony  ntp
  yum install -y chrony  安装
  systemctl enable chronyd  开机启动
  systemctl restart chronyd  启动服务
  timedatectl
  chronyc sources

  开放端口看
  [root@shjhpr01 ~]# netstat -ntlpu |grep chrony
udp        0      0 127.0.0.1:323           0.0.0.0:*                           31875/chronyd      
udp        0      0 0.0.0.0:123             0.0.0.0:*                           31875/chronyd      
udp6       0      0 ::1:323                 :::*                                31875/chronyd      
[root@shjhpr01 ~]#

 
客户端配置chrony
vim /etc/chrony.conf
service 172.30.100.139 iburst
保存重启服务
查看同步命令

systemctl restart chronyd
chronyc sources -v
date
timedatectl -h
timedatectl set-timezone Asia/Shanghai
date  确认无误
———————————–
[root@mysql01 ~]# date
Tue Dec 26 02:59:21 EST 2017
[root@mysql01 ~]# !time
-bash: !time: event not found
[root@mysql01 ~]# timedatectl
      Local time: Tue 2017-12-26 02:59:42 EST
  Universal time: Tue 2017-12-26 07:59:42 UTC
        RTC time: Tue 2017-12-26 07:59:42
       Time zone: America/New_York (EST, -0500)
     NTP enabled: yes
NTP synchronized: yes
RTC in local TZ: no
      DST active: no
Last DST change: DST ended at
                  Sun 2017-11-05 01:59:59 EDT
                  Sun 2017-11-05 01:00:00 EST
Next DST change: DST begins (the clock jumps one hour forward) at
                  Sun 2018-03-11 01:59:59 EST
                  Sun 2018-03-11 03:00:00 EDT
—————————————-
[root@mysql01 ~]# timedatectl
      Local time: Tue 2017-12-26 16:00:47 CST
  Universal time: Tue 2017-12-26 08:00:47 UTC
        RTC time: Tue 2017-12-26 08:00:47
       Time zone: Asia/Shanghai (CST, +0800)
     NTP enabled: yes
NTP synchronized: yes
RTC in local TZ: no
      DST active: n/a

6.8
[root@localhost ~]# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*172.30.100.139  85.199.214.100   2 u   24   64    1    2.261    1.572   0.806
7.0
[root@shjhelk01 conf.d]# chronyc sources
210 Number of sources = 1
MS Name/IP address         Stratum Poll Reach LastRx Last sample
===============================================================================
^* 172.30.100.139                2   6   377    42   -203us[+1131us] +/-  145ms
[root@shjhelk01 conf.d]#

测试
[root@localhost ~]# date -s 2000-01-01  修改时间
Sat Jan  1 00:00:00 CST 2000
[root@localhost ~]# date
Sat Jan  1 00:00:02 CST 2000
[root@localhost ~]# service ntpd restart 重启服务
Shutting down ntpd:                                        [FAILED]
Starting ntpd: 
[root@localhost ~]# date
Fri Apr 28 15:50:59 CST 2017
[root@localhost ~]# ntpq -p  查看状态
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*172.30.100.139  61.216.153.104   4 u    9   64    1    2.673    0.396   0.376
[root@localhost ~]# 

文章出自:CCIE那点事 http://www.jdccie.com/ 版权所有。本站文章除注明出处外,皆为作者原创文章,可自由引用,但请注明来源。 禁止全文转载。
本文链接:http://www.jdccie.com/?p=3722转载请注明转自CCIE那点事
如果喜欢:点此订阅本站
  • 相关文章
  • 为您推荐
  • 各种观点