SaltStack自动化部署Zabbix Agent

 Zabbix Agent建议在系统初始化的时候直接使用SaltStack进行自动化部署,以后的管理和变更均使用SaltStack来完成。
 
1.准备SaltStack环境(略)
 
注:可以参考SaltStack专栏里面的SaltStack快速入门。
 
2.创建对应的目录

root@linux-node1 ~]# mkdir /srv/salt/base/zabbix/files/ -p

3.准备Zabbix配置文件,手动安装zabbix-agent后,copy文件到files目录下:

[root@linux-node1 ~]# yum install -y zabbix-agent
[root@linux-node1 ~]# cp /etc/zabbix/zabbix_agentd.conf /srv/salt/base/zabbix/files/

4.修改zabbix_agentd.conf为模板文件

[root@linux-node1 ~]# vim /srv/salt/base/zabbix/files/zabbix_agentd.conf 
Server={{ ZABBIX_SERVER }}
ServerActive={{ ZABBIX_ACTIVE_SERVER }}
Hostname={{ AGENT_HOSTNAME }}

5.编写状态文件:

[root@linux-node1 ~]# vim /srv/salt/base/zabbix/zabbix-agent.sls
zabbix-repo:
  cmd.run:
    - name: rpm -i https://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-1.el7.noarch.rpm 
    - unless: rpm -qa | grep zabbix-release  

zabbix-agent:
  pkg.installed:
    - name: zabbix-agent
    - version: 4.0.2-1.el7
    - require:
      - cmd: zabbix-repo
  file.managed:
    - name: /etc/zabbix/zabbix_agentd.conf
    - source: salt://zabbix/files/zabbix_agentd.conf
    - template: jinja
    - defaults:
      ZABBIX_SERVER: 192.168.56.11,192.168.56.12
      ZABBIX_ACTIVE_SERVER: 192.168.56.11:10051,192.168.56.12:10051
      AGENT_HOSTNAME: {{ grains['fqdn'] }} 
    - require:
      - pkg: zabbix-agent
  service.running:
    - enable: True
    - watch:
      - pkg: zabbix-agent
      - file: zabbix-agent

6.执行状态

[root@linux-node1 ~]# salt '*' state.sls zabbix.zabbix-agent
linux-node1.example.com:
----------
          ID: zabbix-repo
    Function: cmd.run
        Name: rpm -i https://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-1.el7.noarch.rpm
      Result: True
     Comment: unless condition is true
     Started: 17:19:44.412981
    Duration: 938.341 ms
     Changes:   
----------
          ID: zabbix-agent
    Function: pkg.installed
      Result: True
     Comment: All specified packages are already installed and are at the desired version
     Started: 17:19:49.124383
    Duration: 2037.406 ms
     Changes:   
----------
          ID: zabbix-agent
    Function: file.managed
        Name: /etc/zabbix/zabbix_agentd.conf
      Result: True
     Comment: File /etc/zabbix/zabbix_agentd.conf is in the correct state
     Started: 17:19:51.165575
    Duration: 53.529 ms
     Changes:   
----------
          ID: zabbix-agent
    Function: service.running
      Result: True
     Comment: The service zabbix-agent is already running
     Started: 17:19:51.220700
    Duration: 56.149 ms
     Changes:   

Summary for linux-node1.example.com
------------
Succeeded: 4
Failed:    0
------------
Total states run:     4
Total run time:   3.085 s
linux-node2.example.com:
----------
          ID: zabbix-repo
    Function: cmd.run
        Name: rpm -i https://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-1.el7.noarch.rpm
      Result: True
     Comment: unless condition is true
     Started: 17:19:45.512368
    Duration: 1108.944 ms
     Changes:   
----------
          ID: zabbix-agent
    Function: pkg.installed
      Result: True
     Comment: All specified packages are already installed and are at the desired version
     Started: 17:19:49.699172
    Duration: 1787.491 ms
     Changes:   
----------
          ID: zabbix-agent
    Function: file.managed
        Name: /etc/zabbix/zabbix_agentd.conf
      Result: True
     Comment: File /etc/zabbix/zabbix_agentd.conf is in the correct state
     Started: 17:19:51.490851
    Duration: 53.646 ms
     Changes:   
----------
          ID: zabbix-agent
    Function: service.running
      Result: True
     Comment: The service zabbix-agent is already running
     Started: 17:19:51.546009
    Duration: 63.202 ms
     Changes:   

Summary for linux-node2.example.com
------------
Succeeded: 4
Failed:    0
------------
Total states run:     4
Total run time:   3.013 s

 作者:赵班长

地址:https://www.unixhot.com/article/139465

猜你喜欢

转载自blog.csdn.net/yetugeng/article/details/86635049