8、安装prometheus-agent、并配置好prometheus-agent
for i in 72 73 74; do ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.1.$i; done
base group_vars hosts site.yml
[root@wld-01-71 roles]# cat hosts
web01 ansible_ssh_host=192.168.1.72
web02 ansible_ssh_host=192.168.1.73
lb01 ansible_ssh_host=192.168.1.74
[root@wld-01-71 roles]# mkdir {group_vars,base/{tasks,handlers,templatess}} -pv
mkdir: 已创建目录 "base/templatess"
[root@wld-01-71 roles]# cat base/tasks/main.yml
- name: Add Base Yum Repository
description: Base Aliyun Repository
baseurl: http://mirrors.aliyun.com/centos/$releasever/extras/$basearch/
gpgkey: http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
- name: Add EPEL Yum Repository
description: EPEL YUM Repository
baseurl: http://mirrors.aliyun.com/epel/7/$basearch/
- name: Add Nginx Yum Repository
description: Nginx YUM Repository
baseurl: http://nginx.org/packages/centos/7/$basearch/
when: (ansible_hostname is match ( "web*" )) or (ansible_hostname is match( "lb*" ))
- name: Add PHP Yum Repository
description: PHP YUM Repository
baseurl: http://us-east.repo.webtatic.com/yum/el7/x86_64/
when: (ansible_hostname is match ( "web*" ))
yum: name={{ packages }} state=present
user: name=www uid=666 group=666 createhome=no shell=/sbin/nologin
user: name=tomcat uid=888 group=888 createhome=no shell=/sbin/nologin
- name: Create Group prometheus
group: name=prometheus gid=777
- name: Create User prometheus
user: name=prometheus uid=777 group=777 createhome=no shell=/sbin/nologin
- name: Add node_exporter server
copy: src=/etc/ansible/roles/base/templatess/node_exporter-0.17.0.linux-amd64.tar.gz dest=/usr/local/src
shell: cd /usr/local/src;tar -xvf node_exporter-0.17.0.linux-amd64.tar.gz -C /usr/local/
file: src={{ "/usr/local/node_exporter-0.17.0.linux-amd64" }} dest={{ "/usr/local/node_exporter" }} state=link
- name: Copy node_exporter.service
copy: src=/etc/ansible/roles/base/templatess/node_exporter.service dest=/usr/lib/systemd/system/
- name: Set the boot to start automatically
shell: systemctl enable node_exporter.service
shell: systemctl start node_exporter.service
- name: Add blackbox_exporter server
copy: src=/etc/ansible/roles/base/templatess/blackbox_exporter-0.14.0.linux-amd64.tar.gz dest=/usr/local/src
shell: cd /usr/local/src;tar -xvf blackbox_exporter-0.14.0.linux-amd64.tar.gz -C /usr/local/
file: src={{ "/usr/local/blackbox_exporter-0.14.0.linux-amd64" }} dest={{ "/usr/local/blackbox_exporter" }} state=link owner=prometheus group=prometheus
- name: Copy blackbox_exporter.service
copy: src=/etc/ansible/roles/base/templatess/blackbox_exporter.service dest=/usr/lib/systemd/system/
copy: src=/etc/ansible/roles/base/templatess/blackbox.yml dest=/usr/local/blackbox_exporter/
- name: Set the boot to start automatically
shell: systemctl enable blackbox_exporter.service
- name: Start blackbox_exporter service
shell: systemctl start blackbox_exporter.service