Skip to content

Commit 0493139

Browse files
author
luofei1
committed
network:将域名解析的配置独立出来;ntp:简化ntp配置流程;+resolv:新增域名解析
1 parent e368cdd commit 0493139

File tree

8 files changed

+42
-105
lines changed

8 files changed

+42
-105
lines changed

.gitmodules

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
11
[submodule "roles/cloudalchemy.prometheus"]
22
path = roles/cloudalchemy.prometheus
33
url = https://github.com/cloudalchemy/ansible-prometheus
4+
[submodule "roles/ahuffman.resolv"]
5+
path = roles/ahuffman.resolv
6+
url = https://github.com/ahuffman/ansible-resolv
7+
[submodule "roles/geerlingguy.ntp"]
8+
path = roles/geerlingguy.ntp
9+
url = https://github.com/geerlingguy/ansible-role-ntp

files/network/resolv.conf.j2

-2
This file was deleted.

playbooks/network.yml

-10
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424
address: "{{ ansible_default_ipv4['address'] }}"
2525
netmask: "{{ ansible_default_ipv4['netmask'] }}"
2626
gateway: "{{ ansible_default_ipv4['gateway'] }}"
27-
dns1: 180.76.76.76
28-
dns2: 114.114.114.114
2927
# }}}
3028

3129
- shell: | # {{{ 计算子网掩码
@@ -77,14 +75,6 @@
7775
when: ansible_os_family == "RedHat"
7876
# }}}
7977

80-
- template: # {{{ dns设置,针对18.04之前的Ubuntu和Debian
81-
backup: yes
82-
src: files/network/resolv.conf.j2
83-
dest: /etc/resolv.conf
84-
mode: 0644
85-
when: (ansible_distribution == "Ubuntu" and ansible_distribution_version is version(ubuntu_version,"<")) or (ansible_distribution == "Debian")
86-
# }}}
87-
8878
- shell: netplan apply # {{{ 使网络生效
8979
when: ansible_distribution == "Ubuntu" and ansible_distribution_version is version(ubuntu_version,">=")
9080

playbooks/ntp.yml

+16-93
Original file line numberDiff line numberDiff line change
@@ -7,97 +7,20 @@
77
####
88
---
99
- name: 设置ntp时间同步
10-
hosts: dist.ubuntu:dist.centos
10+
hosts: all
1111
tags: ntp
12-
tasks:
13-
- name: 设置时区 # {{{
14-
shell: timedatectl set-timezone 'Asia/Shanghai'
15-
# }}}
16-
17-
- name: 安装ntp服务(RedHat) # {{{
18-
yum:
19-
name: ntp
20-
state: installed
21-
when: ansible_os_family == "RedHat"
22-
# }}}
23-
24-
- name: 安装ntp服务(Debian) # {{{
25-
package:
26-
name: "{{ item }}"
27-
state: latest
28-
with_items:
29-
- ntpdate
30-
- ntp
31-
when: ansible_os_family == "Debian"
32-
# }}}
33-
34-
- name: 设置时间同步(Debian) # {{{
35-
lineinfile:
36-
dest: /etc/ntp.conf
37-
regexp: '^pool [0,1,2,3]'
38-
line: "{{ item }}"
39-
with_items:
40-
- 'server ntp.aliyun.com'
41-
- 'server ntp1.aliyun.com'
42-
- 'server ntp2.aliyun.com'
43-
- 'server ntp3.aliyun.com'
44-
when: ansible_os_family == 'Debian'
45-
# }}}
46-
47-
- name: 设置时间同步(RedHat) # {{{
48-
lineinfile:
49-
dest: /etc/ntp.conf
50-
regexp: '^server [0,1,2,3]'
51-
line: "{{ item }}"
52-
with_items:
53-
- 'server ntp.aliyun.com'
54-
- 'server ntp1.aliyun.com'
55-
- 'server ntp2.aliyun.com'
56-
- 'server ntp3.aliyun.com'
57-
when: ansible_os_family == 'RedHat'
58-
# }}}
59-
60-
- name: 停止ntp服务(Debian) # {{{
61-
service:
62-
name: ntp
63-
state: stopped
64-
when: ansible_os_family == 'Debian'
65-
# }}}
66-
67-
- name: 停止ntp服务(RedHat) # {{{
68-
service:
69-
name: ntpd
70-
state: stopped
71-
when: ansible_os_family == "RedHat"
72-
# }}}
73-
74-
- name: 进行时间同步 # {{{
75-
raw: ntpdate time.windows.com
76-
ignore_errors: true
77-
# }}}
78-
79-
- name: 设置计划任务,每隔一小时同步一次实践 # {{{
80-
lineinfile:
81-
dest: /etc/crontab
82-
line: '* */1 * * * ntpdate -s time.windows.com'
83-
# }}}
84-
85-
- name: 计划任务生效 # {{{
86-
raw: crontab /etc/crontab
87-
# }}}
88-
89-
- name: 启动ntp服务(Debian) # {{{
90-
service:
91-
name: ntp
92-
state: restarted
93-
enabled: yes
94-
when: ansible_os_family == 'Debian'
95-
# }}}
96-
97-
- name: 启动ntp服务(RedHat) # {{{
98-
service:
99-
name: ntpd
100-
state: restarted
101-
enabled: yes
102-
when: ansible_os_family == 'RedHat'
103-
# }}}
12+
roles:
13+
- role: roles/geerlingguy.ntp
14+
ntp_enabled: true
15+
ntp_timezone: "Asia/Shanghai"
16+
ntp_manage_config: true
17+
ntp_servers:
18+
- "ntp{{ '.' + ntp_area if ntp_area else '' }}.aliyun.com"
19+
- "ntp1{{ '.' + ntp_area if ntp_area else '' }}.aliyun.com"
20+
- "ntp2{{ '.' + ntp_area if ntp_area else '' }}.aliyun.com"
21+
- "ntp3{{ '.' + ntp_area if ntp_area else '' }}.aliyun.com"
22+
ntp_restrict:
23+
- "127.0.0.1"
24+
- "::1"
25+
ntp_cron_handler_enabled: true
26+
ntp_tinker_panic: true

playbooks/resolv.yml

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# 设置域名解析
2+
# Leif160519 @20201204
3+
4+
- hosts: all
5+
tags: resolv
6+
roles:
7+
- role: roles/ahuffman.resolv
8+
resolv_nameservers:
9+
- "180.76.76.76"
10+
- "114.114.114.114"
11+
resolv_domain: "localdomain"
12+
resolv_search: "localdomain"
13+
resolv_options:
14+
- "timeout:2"
15+
- "rotate"

playbooks/universal.yml

+3
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,8 @@
2222
# 设置静态IP地址
2323
- import_playbook: network.yml
2424

25+
# 设置域名解析
26+
- import_playbook: resolv.yml
27+
2528
# 安装node_exporter
2629
- import_playbook: prometheus.yml

roles/ahuffman.resolv

Submodule ahuffman.resolv added at 59bc366

roles/geerlingguy.ntp

Submodule geerlingguy.ntp added at 5269d1e

0 commit comments

Comments
 (0)