因爲要部署zabbix監控,但是agent需要每臺機器都安裝較爲麻煩,於是想到了利用ssh協議進行自動化運維管控的ansible進行搭建。
與yum或者apt源安裝不能安裝最新版本,官網上源碼編譯安裝寫的很簡陋,因此手動探索一番。
方式:源碼安裝
安裝用戶:ansible用戶,添加sudo權限
版本:2.2.0 0.0.devel
源碼下載方式
$ git clone git://github.com/ansible/ansible.git --recursive
如下載較慢可以考慮下面地址
$ wget http://releases.ansible.com/ansible/ansible-latest.tar.gz
安裝前需要的環境
源碼編譯器需要安裝以下包才能順利編譯
$ sudo apt-get install gcc python-pip python-dev libffi-dev sshpass $ sudo pip install paramiko PyYAML Jinja2 httplib2six
添加環境變量
$ cd ansible/ $ source hacking/env-setup
進行源碼編譯
$ sudo make install
若沒有error,能夠運行ansible命令即爲安裝完成
將配置文件拷貝到/etc下,該文件在源碼的examples文件夾中
$ sudo cp -r example/* /etc/ansible/ #修改爲root權限就能用了
配置管控機器的遠程IP
$ sudo echo "192.168.3.2" >> /etc/ansible/hosts #執行聯通測試 $ ansible all -m ping -u root 192.168.3.2 | UNREACHABLE! => { "changed": false, "msg": "Failed to connect to the host via ssh.", "unreachable": true }
此時不能到達的原因是ssh公鑰未下發。
生成公鑰
$ ssh-keygen #此時只需一路回車,生成公鑰,ansible不支持ssh公鑰密碼因此不需要輸入密碼 分發公鑰
$ ssh-copy-id IP@USER
下發後重新執行即可
$ ansible all -m ping -u root 192.168.3.2 | SUCCESS => { "changed": false, "ping": "pong" }
注:-u 爲管控用戶命令,上述命令必須加上用戶,因爲是普通用戶管控遠程主機的root權限,如root管控遠程root不需要-u指定用戶。
分發文件
$ ansible all -m copy -u root -a "src=/etc/profile dest=/tmp/aa"
刪除文件
$ ansible all -m file -a "dest=/tmp/aa state=absent" -u root
過程並非如此順利,相關報錯信息我會整理好進行分享。
Zabbix Agent監控部署過程
複製到指定的目錄
$ ansible zabbix -m copy -a "src=/mnt/zabbix-3.2.3.tar.gz dest=/mnt" -u root
壓縮包解壓
$ ansible zabbix -m shell -a "tar -zxvf /mnt/zabbix-3.2.3.tar.gz -C /usr/local/src" -u root
將編譯腳本分發到解壓後的目錄
$ ansible zabbix -m copy -a "src=/mnt/zabbix_install.sh dest=/usr/local/src/zabbix-3.2.3/ mode=755" -u root
確認腳本已經存在
$ ansible zabbix -m shell -a "ll /usr/local/src/zabbix-3.2.3/zabbix_install.sh" -u root 192.168.30.207 | SUCCESS | rc=0 >> -rw-r--r-- 1 root root 164 Jan 23 16:51 /usr/local/src/zabbix-3.2.3/zabbix_install.sh 192.168.30.206 | SUCCESS | rc=0 >> -rw-r--r-- 1 root root 164 Jan 23 16:51 /usr/local/src/zabbix-3.2.3/zabbix_install.sh
執行編譯腳本
$ ansible zabbix -m shell -a "sh /usr/local/src/zabbix-3.2.3/zabbix_install.sh" -u root
分發zabbix-agent.conf配置文件
$ ansible zabbix -m shell -a "src=/mnt/zabbix-3.2.3/misc/init.d/ubuntu/zabbix-agent.conf dest=/etc/lnmp/zabbix-agent.conf" -u root
分發啓動腳本
$ ansible zabbix -m shell -a "src=/mnt/zabbix-3.2.3/misc/init.d/debian/zabbix-agent dest=/etc/init.d/" -u root 192.168.30.206 | SUCCESS | rc=0 >> 192.168.30.207 | SUCCESS | rc=0 >>
確認/usr/local/sbin/是否存在zabbix_agentd
$ ansible zabbix -m shell -a "ls -l /usr/local/sbin/zabbix_agentd" -u root
鏈接sbin到/usr/local/sbin的位置
$ ansible zabbix -m shell -a "ln -s /usr/local/lnmp/zabbix-3.2.3/sbin/* /usr/local/sbin/" -u root
確認sbin的位置
$ ansible zabbix -m shell -a "ls -l /usr/local/sbin/zabbix_agentd" -u root 192.168.30.207 | SUCCESS | rc=0 >> lrwxrwxrwx 1 root root 47 Jan 23 17:10 /usr/local/sbin/zabbix_agentd -> /usr/local/lnmp/zabbix-3.2.3/sbin/zabbix_agentd 192.168.30.206 | SUCCESS | rc=0 >> lrwxrwxrwx 1 root root 47 Jan 23 17:10 /usr/local/sbin/zabbix_agentd -> /usr/local/lnmp/zabbix-3.2.3/sbin/zabbix_agentd
分發agent的固定配置文件
$ ansible zabbix -m copy -a "src=/etc/zabbix_agentd.conf dest=/etc/lnmp" -u root
分發啓動腳本
$ ansible zabbix -m copy -a "src=/mnt/zabbix-3.2.3/misc/init.d/debian/zabbix-agent dest=/etc/init.d mode=755" -u root
啓動agent程序
$ ansible zabbix -m shell -a "service zabbix-agent start" -u root 192.168.30.206 | SUCCESS | rc=0 >> Starting Zabbix agent daemon: zabbix_agentdzabbix_agentd [15924]: user zabbix does not exist zabbix_agentd [15924]: cannot run as root! 192.168.30.207 | SUCCESS | rc=0 >> Starting Zabbix agent daemon: zabbix_agentdzabbix_agentd [16427]: user zabbix does not exist zabbix_agentd [16427]: cannot run as root!
增加zabbix用戶
$ ansible zabbix -m shell -a "useradd zabbix --shell=/sbin/nologin" -u root
啓動agent程序
$ ansible zabbix -m shell -a "service zabbix-agent start" -u root 192.168.30.207 | SUCCESS | rc=0 >> Starting Zabbix agent daemon: zabbix_agentd 192.168.30.206 | SUCCESS | rc=0 >> Starting Zabbix agent daemon: zabbix_agentd
確認端口是否存在
$ ansible zabbix -m shell -a "netstat -an | grep 1005" -u root
產看啓動日誌是否有報錯
$ ansible zabbix -m shell -a "tail -10 /tmp/zabbix_agentd.log" -u root 192.168.30.207 | SUCCESS | rc=0 >> 16708:20170123:173136.720 TLS support: NO 16708:20170123:173136.720 ************************** 16708:20170123:173136.720 using configuration file: /etc/lnmp/zabbix-3.2.3/zabbix_agentd.conf 16708:20170123:173136.720 agent #0 started [main process] 16713:20170123:173136.721 agent #5 started [active checks #1] 16712:20170123:173136.721 agent #4 started [listener #3] 16711:20170123:173136.721 agent #3 started [listener #2] 16713:20170123:173136.721 active check configuration update from [127.0.0.1:10051] started to fail (cannot connect to [[127.0.0.1]:10051]: [111] Connection refused) 16710:20170123:173136.721 agent #2 started [listener #1] 16709:20170123:173136.721 agent #1 started [collector] 192.168.30.206 | SUCCESS | rc=0 >> 16205:20170123:173141.102 TLS support: NO 16205:20170123:173141.102 ************************** 16205:20170123:173141.102 using configuration file: /etc/lnmp/zabbix-3.2.3/zabbix_agentd.conf 16205:20170123:173141.102 agent #0 started [main process] 16210:20170123:173141.103 agent #5 started [active checks #1] 16209:20170123:173141.103 agent #4 started [listener #3] 16208:20170123:173141.103 agent #3 started [listener #2] 16210:20170123:173141.104 active check configuration update from [127.0.0.1:10051] started to fail (cannot connect to [[127.0.0.1]:10051]: [111] Connection refused) 16207:20170123:173141.104 agent #2 started [listener #1] 16206:20170123:173141.104 agent #1 started [collector]
開機自啓的部署
$ ansible zabbix -m shell -a "update-rc.d zabbix-agent defaults 95" -u root