Ansible 批量安装 node_exporter

安装配置 Ansible

配置 hosts ,并测试连通性

1
2
3
4
5
6
7
8
9
10
# 节点主机名写入hosts
echo "192.168.0.112 prome-master01" >> /etc/hosts
echo "192.168.0.127 prome-node01" >> /etc/hosts

# master上生成ssh key 并拷贝到node上
ssh-keygen
ssh-copy-id prome_node_01

# 测试ssh联通
ssh prome_node_01

Master 上安装 ansible

1
2
3
4
5
6
yum install -y ansible

# 关闭hostcheck
vim /etc/ansible/ansible.cfg

ssh_args = -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no

编写 playbook

playbook执行时需要设置机器文件

1
2
3
4
cat <<EOF > /opt/tgzs/host_file
prome-master01
prome-node01
EOF

设置syslog 和logrotate服务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# 编写 yaml
vim init_syslog_logrotate.yaml

- name: init syslog logrotate
hosts: all
user: root
gather_facts: false
vars:
app_log_path: /opt/logs/
sc_path: /opt/tgzs/
syslog_conf: syslog_server.conf
logrotate_conf: logrotate.conf

tasks:

- name: mkdir
file: path={{ app_log_path }} state=directory


- name: copy files
copy:
src: '{{ item.src }}'
dest: '{{ item.dest }}'
owner: root
group: root
mode: 0644
force: true

with_items:
- { src: '{{ sc_path }}/{{ syslog_conf }}', dest: '/etc/rsyslog.d/{{ syslog_conf }}' }
- { src: '{{ sc_path }}/{{ logrotate_conf }}', dest: '/etc/logrotate.d/{{ logrotate_conf }}' }
register: result

- name: Show debug info
debug: var=result verbosity=0


- name: restart service

systemd:
name: "{{ item }}"
state: restarted
daemon_reload: yes
with_items:
- 'rsyslog'
register: result

- name: Show debug info
debug: var=result verbosity=0


# 设置服务
ansible-playbook -i host_file init_syslog_logrotate.yaml

编写ansible 发布服务脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# 编写 deploy 脚本
vim service_deploy.yaml

- name: install
hosts: all
user: root
gather_facts: false
vars:
local_path: /opt/tgzs
app_dir: /opt/app

tasks:
- name: mkdir
file: path={{ app_dir }}/{{ app }} state=directory
- name: mkdir
file: path={{ local_path }} state=directory


- name: copy config and service
copy:
src: '{{ item.src }}'
dest: '{{ item.dest }}'
owner: root
group: root
mode: 0644
force: true

with_items:
- { src: '{{ local_path }}/{{ tgz }}', dest: '{{ local_path }}/{{ tgz }}' }
- { src: '{{ local_path }}/{{ app }}.service', dest: '/etc/systemd/system/{{ app }}.service' }

register: result
- name: Show debug info
debug: var=result verbosity=0

- name: tar gz
shell: rm -rf /root/{{ app }}* ; \
tar xf {{ local_path }}/{{ tgz }} -C /root/ ; \
/bin/cp -far /root/{{ app }}*/* {{ app_dir }}/{{ app }}/ \

register: result
- name: Show debug info
debug: var=result verbosity=0

- name: restart service
systemd:
name: "{{ item }}"
state: restarted
daemon_reload: yes
enabled: yes
with_items:
- '{{ app }}'
register: result

- name: Show debug info
debug: var=result verbosity=0

# 部署服务
ansible-playbook -i host_file service_deploy.yaml -e "tgz=node_exporter-1.1.2.linux-amd64.tar.gz" -e "app=node_exporter"

查看结果并验证

检查 node_exporter 服务状态

1
ansible -i host_file all -m shell -a " ps -ef |grep node_exporter|grep -v grep "

浏览器访问 9100/metrics

1
2
node-IP:9100/metrics
master-IP:9100/metrics