Ansible 多环境配置和安装 zabbix-agent 服务

1、环境目录结构

# tree /etc/ansible/
/etc/ansible/
├── ansible.cfg
├── environments
│   ├── dev
│   │   ├── group_vars
│   │   │   └── zabbix
│   │   └── hosts
│   ├── prod
│   │   └── group_vars
│   └── test
│       └── group_vars
├── files
│   └── zabbix
│       ├── zabbix-agent-3.4.10-1.el6.x86_64.rpm
│       ├── zabbix-agent-3.4.10-1.el7.x86_64.rpm
│       ├── zabbix-sender-3.4.10-1.el6.x86_64.rpm
│       └── zabbix-sender-3.4.10-1.el7.x86_64.rpm
├── playbooks
│   └── zabbix
│       └── zabbix_agent_install.yml
├── roles
└── templates
    └── zabbix
        └── zabbix_agentd.conf.j2

2、相关的文件内容

# cat /etc/ansible/environments/dev/group_vars/zabbix 
---
zabbix_server_ip: 192.168.1.254

# cat /etc/ansible/environments/dev/hosts 
[lvs]
192.168.1.125

[nginx]
192.168.1.126

[zabbix:children]
lvs
nginx

# cat /etc/ansible/playbooks/zabbix/zabbix_agent_install.yml 
---
- hosts: lvs:nginx
  user: root
  gather_facts: True
  tasks:
    - name: copy zabbix-agent to remote host
      copy: src=/etc/ansible/files/zabbix/zabbix-agent-3.4.10-1.el6.x86_64.rpm dest=/tmp/zabbix-agent-3.4.10-1.el6.x86_64.rpm
    - name: install zabbix agent on centos 6.x
      yum: 
        name: /tmp/zabbix-agent-3.4.10-1.el6.x86_64.rpm
        state: present
      when: ansible_distribution == "CentOS" and ansible_distribution_major_version == "6"
    - name: configure zabbix agent
      template: src=/etc/ansible/templates/zabbix/zabbix_agentd.conf.j2 dest=/etc/zabbix/zabbix_agentd.conf
      notify:
        - restart zabbix-agent 
    - name: start zabbix-agent
      service: name=zabbix-agent state=started enabled=yes
  handlers:
    - name: restart zabbix-agent
      service: name=zabbix-agent state=restarted

# egrep -v "^$|^#" /etc/ansible/templates/zabbix/zabbix_agentd.conf.j2 
PidFile=/var/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
LogFileSize=0
Server={{ zabbix_server_ip }}
ListenPort=10050
ListenIP={{ ansible_default_ipv4.address }}
ServerActive={{ zabbix_server_ip }}
Hostname={{ ansible_hostname }}
Include=/etc/zabbix/zabbix_agentd.d/*.conf
UnsafeUserParameters=1

3、playbook 执行方法

# ansible-playbook playbooks/zabbix/zabbix_agent_install.yml

PLAY [lvs:nginx] ************************************************************************************************************************************************************************************************

TASK [Gathering Facts] ******************************************************************************************************************************************************************************************
ok: [192.168.1.125]
ok: [192.168.1.126]

TASK [copy zabbix-agent to remote host] *************************************************************************************************************************************************************************
ok: [192.168.1.125]
changed: [192.168.1.126]

TASK [install zabbix agent on centos 6.x] ***********************************************************************************************************************************************************************
ok: [192.168.1.125]
changed: [192.168.1.126]

TASK [configure zabbix agent] ***********************************************************************************************************************************************************************************
ok: [192.168.1.125]
changed: [192.168.1.126]

TASK [start zabbix-agent] ***************************************************************************************************************************************************************************************
ok: [192.168.1.125]
changed: [192.168.1.126]

RUNNING HANDLER [restart zabbix-agent] **************************************************************************************************************************************************************************
changed: [192.168.1.126]

PLAY RECAP ******************************************************************************************************************************************************************************************************
192.168.1.125              : ok=5    changed=0    unreachable=0    failed=0   
192.168.1.126              : ok=6    changed=5    unreachable=0    failed=0   


4、再次确认 zabbix-agent 服务是否正常运行

# ansible -i /etc/ansible/environments/dev/hosts all -m shell -a "/etc/init.d/zabbix-agent status"
192.168.1.125 | SUCCESS | rc=0 >>
zabbix_agentd (pid  1283) is running...

192.168.1.126 | SUCCESS | rc=0 >>
zabbix_agentd (pid  1793) is running...


最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容