#######
使用ansible(playbook)安装node_exporter
目标:在主机192.168.1.102上给192.168.1.103和192.168.1.104安装node_exporter
准备:“node_exporter”文件和“install_node_exporter.sh”文件
[root@W102 install_node_exporter]# tree playbook/
playbook/
├── ansible.cfg
├── hosts
├── ins_expor.yml
├── log
│ └── ansible.log
└── template
├── install_node_exporter.sh
└── node_exporter
2 directories, 7 files
[root@W102 install_node_exporter]#
[root@W102 playbook]# ls
ansible.cfg hosts ins_expor.yml log template
[root@W102 playbook]# cat ansible.cfg
[defaults]
inventory = ./hosts
host_key_checking = False
forks = 5
default_sudo_user = root
remote_port = 22
timeput = 20
log_path = ./log/ansible.log
[root@W102 playbook]# cat hosts
[proc]
192.168.1.103 ansible_user=root ansible_password=c
192.168.1.104 ansible_host=192.168.1.104 ansible_user=root ansible_password="c"
[root@W102 playbook]# cat ins_expor.yml
---
- hosts: proc
remote_user: root
vars:
dest_dir: /data/nodeExporter/
exporter_src_path: /data/ansible/install_node_exporter/playbook/template/node_exporter
install_exporter_src_path: /data/ansible/install_node_exporter/playbook/template/install_node_exporter.sh
dest_path: /tmp/ins_expor.yml
sh_file: install_node_exporter.sh
tasks:
- name: mkdir
shell: "mkdir -p {{dest_dir}}"
- name: copy node_exporter
copy: "src={{exporter_src_path}} dest={{dest_dir}}"
- name: copy install_node_exporter.sh
copy: "src={{install_exporter_src_path}} dest={{dest_dir}}"
- name: bash install_node_exporter.sh
shell: "echo {{dest_dir}}{{sh_file}} && bash {{dest_dir}}{{sh_file}}"
[root@W102 template]# cat install_node_exporter.sh
#!/bin/bash
export port=9100
expname=node_exporter
src_file=/data/nodeExporter/node_exporter
dst_file=/usr/local/bin/${expname}
serverfile=/etc/systemd/system/node_exporter.service
servicename=node_exporter.service
if [ -e ${dst_file} ];then
export port=9101
expname=node_exporter${port}
dst_file=/usr/local/bin/${expname}
fi
cp ${src_file} ${dst_file}
chmod 751 ${dst_file}
if [ -e ${serverfile} ];then
servicename=node_exporter${port}.service
serverfile=/etc/systemd/system/${servicename}
fi
touch ${serverfile}
chmod 751 ${serverfile}
#配置node_exporter开机自启
cat > ${serverfile} << EOF
[Unit]
Description=Prometheus Node Exporter
After=network.target
[Service]
ExecStart=/usr/local/bin/node_exporter --web.listen-address=:${port}
User=root
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable ${servicename}
systemctl start ${servicename}
netstat -ntulp | grep ${port}
[root@W102 template]#
[root@W102 playbook]# pwd
/data/ansible/install_node_exporter/playbook
[root@W102 playbook]# ansible-playbook ins_expor.yml