ansible自动化部署zabbix-agent实验

ansible自动化部署

0.环境

完全参考:利用ansible批量部署zabbix-agent

IP hostname 系统
192.168.255.133 ansible-server CentOS7.6
192.168.255.134 client-1 CentOS7.6
192.168.255.135 client-2 CentOS7.6

1.主服务器安装ansible

安装epel源
yum install -y epel-release
ansible会自动从epel源中下载安装
yum install -y ansible
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-lLiIfDoo-1576068638258)(Untitled/Untitled.png)]

2.三台CentOS7虚机修改hostname,ip地址以及hosts

三台虚机分别修改hostname 为ansible-server client-1 client-2
hostnamectl set-hostname ansible-server
IP分别为192.168.255.133 192.168.255.134 192.168.255.135
修改hosts文件 vi /etc/hosts 添加 IP hostname
重启reboot
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-sqgjX2tE-1576068638259)(Untitled/Untitled%201.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-eJsoZdKk-1576068638259)(Untitled/Untitled%202.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-GrzMlR1I-1576068638260)(Untitled/Untitled%203.png)]
目前三台虚机为
192.168.255.133 ansible-server CentOS7.6
192.168.255.134 client-1 CentOS7.6
192.168.255.135 client-2 CentOS7.6

3.编辑ansible的hosts文件:

主服务器配置
vim /etc/ansible/hosts
新建分组new,hostname写上,后面会赋值给变量{{ hostname }}
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-hoNjIbGj-1576068638260)(Untitled/Untitled%204.png)]

4.创建相关文件夹

创建三个文件夹
files 存放zabbix-agent的rpm包,通过这里分发给各client
templates 存放main.yaml文件
tasks 存放zabbix-agent的配置文件模板,格式为XXX.j2
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-hg58k0pn-1576068638261)(Untitled/Untitled%205.png)]

5.三个目录下分别建立响应文件

进入files文件夹
下载rpm包

cd /etc/ansible/roles/zabbix-agent/files
wget http://repo.zabbix.com/zabbix/4.4/rhel/7/x86_64/zabbix-agent-4.4.3-1.el7.x86_64.rpm

在这里插入图片描述
进入tasks文件夹,添加main.yaml文件

cd tasks
vim main.yaml
- name: Get the zabbix-agent
  copy: src=zabbix-agent-4.4.3-1.el7.x86_64.rpm dest=/root/
- name: Install the zabbix-agent
  command: rpm -ivh /root/zabbix-agent-4.4.3-1.el7.x86_64.rpm
- name: Copy the zabbix_agentd.conf
  template: src=zabbix_agentd.conf.j2 dest=/etc/zabbix/zabbix_agentd.conf mode=644
- name: Start zabbix-agent
  service: name=zabbix-agent state=restarted enabled=true
- name: Delete zabbix-agent package
  shell: rm -rf /root/zabbix-agent-4.4.3-1.el7.x86_64.rpm

进入templates

