前言:html
生產上使用splunk進行日誌蒐集,服務端已經安裝完成,客戶端有幾十臺須要部署,現用ansible批量安裝。python
環境說明:linux
主機名 | 操做系統版本 | ip | ansible version | 備註 |
---|---|---|---|---|
ansible-awx | Centos 7.6.1810 | 172.27.34.51 | 2.9.9 | ansible管理服務器 |
client | Centos 7.6.1810 | 172.27.34.85 | / | splukn客戶端 |
- 獲取安裝包並解壓
- 新建或修改配置文件inputs.conf和props.conf
- 註冊客戶端到服務端
- 啓動服務並設置爲開機自啓動
客服端安裝分爲4步,第一步是上傳壓縮包,而後解壓;第二步是進入到對應目錄,而後新建(修改)配置文件;第三步是執行‘splunk add forward-server’命令,將客戶端註冊到服務端,此時會有交互窗口,會讓輸入用戶名密碼信息;第四步是運行程序並設置爲開機自啓動。git
本文使用ansible方式進行客戶端批量安裝,調用各對應模塊模擬以上4步。github
因爲會使用到ansible的expect模塊,該模塊運行時須要調用python的pexpect模塊,先安裝python的pexpect模塊web
[root@ansible-awx yaml]# more install_pexpect.yaml --- - hosts: "{{ hostlist }}" tasks: - name: Unarchive ptyprocess unarchive: src: /tmp/splunk/ptyprocess-0.6.0.tar.gz dest: /root mode: 0755 owner: root group: root - name: install ptyprocess shell: "cd /root/ptyprocess-0.6.0;python ./setup.py install" - name: Unarchive pexpect unarchive: src: /tmp/splunk/pexpect-4.8.0.tar.gz dest: /root mode: 0755 owner: root group: root - name: install pexpect shell: "cd /root/pexpect-4.8.0;python ./setup.py install" [root@ansible-awx yaml]# ansible-playbook install_pexpect.yaml -e hostlist=test85
執行邏輯:將ansible服務器的兩個安裝包經過unarchive模塊解壓並傳到splunk客戶端,而後使用shell模塊運行python命令安裝。shell
[root@client ~]# python Python 2.7.5 (default, Oct 30 2018, 23:45:53) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import pexpect >>> >>> exit()
在splunk客戶端運行導入命令,如能正確導入pexpect模塊,則表明安裝成功。bash
[root@ansible-awx roles]# ansible-galaxy init splunk - Role splunk was created successfully
role名爲splunk服務器
[root@ansible-awx ansible]# more splunk.yaml --- - hosts: "{{ hostlist }}" roles: - role: splunk
hosts列表需執行的時候指定。tcp
[root@ansible-awx tasks]# more main.yml --- # tasks file for splunk client install # author: loong576 - name: Unarchive client install file unarchive: src: /tmp/splunk/splunkforwarder-8.0.5-a1a6394cc5ae-Linux-x86_64.tgz dest: /opt mode: 0755 owner: root group: root - name: copy template file copy: src: /etc/ansible/roles/splunk/templates/props.conf dest: /opt/splunkforwarder/etc/system/local owner: root group: root - name: modify template file template: src: /etc/ansible/roles/splunk/templates/inputs.conf dest: /opt/splunkforwarder/etc/system/local/inputs.conf - name: use expect add forward-server expect: command: /opt/splunkforwarder/bin/splunk add forward-server xx.xx.xx.xx:9997 --accept-license responses: Do you agree with this license? [y/n]: "y" Please enter an administrator username: "admin" Please enter a new password: "splunk@123!" Please confirm new password: "splunk@123!" - name: start the client and enable the process shell: "/opt/splunkforwarder/bin/splunk start;/opt/splunkforwarder/bin/splunk enable boot-start"
xx.xx.xx.xx:9997爲splunk服務端ip和端口,根據實際狀況修改。
若是手動安裝,會有交互式界面產生,如圖,需輸入確認信息和用戶名密碼信息。
[root@ansible-awx ansible]# cd /etc/ansible/roles/splunk/templates/ [root@ansible-awx templates]# ll 總用量 8 -rw-r--r-- 1 root root 127 10月 21 16:14 inputs.conf -rw-r--r-- 1 root root 25 10月 21 16:14 props.conf [root@ansible-awx templates]# more inputs.conf [default] index = callcent host = {{ ansible_default_ipv4.address }} sourcetype = messageslog [monitor:///var/log/messages] [root@ansible-awx templates]# more props.conf [callcent] CHARSET=UTF-8
配置文件inputs.con的host參數對應客戶端主機ip,經過ansible_default_ipv4.address獲取;props.conf爲固定文件,無需定製修改。
[root@ansible-awx ansible]# ansible-playbook splunk.yaml -e hostlist=test85
執行對象爲test85
[root@client ~]# netstat -anlp|grep 8089 tcp 0 0 0.0.0.0:8089 0.0.0.0:* LISTEN 17488/splunkd [root@client ~]# ps -ef|grep splunk |grep -v grep root 17488 1 0 16:48 ? 00:00:01 splunkd -p 8089 start root 17496 17488 0 16:48 ? 00:00:00 [splunkd pid=17488] splunkd -p 8089 start [process-runner]
登陸到客戶端,能夠查看到8089端口處於監聽狀態,進程已拉起;也能夠登陸到splunk服務端經過web查看日誌蒐集狀況。
結果符合預期
本文全部腳本和配置文件已上傳github:ansible-production-practice-5
更多請點擊:ansible系列文章