ansible批量安装zabbix_agent

目录

  1. 安装ansible
  2. 解压脚本文件
  3. 配置待安装的虚拟机ip
  4. 手动配置免密登录
  5. 批量配置免密登录
  6. 配置zabbix_agent.yaml
  7. 配置zabbix_agent.sh
  8. 执行zabbix_agent.yaml
链接:https://pan.baidu.com/s/196q-lgmyA-7fV90m5Y_llA 
提取码:h59p 
一:安装ansible
  1. 安装gcc
yum install -y gcc*
  1. 在线安装ansible(与离线安装2选1)
yum install -y ansible
  1. 离线安装ansible(与在线安装2选1)
将 ansible_install.sh 和软件包放入/usr/local/src/ansible 目录下执行 sh ansible_install.sh
  1. 查看是否安装完成
ansible --version
  1. 配置
mkdir /etc/ansible

进入ansible-1.7.2 examples下

cp hosts ansible.cfg /etc/ansible/
二:解压脚本文件
  1. 解压并拷贝ansible_config文件

复制 ansible_config.tar到/etc/ansible目录下

tar -zxvf ansible_config.tar
cd ansible_consig
cp * ../
cd ..

2.拷贝skyform-agent-centos7.3.tar文件

复制skyform-agent-centos7.3.tar到/usr/local/src目录下

二:配置待安装的虚拟机ip
  1. hosts 文件中添加zabbix_agent主机组并添加ip列表
[root@centos1 ~]# cd /etc/ansible/
[root@centos1 ansible]# ls
ansible.cfg  hosts  roles
[root@centos1 ansible]# vi hosts 
[zabbix_agent]
10.15.21.2
10.15.21.3
  1. 查看主机组内的主机
[root@centos1 ansible]# ansible zabbix_agent --list-hosts 
  hosts (3):
    10.15.21.2
    10.15.21.3
  1. 测试是否可以ping通
[root@zk3 ansible]# ansible all -m ping
10.15.21.4 | UNREACHABLE! => {
    "changed": false, 
    "msg": "Failed to connect to the host via ssh: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).\r\n", 
    "unreachable": true
}
10.15.21.2 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
10.15.21.3 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
  1. 查看hosts文件并向所有主机推送修改好的hosts文件(当zabbix-agent配置指定ip时可忽略)
ansible all -m copy -a "src=/etc/hosts dest=/etc/hosts"
  1. 查看组内主机是否配置zabbix_agent
ansible all -m shell -a "ps -ef | grep zabbix_agent"
三:手动配置免密登录
  1. 配置ssh免密通信
[root@centos1 ~]# ssh-keygen
[root@centos1 ~]# ssh-copy-id 10.15.21.2
[root@centos1 ~]# ssh-copy-id 10.15.21.3
四:批量配置免密登录
  1. 在cd /etc/ansible/目录下创建ssh-addkey.yml文件
cd /etc/ansible/
touch ssh-addkey.yml
# ssh-addkey.yml 
- hosts: ssh_key
  gather_facts: no
  tasks:
  - name: install ssh key
    authorized_key: user=root
                    key="{{ lookup('file', '/root/.ssh/id_rsa.pub') }}" 
                    state=present
  1. 执行免密登录
ssh-keygen
ansible-playbook ssh-addkey.yml
  1. 测试脚本(在脚本中获取当前ip)
#sh-addkey.yml
- hosts: ssh_key
  gather_facts: no
  tasks:
  - name: create a file
    shell: "echo {{inventory_hostname}}"
五:配置zabbix_agent.yaml
- hosts: zabbix_agent
  remote_user: root
  tasks:
  - name: copy install_shell to clien
    copy: src=/etc/ansible/zabbix_agent.sh dest=/usr/local/src/zabbix_agent.sh
  - name: sent tar
    copy: src=/usr/local/src/skyform-agent-centos7.3.tar dest=/usr/local/src/skyform-agent-centos7.3.tar
    notify: install shell
  handlers:
  - name: install shell
    shell: /bin/bash /usr/local/src/zabbix_agent.sh
六:配置zabbix_agent.sh
#!/bin/bash
cd /usr/local/src

rm -rf skyform-agent-centos7.3

tar -zxvf skyform-agent-centos7.3.tar

systemctl status zabbix-agent

./skyform-agent-centos7.3/uinstall.sh

rpm -ivh /usr/local/src/skyform-agent-centos7.3/packages/zabbix-agent-3.4.1-1.el7.x86_64.rpm

\cp /usr/local/src/skyform-agent-centos7.3/packages/zabbix_agentd.conf /etc/zabbix/

ipAddr=$(/sbin/ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6 | awk '{print $2}' | tr -d "addr:")
sed -i "15i Hostname=$ipAddr" /etc/zabbix/zabbix_agentd.conf

systemctl enable zabbix-agent

systemctl start zabbix-agent
七:执行zabbix_agent.yaml
ansible-playbook zabbix_agent.yaml
问题
[root@ntp_server ansible]# ansible zabbix_agent -m ping
10.15.21.212 | FAILED => to use the 'ssh' connection type with passwords, you must install the sshpass program
10.15.21.211 | FAILED => to use the 'ssh' connection type with passwords, you must install the sshpass program
yum -y install sshpass
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值