惯例,自用。。。。。。。
一、主控机安装配置ansible
1、安装
yum install -y ansible
2、配置hosts文件,添加目标主机
3、主控机生成ssh公钥,并发送请求到目标主机
ssh-keygen
ssh-copy-id 目标主机IP
4、测试
ansible all -m ping
二、通过ansible向目标主机部署zabbix-agent
1、修改zabbix_agent.sh文件
2、运行zabbix_agent.yaml剧本
ansible-playbook zabbix-agent.yaml
3、zabbix_agent.yaml文件内容如下:
- hosts: zabbix_agent
remote_user: root
tasks:
- name: close firewall
command: systemctl stop firewalld.service
- name: disable firewall
command: systemctl disable firewalld.service
- name: close selinux
command: setenforce 0
- name: disable selinux
command: sed -i 's\SELINUX=enforcing\SELINUX=disabled\g' /etc/selinux/config
- name: copy sh
copy: src=/etc/ansible/zabbix-agent/zabbix_agent.sh dest=/usr/local/src/zabbix_agent.sh mode=0700
- name: copy yum
copy: src=/etc/ansible/zabbix-agent/CentOS-Base.repo dest=/usr/local/src/CentOS-Base.repo
- name: sent tar
copy: src=/etc/ansible/zabbix-agent/zabbix-3.4.10.tar.gz dest=/usr/local/src/zabbix-3.4.10.tar.gz
notify: install shell
handlers:
- name: install shell
shell: /usr/local/src/zabbix_agent.sh
4、zabbix_agent.sh内容如下:
#!/bin/bash
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.back
cp /usr/local/src/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo
yum clean all
yum install -y gcc*
yum install -y pcre*
groupadd zabbix
useradd -g zabbix zabbix
cd /usr/local/src/
tar -zxvf zabbix-3.4.10.tar.gz
cd zabbix-3.4.10
./configure --enable-agent
make && make install
cp misc/init.d/fedora/core5/zabbix_agentd /etc/init.d/
chmod 700 /etc/init.d/zabbix_agentd
sed -i 's\Server=127.0.0.1\Server=10.0.0.7\g' /usr/local/etc/zabbix_agentd.conf
sed -i 's\ServerActive=127.0.0.1\Server=10.0.0.7\g' /usr/local/etc/zabbix_agentd.conf
sed -i 's\Hostname=127.0.0.1\Server=10.0.0.7\g' /usr/local/etc/zabbix_agentd.conf
/etc/init.d/zabbix_agentd start ##启动
chkconfig zabbix_agentd on ##设为开机启动