Ansible 是一个自动化管理it资源的工具

Ansible 的优点:

  • 无客户端
  • 推送式
  • 丰富的module 1w+ 覆盖主流安装脚本
  • 基于YAML的playbook (批量执行脚本)
  • 商业化支持

缺点:

  • 效率低 易挂起 基于ssh
  • 并发性能差(确实)

使用 Python 编写,追求自动化,而不是性能。

Ansible 对比其他

目的:需要在内网的几台服务器上部署zabbix-agent监控

git仓库:192.168.2.156
test环境:192.168.2.159
zabbix-server+ansible:192.168.2.125

OS:Centos7

centos上面的zabbix-server 有个坑的地方:
必须要关闭SELINUX,如果没关,zabbix-server启动不了,没权限绑定套接字的,日志如下:

1
25485:20180704:050722.028 cannot start preprocessing service: Cannot bind socket to "/var/run/zabbix/zabbix_server_preprocessing.sock": [13] Permission denied.

临时关闭SELINUX(无需重启)

1
setenforce 0

永久关闭SELINUX(需重启)
vim /etc/selinux/config

1
将SELINUX=enforcing改为SELINUX=disabled

zabbix-server的详细安装请看:传送门

server端启动自动发现

启动自动发现

配置 - 自动发现
默认会有个名叫”Local network”的未启用的规则,修改这个规则:

1
2
3
4
5
6
7
name:Local network
discovery by proxy:no proxy
ip range:192.168.2.1-254
update interval:1h
checks new:Zabbix-agent "system.uname"
device uniqueness criteria:ip address
enabled:yes

勾选启用。

配置自动发现后的动作

参考:https://blog.csdn.net/jing875480512/article/details/79064583

安装配置ansible

安装ansible

yum install ansible

配置ansible

vim /etc/ansible/ansible.cfg

1
2
3
4
[defaults]
inventory = /etc/ansible/hosts
[ssh_connection]
ssh_args = -o StrictHostKeyChecking=no

添加主机

vim /etc/ansible/hosts

1
2
3
4
5
6
git ansible_ssh_host=192.168.2.156 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=pass
dev ansible_ssh_host=192.168.2.159 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=pass

[test]
git
dev

test是分组,分组内有两台别名为”git”和”dev”的机器。

如果是使用密钥的方式

1
ansible_ssh_private_key_file=/home/user/private.pem

以及Inventoy参数选项

1
2
3
4
5
6
7
8
9
10
ansible_ssh_host
ansible_ssh_port
ansible_ssh_user
ansible_ssh_pass ssh密码
ansible_sudo_pass sudo密码
ansible_ssh_exe ssh执行路径
ansible_connection 与主机的连接类型 比如:local,ssh或者paramiko
ansible_ssh_private_key_file 私钥文件
ansible_shell_type 目标系统的shell类型
ansible_python_interpreter python解释器

测试主机

ping测试

1
2
3
4
5
6
7
8
9
# ansible all -m ping
192.168.2.159 | SUCCESS => {
"changed": false,
"ping": "pong"
}
192.168.2.156 | SUCCESS => {
"changed": false,
"ping": "pong"
}

ansible推送安装zabbix-agent

编写安装playbook的yaml配置文件

正常安装zabbix-agent的步骤如下:

1
2
3
4
rpm -i http://repo.zabbix.com/zabbix/3.4/rhel/7/x86_64/zabbix-release-3.4-2.el7.noarch.rpm 
yum install -y zabbix-agent
sed -i 's\Server=127.0.0.1\Server=192.168.2.125\g' /etc/zabbix/zabbix_agentd.conf
service zabbix-agent start

改写成playbook

vim /etc/ansible/zabbix-agent.yaml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
- hosts: test
gather_facts: no
remote_user: root
become: true
tasks:
- name: install zabbix_agent rpm
yum:
name: http://repo.zabbix.com/zabbix/3.4/rhel/7/x86_64/zabbix-release-3.4-2.el7.noarch.rpm
state: installed
when: ansible_distribution == 'CentOS'
- name: install zabbix_agent
yum:
name: "{{ item }}"
state: installed
with_items:
- zabbix-agent
when: ansible_distribution == 'CentOS'
- name: config zabbix server
replace:
path: /etc/zabbix/zabbix_agentd.conf
regexp: Server=127.0.0.1
replace: Server=192.168.2.125
when: ansible_distribution == 'CentOS'
- name: disable selinux
selinux:
state: disabled
when: ansible_distribution == 'CentOS'
- name: start zabbix agent
systemd:
name: zabbix-agent
state: started
when: ansible_distribution == 'CentOS'

此yaml剧本文件仅支持centos

使用playbook推送安装

ansible-playbook /etc/ansible/zabbix-agent.yaml

执行结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
[root@ansible-test ~]# ansible-playbook /etc/ansible/zabbix_agent.yaml 

PLAY [test] ***************************************************************************************************************************

TASK [Gathering Facts] ****************************************************************************************************************
ok: [dev]
ok: [git]

TASK [install zabbix_agent rpm] *******************************************************************************************************
ok: [dev]
ok: [git]

TASK [install zabbix_agent] ***********************************************************************************************************
ok: [dev] => (item=[u'zabbix-agent'])
ok: [git] => (item=[u'zabbix-agent'])

TASK [config zabbix server] ***********************************************************************************************************
ok: [dev]
ok: [git]

TASK [disable selinux] ****************************************************************************************************************
ok: [dev]
ok: [git]

TASK [start zabbix agent] *************************************************************************************************************
changed: [dev]
changed: [git]

PLAY RECAP ****************************************************************************************************************************
dev : ok=6 changed=1 unreachable=0 failed=0
git : ok=6 changed=1 unreachable=0 failed=0

命令返回中有“successful”字符串,则为 changed 状态。
因为前几个task我执行过了,所以再次执行playbook显示ok状态。

回到server的web端:
监测中 - 自动发现,已经发现了这两台主机。
配置 - 主机,主机已经添加好了,并链接好了模板。

部分参考:http://blog.51cto.com/3381847248/2053858