前言:html
生產上新入網的服務器都須要安裝prometheus的監控客戶端軟件,主要步驟有:新建監控用戶、拷貝客戶端軟件、拉起客戶端進程、開機自啓動。本文記錄了使用ansible的role方式批量快速的安裝該客戶端軟件。node
本文使用到的主要模塊:user、stat、copy、shell、script、lineinfile等。python
環境說明:git
主機名 | 操做系統版本 | ip | ansible version | 備註 |
---|---|---|---|---|
ansible | Centos 7.6.1810 | 172.27.34.51 | 2.9.9 | ansible管理服務器 |
ansible-awx | Centos 7.6.1810 | 172.27.34.50 | / | 被管服務器 |
[root@ansible ~]# cd /etc/ansible/roles [root@ansible roles]# ansible-galaxy init prometheus - Role prometheus was created successfully [root@ansible roles]# tree prometheus prometheus ├── defaults │ └── main.yml ├── files ├── handlers │ └── main.yml ├── meta │ └── main.yml ├── README.md ├── tasks │ └── main.yml ├── templates ├── tests │ ├── inventory │ └── test.yml └── vars └── main.yml 8 directories, 8 files
使用ansible-galaxy命令初始化role的目錄github
[root@ansible ~]# yum -y install python3-pip
[root@ansible ~]# cd /tmp [root@ansible tmp]# pip3 download passlib==1.7.2 -d /tmp/pkg [root@ansible tmp]# more requirements.txt passlib==1.7.2 [root@ansible tmp]# pip3 install --no-index --find-links=./pkg -r requirements.txt WARNING: Running pip install with root privileges is generally not a good idea. Try `pip3 install --user` instead. Collecting passlib==1.7.2 (from -r requirements.txt (line 1)) Installing collected packages: passlib Successfully installed passlib-1.7.2
生產密碼會使用到Python的passlib模塊web
[root@ansible ~]# python3 -c "from passlib.hash import sha512_crypt; import getpass; print(sha512_crypt.using(rounds=5000).hash(getpass.getpass()))" Password: $6$irgqm/Fea6/O07B7$LJpYtZoKqUkF.pN4D71LX2Cac3TNrF2.1GKGLfaSWxvKupknNLbWNcYym3LuojT3BqUeUCgsrmD/M6FqTx4lK/
輸入明文密碼會生成密碼密文,複製該密碼,後面建立用戶時會用到。shell
[root@ansible ansible]# pwd /etc/ansible [root@ansible ansible]# more prometheus.yaml --- - hosts: "{{ hostlist }}" gather_facts: no roles: - role: prometheus
[root@ansible ~]# cd /etc/ansible/roles [root@ansible roles]# more prometheus/tasks/main.yml --- # tasks file for prometheus # author: loong576 - name: user search shell: id {{ user_name }} register: user_search ignore_errors: true - name: user add user: name: "{{ user_name }}" shell: "{{ user_bash }}" password: "{{ user_password }}" when: user_search.failed == true - name: file search stat: path: "{{ file_dest }}/{{ file_src }}" register: file_search - name: copy files copy: src: "{{ file_src }}" dest: "{{ file_dest }}" mode: 0755 when: file_search.stat.exists == false - name: process search shell: "ps -ef|grep node_exporter |grep -v grep" register: process ignore_errors: true - name: install node_exporter environment: dest: "{{ file_dest }}" src: "{{ file_src }}" port: "{{ node_port }}" script: startup.sh register: start tags: start when: process.failed == true - name: exec when startup lineinfile: dest: /etc/rc.local line: nohup {{ file_dest }}/{{ file_src }} --web.listen-address=:{{ node_port }} >/dev/null &
執行邏輯爲:判斷被執行主機上有無監控用戶,若無則新增;判斷被執行主機有無客戶端文件,若無則拷貝;判斷被執行主機有無客戶端進程,若無則拉起;最後設置客戶端進程開機自啓動。bash
[root@ansible roles]# more prometheus/defaults/main.yml --- # defaults file for prometheus user_name: sysmonitor user_bash: /bin/bash user_password: $6$bB7R8JF3U7L7s/3E$fKOQwpoZ7RESfMmX6uqts1gw4yeXniRNctI2JRBRS2/120EgrHCWS3DboiRhO5sN0CjoVxvtAKgeDVQRaPlc0/ file_src: node_exporter file_dest: /home/sysmonitor node_port: 9100
定義監控用戶的用戶名、shell、密碼,客戶端執行文件的文件名、文件路徑和端口。服務器
[root@ansible roles]# ll prometheus/files/ 總用量 16512 -rw-r--r-- 1 root root 16900416 7月 30 16:04 node_exporter -rwxr--r-- 1 root root 102 7月 31 11:32 startup.sh [root@ansible roles]# more prometheus/files/startup.sh #/bin/bash echo $dest echo $src echo $port nohup $dest/$src --web.listen-address=:$port >/dev/null &
file文件有兩個,node_exporter爲客戶端執行文件,startup.sh爲客戶端進程拉起腳本。ide
[root@ansible ansible]# pwd /etc/ansible [root@ansible ansible]# ansible-playbook prometheus.yaml -e hostlist=test50
‘ -e hostlist=test50’指定被執行的主機爲test50,即172.27.34.50
登錄被管主機test50,發現監控用戶和監控進程都在且加入到了開機自啓動文件中,符合預期。
本文全部腳本和配置文件已上傳github:ansible-production-practice-2
更多請點擊:ansible系列文章