标签:get str perm 监控 var ansible 目录 inventory The
SSH部分
操作主机创建密钥
[root@localhost ~]# ssh-keygen
[root@localhost ~]# ls .ssh/
id_rsa id_rsa.pub
分发公钥到被监控端
[root@localhost .ssh]# ssh-copy-id -i id_rsa.pub root@192.168.30.43
批量实现SSH免密登录,参考另外一篇博文。
Playbook部分
├── roles
│ └── zabbix_agent_install
│ ├── files
│ │ └── zabbix-agent-5.2.4-1.el7.x86_64.rpm
│ ├── tasks
│ │ ├── install.yml
│ │ └── main.yml
│ └── vars
│ └── main.yml
└── zabbix_agent.yml
修改主机组
vim /etc/ansible/hosts
[zbxagent]
192.168.30.41
192.168.30.42
192.168.30.43
创建管理目录:
mkdir -p zabbix_agent/roles/zabbix_agent_install/{files,tasks,vars}
创建zabbix_agent入口文件,用来调用zabbix_agent_install
# vim zabbix_agent.yml
#用于批量安装Zabbix-agent
- hosts: zbxagent
remote_user: root
gather_facts: True
roles:
- zabbix_agent_install
创建变量:
# vim roles/zabbix_agent_install/vars/main.yml
#定义zabbix安装中的变量,本次安装5.2,rhel7下rpm包,其他版本参考https://repo.zabbix.com/zabbix/
ZABBIX_VER: 5.2
RPM_URL: https://repo.zabbix.com/zabbix/{{ ZABBIX_VER }}/rhel/7/x86_64/zabbix-release-{{ ZABBIX_VER }}-1.el7.noarch.rpm
SERVER_IP: 192.168.30.128 #指定zabbix-server IP
安装和配置文件:
vim roles/zabbix_agent_install/tasks/install.yml
#当前主机下没有RPM包
#- name: 下载RPM包
# get_url: url={{ RPM_URL }} dest={{ SOURCE_DIR }}
- name: 创建所需目录
file: name={{ item }} state=directory mode=0755 recurse=yes
with_items:
- "{{ SOURCE_DIR }}"
#当前主机file目录下已有RPM包
- name: 拷贝现有RPM包到所有主机
copy: src=. dest={{ SOURCE_DIR }}
- name: Finding RPM files
find:
paths: "{{ SOURCE_DIR }}"
patterns: "*rpm"
register: rpm_result
- name: debug
debug: var=rpm_result.files
- name: Install RPM
yum:
name: "{{ item.path }}"
state: present
with_items:
- "{{ rpm_result.files }}"
- name: 修改zabbix_agent配置_1
lineinfile:
dest: /etc/zabbix/zabbix_agentd.conf
regexp: "Server=127.0.0.1"
insertbefore: "### Option: ListenPort"
line: "Server={{ SERVER_IP }}"
- name: 修改zabbix_server配置_2
lineinfile:
dest: /etc/zabbix/zabbix_agentd.conf
insertafter: "# StartAgents=3"
line: "StartAgents=3"
- name: 修改zabbix_server配置_3
lineinfile:
dest: /etc/zabbix/zabbix_agentd.conf
regexp: "ServerActive=127.0.0.1"
insertbefore: "### Option: Hostname"
line: "ServerActive={{ SERVER_IP }}"
- name: 修改zabbix_server配置_4
lineinfile:
dest: /etc/zabbix/zabbix_agentd.conf
regexp: "Hostname=Zabbix server"
insertbefore: "### Option: HostnameItem"
line: "Hostname={{ hostvars[inventory_hostname][‘ansible_default_ipv4‘][‘address‘] }}"
- name: 修改zabbix_server配置_5
lineinfile:
dest: /etc/zabbix/zabbix_agentd.conf
insertafter: "# UnsafeUserParameters=0"
line: "UnsafeUserParameters=1"
- name: 启动zabbix-agent并开机启动
service:
name: zabbix-agent
state: started
enabled: yes
- name: 防火墙放行zabbix-agent
firewalld:
port: 10050/tcp
permanent: yes
immediate: yes
state: enabled
引用文件main.yml:
vim roles/zabbix_agent_install/tasks/main.yml
- include: install.yml
批量部署安装
ansible-playbook zabbix_agent.yml
Ansible用playbook批量安装zabbix agent
标签:get str perm 监控 var ansible 目录 inventory The
原文地址:https://www.cnblogs.com/st2021/p/14490035.html