ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能。
一、此处不是本节重点简要写一部分概括一下!!
1、在ansible自动部署zabbix-agent之前首先在管理机上安装ansible、
ssh-keygen -t rsa
ssh-copy-id root@192.168.71.21
ssh-copy-id root@192.168.71.22
ssh-copy-id root@192.168.71.23
2、并配置ssh免密登录(省事对接下来的操作)
3、添加主机组确定ansible接下来管理哪些机器实现自动部署
开始部署zabbix-agent角色
1、
2、准备zabbix-agent的配置模板
注意:此处适用Jinja模板因为zabbix监控被监控机器时每个机器都有自己的主机名,Jinja模板支持调用变量并且Jinja模板要使用template模块推送,建议把更改过得文件名字以.j2
cp /etc/zabbix/zabbix_agentd.conf /etc/ansible/roles/zabbix/templates/zabbix_agentd.conf
mv /etc/ansible/roles/zabbix/templates/zabbix_agentd.conf /etc/ansible/roles/zabbix/templates/zabbix_agentd.conf.j2
vim /etc/ansible/roles/zabbix/templates/zabbix_agentd.conf.j2
一下需要更改的信息:
Server=192.168.71.10
ServerActive=192.168.71.10
Hostname={{ ansible_fqdn }}
3、开始编写剧本
[root@localhost roles]# cat /etc/ansible/roles/zabbix/tasks/main.yml
---
# tasks file for zabbix
- name: yum install
yum_repository:
name: zabbix
description: YUM zabbix
file: zabbix
baseurl: https://mirrors.aliyun.com/zabbix/zabbix/4.4/rhel/7/x86_64/
gpgcheck: no
enabled: yes
- name: install zabbix-agent
yum:
name: zabbix-agent
state: present
- name: copy
template:
src: zabbix_agentd.conf.j2
dest: /etc/zabbix/zabbix_agentd.conf
notify: server zabbix
- name: systemctl
service:
name: zabbix-agent
state: started
[root@localhost roles]# cat /etc/ansible/roles/zabbix/handlers/main.yml
---
# handlers file for zabbix
- name: server zabbix
service:
name: zabbix-agent
state: restarted
[root@localhost roles]# cat /opt/11/zabbix.yaml
- hosts: sqlserver
user: root
roles:
- zabbix
4、剧本完成,执行剧本
[root@localhost roles]# ansible-playbook /opt/11/zabbix.yaml
PLAY [sqlserver] ************************************************************************************************************************************************************************************************
TASK [Gathering Facts] ******************************************************************************************************************************************************************************************
ok: [192.168.71.22]
ok: [192.168.71.23]
ok: [192.168.71.21]
TASK [zabbix : yum install] *************************************************************************************************************************************************************************************
ok: [192.168.71.22]
ok: [192.168.71.23]
ok: [192.168.71.21]
TASK [install zabbix-agent] *************************************************************************************************************************************************************************************
ok: [192.168.71.22]
ok: [192.168.71.23]
ok: [192.168.71.21]
TASK [zabbix : copy] ********************************************************************************************************************************************************************************************
ok: [192.168.71.22]
ok: [192.168.71.21]
ok: [192.168.71.23]
TASK [zabbix : systemctl] ***************************************************************************************************************************************************************************************
ok: [192.168.71.22]
ok: [192.168.71.23]
ok: [192.168.71.21]
PLAY RECAP ******************************************************************************************************************************************************************************************************
192.168.71.21 : ok=5 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
192.168.71.22 : ok=5 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
192.168.71.23 : ok=5 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[点击并拖拽以移动]