因为要部署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