cd templates
vim zabbix_agentd.conf.j2
PidFile=/var/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
LogFileSize=0
Server=192.168.255.133
ServerActive=192.168.255.133:10050
Hostname={{ hostname }}
Include=/etc/zabbix/zabbix_agentd.d/*.conf

在这里插入图片描述

6.配置ansible无密码SSH到client

产生私钥公钥

cd ~
ssh-keygen

成功后在~/.ssh/路径下将生成ssh密钥文件:id_rsa及id_rsa.pub
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-xqGcG45M-1576068638262)(Untitled/TIM20191211201841.png)]

将公钥发送到192.168.255.134 和192.168.255.135

ssh-copy-id root@192.168.255.134
ssh-copy-id root@192.168.255.135

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-8wqbkV7h-1576068638263)(Untitled/Untitled%208.png)]

7.以ansible方式ping通客户端

ansible new -m ping

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-QjKUD8tt-1576068638264)(Untitled/Untitled%209.png)]
注意:这一步一定要成功

8.建立一个playbook文件

建立在~目录下,名为:zabbix-agent.yaml,该文件的执行可用来调用创建好的roles

vim zabbix-agent.yaml
- hosts: new    // 指定需要执行的组
  remote_user: root
  roles:
  - zabbix-agent    // 指定调用的角色

9.测试playbook文件

ansible-playbook zabbix-agent.yaml --check

无报错,则测试通过,但是不保证跳过的步骤正确执行,还需要执行去测试下的。
安装和删除俩步骤会被跳过
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-0k8qTcVA-1576068638265)(Untitled/Untitled%2010.png)]

10.执行该playbook文件

ansible-playbook zabbix-agent.yaml

[root@ansible-server ~]# ansible-playbook zabbix-agent.yaml --check

PLAY [new] ************************************************************************************************************************************************************

TASK [Gathering Facts] ************************************************************************************************************************************************
ok: [192.168.255.134]
ok: [192.168.255.135]

TASK [zabbix-agent : Get the zabbix-agent] ****************************************************************************************************************************
ok: [192.168.255.135]
ok: [192.168.255.134]

TASK [zabbix-agent : Install the zabbix-agent] ************************************************************************************************************************
skipping: [192.168.255.134]
skipping: [192.168.255.135]

TASK [zabbix-agent : Copy the zabbix_agentd.conf] *********************************************************************************************************************
changed: [192.168.255.134]
changed: [192.168.255.135]

TASK [zabbix-agent : Start zabbix-agent] ******************************************************************************************************************************
changed: [192.168.255.135]
changed: [192.168.255.134]

TASK [zabbix-agent : Delete zabbix-agent package] *********************************************************************************************************************
skipping: [192.168.255.134]
skipping: [192.168.255.135]

PLAY RECAP ************************************************************************************************************************************************************
192.168.255.134            : ok=4    changed=2    unreachable=0    failed=0    skipped=2    rescued=0    ignored=0   
192.168.255.135            : ok=4    changed=2    unreachable=0    failed=0    skipped=2    rescued=0    ignored=0   

[root@ansible-server ~]# ansible-playbook zabbix-agent.yaml

PLAY [new] ************************************************************************************************************************************************************

TASK [Gathering Facts] ************************************************************************************************************************************************
ok: [192.168.255.134]
ok: [192.168.255.135]

TASK [zabbix-agent : Get the zabbix-agent] ****************************************************************************************************************************
ok: [192.168.255.135]
ok: [192.168.255.134]

TASK [zabbix-agent : Install the zabbix-agent] ************************************************************************************************************************
[WARNING]: Consider using the yum, dnf or zypper module rather than running 'rpm'.  If you need to use command because yum, dnf or zypper is insufficient you can add
'warn: false' to this command task or set 'command_warnings=False' in ansible.cfg to get rid of this message.

changed: [192.168.255.134]
changed: [192.168.255.135]

TASK [zabbix-agent : Copy the zabbix_agentd.conf] *********************************************************************************************************************
changed: [192.168.255.134]
changed: [192.168.255.135]

TASK [zabbix-agent : Start zabbix-agent] ******************************************************************************************************************************
changed: [192.168.255.135]
changed: [192.168.255.134]

TASK [zabbix-agent : Delete zabbix-agent package] *********************************************************************************************************************
[WARNING]: Consider using the file module with state=absent rather than running 'rm'.  If you need to use command because file is insufficient you can add 'warn:
false' to this command task or set 'command_warnings=False' in ansible.cfg to get rid of this message.

changed: [192.168.255.134]
changed: [192.168.255.135]

PLAY RECAP ************************************************************************************************************************************************************
192.168.255.134            : ok=6    changed=4    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
192.168.255.135            : ok=6    changed=4    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

[root@ansible-server ~]# 

这次错误发生的原因是因为拷贝的俩虚机已经安装了zabbix-agent4.4.0,用yum remove zabbix-agent即可
在这里插入图片描述
在这里插入图片描述

11.检查

正确例子:

[root@client-1 ~]# systemctl status zabbix-agent
● zabbix-agent.service - Zabbix Agent
   Loaded: loaded (/usr/lib/systemd/system/zabbix-agent.service; enabled; vendor preset: disabled)
   Active: active (running) since 四 2019-12-12 09:50:49 CST; 6min ago
  Process: 19295 ExecStart=/usr/sbin/zabbix_agentd -c $CONFFILE (code=exited, status=0/SUCCESS)
 Main PID: 19298 (zabbix_agentd)
    Tasks: 6
   CGroup: /system.slice/zabbix-agent.service
           ├─19298 /usr/sbin/zabbix_agentd -c /etc/zabbix/zabbix_agentd.conf
           ├─19299 /usr/sbin/zabbix_agentd: collector [idle 1 sec]
           ├─19300 /usr/sbin/zabbix_agentd: listener #1 [waiting for connection]
           ├─19301 /usr/sbin/zabbix_agentd: listener #2 [waiting for connection]
           ├─19302 /usr/sbin/zabbix_agentd: listener #3 [waiting for connection]
           └─19303 /usr/sbin/zabbix_agentd: active checks #1 [idle 1 sec]

12月 12 09:50:49 client-1 systemd[1]: Starting Zabbix Agent...
12月 12 09:50:49 client-1 systemd[1]: Started Zabbix Agent.
[root@client-1 ~]# systemctl list-unit-files | grep zabbix
zabbix-agent.service                          enabled 
[root@client-1 ~]# egrep -v "^$|^#" /etc/zabbix/zabbix_agentd.conf
PidFile=/var/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
LogFileSize=0
Server=192.168.255.133
ServerActive=192.168.255.133:10050
Hostname=client-1
Include=/etc/zabbix/zabbix_agentd.d/*.conf
[root@client-1 ~]# 

我知道问题原因了,因为之前装过,所以会报错,明天先卸载,再重新装一下,这里是第一次错误的测试结果
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-mrjQbkdl-1576068638267)(Untitled/Untitled%2013.png)]

猜你喜欢

转载自blog.csdn.net/qq_33997198/article/details/103499398