ansible 批量安装 Centos7&Centos6 zabbix-agent客户端,ansiblecentos7
ansible 批量安装 Centos7&Centos6 zabbix-agent客户端,ansiblecentos7
根据公司需要,使用 ansible安装zabbix-agent客户端 Centos7&Centos6,已经测试没问题。
- hosts: all
remote_user: root
tasks:
- name: CentOS6 system copy zabbix-agent rpm
copy: src=/tmp/zabbix-agent-3.4.9-1.el6.x86_64.rpm dest=/tmp/zabbix-agent-3.4.9-1.el6.x86_64.rpm
when:
- ansible_distribution == "CentOS"
- ansible_distribution_major_version == "6"
- name: CentOS7 system copy zabbix-agent rpm
copy: src=/tmp/zabbix-agent-3.4.9-1.el7.x86_64.rpm dest=/tmp/zabbix-agent-3.4.9-1.el7.x86_64.rpm
when:
- ansible_distribution == "CentOS"
- ansible_distribution_major_version == "7"
- name: yum install zabbix-agent
shell: cd /tmp;yum -y localinstall zabbix-agent-3.4.*.rpm
- name: rm zabbix-agent
shell: cd /tmp;rm -rf zabbix-agent-3.*.rpm
- name: configure Server IP
shell: sed -i 's/Server=127.0.0.1/Server=192.168.58.58/' /etc/zabbix/zabbix_agentd.conf
- name: configure ServerActive IP
shell: sed -i 's/ServerActive=127.0.0.1/ServerActive=192.168.58.58/' /etc/zabbix/zabbix_agentd.conf
- name: configure HostMetadata
shell: sed -i 's/# HostMetadata=/HostMetadata=qas/' /etc/zabbix/zabbix_agentd.conf
- name: CentOS6 system configure Hostname
shell: host=`ifconfig |awk -F '[ :]+' 'NR==2 {print $4}'`;sed -i 's/Hostname=Zabbix server/Hostname='$host'/' /etc/zabbix/zabbix_agentd.conf
when:
- ansible_distribution == "CentOS"
- ansible_distribution_major_version == "6"
- name: CentOS7 system configure Hostname
shell: host=`ifconfig |awk -F '[ :]+' 'NR==2 {print $3}'`;sed -i 's/Hostname=Zabbix server/Hostname='$host'/' /etc/zabbix/zabbix_agentd.conf
when:
- ansible_distribution == "CentOS"
- ansible_distribution_major_version == "7"
- name: CentOS6 system start zabbix
shell: /etc/init.d/zabbix-agent start;chkconfig zabbix-agent on
when:
- ansible_distribution == "CentOS"
- ansible_distribution_major_version == "6"
- name: CentOS7 system start zabbix
shell: systemctl enable zabbix-agent.service;systemctl restart zabbix-agent.service
when:
- ansible_distribution == "CentOS"
- ansible_distribution_major_version == "7"
测试结果
[root@qas-zabbix ansible]# ansible-playbook zabbix_agent.yaml
PLAY [all] **********************************************************************************************************************************************************************************
TASK [Gathering Facts] **********************************************************************************************************************************************************************
ok: [192.168.58.253]
ok: [192.168.58.252]
TASK [CentOS6 system copy zabbix-agent rpm] *************************************************************************************************************************************************
skipping: [192.168.58.253]
changed: [192.168.58.252]
TASK [CentOS7 system copy zabbix-agent rpm] *************************************************************************************************************************************************
skipping: [192.168.58.252]
changed: [192.168.58.253]
TASK [yum install zabbix-agent] *************************************************************************************************************************************************************
changed: [192.168.58.253]
changed: [192.168.58.252]
TASK [rm zabbix-agent] **********************************************************************************************************************************************************************
changed: [192.168.58.252]
changed: [192.168.58.253]
TASK [configure Server IP] ******************************************************************************************************************************************************************
changed: [192.168.58.252]
changed: [192.168.58.253]
TASK [configure ServerActive IP] ************************************************************************************************************************************************************
changed: [192.168.58.252]
changed: [192.168.58.253]
TASK [configure HostMetadata] ***************************************************************************************************************************************************************
changed: [192.168.58.253]
changed: [192.168.58.252]
TASK [CentOS6 system configure Hostname] ****************************************************************************************************************************************************
skipping: [192.168.58.253]
changed: [192.168.58.252]
TASK [CentOS7 system configure Hostname] ****************************************************************************************************************************************************
skipping: [192.168.58.252]
changed: [192.168.58.253]
TASK [CentOS6 system start zabbix] **********************************************************************************************************************************************************
skipping: [192.168.58.253]
changed: [192.168.58.252]
TASK [CentOS7 system start zabbix] **********************************************************************************************************************************************************
skipping: [192.168.58.252]
changed: [192.168.58.253]
PLAY RECAP **********************************************************************************************************************************************************************************
192.168.58.252 : ok=9 changed=8 unreachable=0 failed=0
192.168.58.253 : ok=9 changed=8 unreachable=0 failed=0
cat zabbix_key.yaml
- hosts: all
remote_user: root
tasks:
- name: mkdir zabbix script
shell: mkdir -p /etc/zabbix/script/
- name: copy zabbix_linux.sh script
copy: src=/opt/zabbix/script/zabbix_linux.sh dest=/etc/zabbix/script/zabbix_linux.sh
- name: To grant authorization
shell: chown -R zabbix.zabbix /etc/zabbix/script;chmod +x /etc/zabbix/script/zabbix_linux.sh
- name: configure zabbix_agentd.d
copy: src=/opt/zabbix/script/zabbix_linux.conf dest=/etc/zabbix/zabbix_agentd.d/zabbix_linux.conf
- name: CentOS6 system restart zabbix-agentd
shell: /etc/init.d/zabbix-agent restart
when:
- ansible_distribution == "CentOS"
- ansible_distribution_major_version == "6"
- name: CentOS7 system restart zabbix-agentd
shell: systemctl restart zabbix-agent.service
when:
- ansible_distribution == "CentOS"
- ansible_distribution_major_version == "7"
评论暂时关闭