1.安装ansible

sudo apt update
sudo apt-get install software-properties-common
sudo apt-add-repository --yes  ppa:ansible/ansible:2.7.6
sudo apt update
sudo apt-get install ansible

2.添加ansible 主机

两种不同的系统安装方法略微不同,分为两组采用不同playbook
vim /etc/ansible/hosts
 [ubuntu_groups] 
 192.168.0.30 ansible_ssh_private_key_file=/root/test.pem
 192.168.0.10 ansible_ssh_private_key_file=/root/test.pem
 [centos7_groups]
 192.168.0.29 ansible_ssh_private_key_file=/root/test.pem
 192.168.0.28 ansible_ssh_private_key_file=/root/test.pem
 192.168.0.27 ansible_ssh_private_key_file=/root/test.pem

3.编写playbook

vim  /etc/ansible/ubuntu.yml (ubuntu 操作系统)
---
 name: ubuntu playbook
 hosts:  ubuntu_groups 
 remote_user: root
 gather_facts: False
 ignore_errors: True
 tasks:
 name: install zabbix-agent
 shell: apt-get install -y zabbix-agent
 name: Server
 lineinfile:
   backrefs: yes
   path: /etc/zabbix/zabbix_agentd.conf
   regexp: '^Server=127.0.0.1'
   line: 'Server=192.168.0.30'
 name: ServerActive
 lineinfile:
   backrefs: yes
   path: /etc/zabbix/zabbix_agentd.conf
   regexp: '^ServerActive=127.0.0.1'
   line: 'ServerActive=192.168.0.30'
 name: restart
 service:
   name=zabbix-agent
   enabled=yes
   state=restarted 
vim /etc/ansible/centos7.yml   (centos7操作系统)
---
 name: centos7 playbook
 #  hosts: centos7_groups
 hosts: fugong
 remote_user: root
 gather_facts: False
 ignore_errors: True
 tasks:
 name: install rpm
 yum:
   name=https://repo.zabbix.com/zabbix/3.2/rhel/7/x86_64/zabbix-release-3.2-1.el7.noarch.rpm
   state=present
 name: yum makecache
 yum:
   name=*
   update_cache=yes
 name: install agent
 yum:
   name=zabbix-agent
   state=latest
 name: install agent-get
 yum:
   name=zabbix-get
   state=latest
 name: install agent-sender
 yum:
    name=zabbix-sender
    state=latest
 name: Server
 lineinfile:
   backrefs: yes
   path: /etc/zabbix/zabbix_agentd.conf
   regexp: '^Server=127.0.0.1'
   line: 'Server=192.168.0.30'
 name: ServerActive
 lineinfile:
   backrefs: yes
   path: /etc/zabbix/zabbix_agentd.conf
   regexp: '^ServerActive=127.0.0.1'
   line: 'ServerActive=192.168.0.30'
 name: restart
 service:
   name=zabbix-agent
   enabled=yes
   state=restarted 

4.检查语法

 --syntax-check   #检查Playbook中的语法书写,并不实际执行
 --check   #模拟执行,不会真正在机器上执行(查看执行会产生什么变化)
ansible-playbook --syntax-check  centos7.yml
ansible-playbook --syntax-check   ubuntu.yml  
ansible-playbook --check  centos7.yml
ansible-playbook --check   ubuntu.yml   

5.执行安装

ansible-playbook  centos7.yml 
ansible-playbook  ubuntu.yml  

6. 完成

如果一切顺利的话zabbix-agent 已经安装完成,登录安装主机检查查看下进程即可。

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据