playbooks 是一种简单的配置管理系统与多机器部署系统的基础。与现有的其他系统有不同之处,且非常适合于复杂应用的批量部署
Playbooks 的格式是YAML,语法做到最小化

这里讲述的是使用ansible-playbooks的roles安装Zabbix应用

Ansible之源码安装Zabbix-sever

  • Zabbix_Server结构目录
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
#结构目录
[root@Ansible ~]# tree /etc/ansible/
/etc/ansible/
├── ansible.cfg
├── hosts
├── roles
│   └── source_zabbix_server
│   ├── files
│   │   ├── authorization.sql
│   │   ├── initialize.sql
│   │   ├── yum.repo
│   │   └── zabbix-3.2.4.tar.gz
│   ├── handlers
│   │   └── main.yml
│   ├── tasks
│   │   └── main.yml
│   ├── templates
│   │   ├── fastcgi.conf.j2
│   │   ├── zabbix_agentd.conf
│   │   ├── zabbix.conf
│   │   └── zabbix_server.conf
│   └── vars
│   └── main.yml
└── site.yml
7 directories, 14 files
[root@Ansible ~]#

环境:
   ansible 2.2.1.0
   Centos 6.6
   zabbix-3.2.4

参考:
Ansible-Playbooks之安装LNMP

1
2
3
4
5
6
7
8
9
10
11
#入口文件
[root@Ansible ~]# cd /etc/ansible/
[root@Ansible ansible]# cat site.yml
- hosts: test_hosts
user: root
roles:
- yum_mysql
- yum_php
- source_nginx
- source_zabbix_server
[root@Ansible ansible]#

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#tasks
[root@Ansible ansible]# cat roles/source_zabbix_server/tasks/main.yml
- name: disabled iptables
service: name=iptables state=stopped
- name: Set the selinux value to Permissive
shell: setenforce 0
- name: Install libselinux-python package
yum: name=libselinux-python state=installed
- name: Check whether yum.repo file exist
shell: ls /etc/yum.repos.d/yum.repo
register: result
ignore_errors: True
- name: Copy yum.repo file
copy: src="files/yum.repo" dest="/etc/yum.repos.d/yum.repo" mode=0644 owner=root group=root
when: result|failed
- name: Rebuild the yum cache
shell: yum clean all && yum makecache
when: result|failed
- name: Install same devel packages
yum: name="{{item.name}}"
with_items:
- name: "net-snmp"
- name: "net-snmp-devel"
- name: "libxml2-devel"
- name: "libcurl-devel"
- name: "java-devel"
- name: "unixODBC"
- name: "unixODBC-devel"
- name: "OpenIPMI-devel"
- name: "curl-devel"
- name: "libssh2-devel"
- name: "MySQL-python"
- name: Check whether jdk-7u79-linux-x64.tar.gz file exist
shell: ls /tmp/jdk-7u79-linux-x64.tar.gz
register: result
ignore_errors: True
- name: download jdk-7u79 source package
get_url: url="http://note.youdao.com/yws/api/personal/file/B6CC3F0E2AED4E51B16CE7FAF865A81B?method=download&shareKey=8bce5fcc4e54f5295476a3be85eb16d5" dest="/tmp"
when: result|failed
- name: Check whether '/usr/local/jdk1.7.0_79' file exist
shell: ls /usr/local/jdk1.7.0_79
register: result
ignore_errors: True
- name: unarchive jdk-7u79 source package
unarchive: src="/tmp/jdk-7u79-linux-x64.tar.gz" dest="/usr/local/" remote_src=yes
when: result|failed
- name: Configure java variable
shell: echo 'export JAVA_HOME=/usr/local/jdk1.7.0_79' >> /etc/profile && echo 'export PATH=${JAVA_HOME}/bin/:$PATH' >> /etc/profile && source /etc/profile
when: result|failed
- name: unarchive zabbix-3.2.4 source package
unarchive: src="files/zabbix-3.2.4.tar.gz" dest="/tmp" remote_src=no
- name: Install zabbix source package
shell: cd /tmp/zabbix-3.2.4 && ./configure --prefix={{zabbix_install_path}} --enable-server --enable-agent --with-mysql --with-net-snmp --with-libcurl --with-libxml2 --enable-java && make && make install
- name: Check whether zabbix users exist
shell: id zabbix
register: result
ignore_errors: True
- name: The zabbix user does not exist, so we need to create it.
user: name=zabbix createhome=yes home=/home/zabbix shell=/sbin/nologin state=present
when: result|failed
- name: Replace some files
lineinfile: dest="{{item.dest}}" regexp="{{item.reg}}" line="{{item.line}}" backrefs=yes
with_items:
- dest: '/etc/php.ini'
reg: '^max_execution_time'
line: 'max_execution_time = 300'
- dest: '/etc/php.ini'
reg: '^post_max_size'
line: 'post_max_size = 16M'
- dest: '/etc/php.ini'
reg: '^max_input_time'
line: 'max_input_time = 300'
- dest: '/etc/php.ini'
reg: '^;date.timezone'
line: 'date.timezone = "Asia/Shanghai"'
- dest: '/etc/php-fpm.conf'
reg: '^;pid ='
line: 'pid = /var/run/php-fpm/php-fpm.pid'
- dest: '/etc/php-fpm.d/www.conf'
reg: '^;pm.min_spare_servers'
line: 'pm.min_spare_servers = 5'
- dest: '/etc/php-fpm.d/www.conf'
reg: '^;pm.max_spare_servers'
line: 'pm.max_spare_servers = 35'
- dest: '/etc/php-fpm.d/www.conf'
reg: '^;^pm.start_servers'
line: 'pm.start_servers = 5'
- name: Copy authorization.sql file
template: src="{{item.name}}" dest="/tmp/"
with_items:
- name: "files/authorization.sql"
- name: "files/initialize.sql"
- name: Set up mysql database authorization
shell: mysql -u{{mysql_user}} -p{{mysql_pass}} -P{{mysql_port}} < "/tmp/authorization.sql"
- name: Authorize Zabbix users
shell: mysql -u{{mysql_user}} -p{{mysql_pass}} -P{{mysql_port}} < "/tmp/initialize.sql"
- name: Initialize the zabbix database
shell: cd /tmp/zabbix-3.2.4 && mysql -u{{mysql_zbx_user}} -p{{mysql_zbx_pass}} -P{{mysql_port}} {{mysql_zbx_db}} < database/mysql/schema.sql && mysql -u{{mysql_zbx_user}} -p{{mysql_zbx_pass}} -P{{mysql_port}} {{mysql_zbx_db}} < database/mysql/images.sql && mysql -u{{mysql_zbx_user}} -p{{mysql_zbx_pass}} -P{{mysql_port}} {{mysql_zbx_db}} < database/mysql/data.sql
- name: Configure zabbix
shell: cd /tmp/zabbix-3.2.4 && cp misc/init.d/fedora/core/zabbix_* /etc/init.d/ && chmod 775 /etc/init.d/zabbix_*
- name: Copy zabbix configure
template: src="{{item.src}}" dest="{{item.dest}}"
with_items:
- src: "templates/zabbix_server.conf"
dest: "{{zabbix_install_path}}/etc/zabbix_server.conf"
- src: "templates/zabbix_agentd.conf"
dest: "{{zabbix_install_path}}/etc/zabbix_agentd.conf"
- name: Create some directory
file: path="{{item.path}}" state=directory mode=0755
with_items:
- path: "{{nginx_install_path}}/html/zabbix"
- path: "{{zabbix_log_path}}"
- path: "{{nginx_log_path}}/zabbix"
- name: Copy zabbix pages
shell: cp -rpf /tmp/zabbix-3.2.4/frontends/php/* {{nginx_install_path}}/html/zabbix/ && chown zabbix:zabbix {{nginx_install_path}}/html/zabbix/
- name: Modify file permissions
file: path="{{nginx_install_path}}/html/zabbix/conf" state=directory owner=zabbix group=zabbix mode=0777
- name: Command Links
file: src="{{item.src}}" dest="{{item.dest}}" state=link remote_src=True
with_items:
- src: "{{zabbix_install_path}}/sbin/zabbix_server"
dest: "/usr/local/sbin/zabbix_server"
- src: "{{zabbix_install_path}}/sbin/zabbix_agentd"
dest: "/usr/local/sbin/zabbix_agentd"
- name: touch zabbix log file
file: path={{item.path}} state=touch owner=zabbix group=zabbix mode=0755
with_items:
- path: "{{zabbix_log_path}}/zabbix_server.log"
- path: "{{zabbix_log_path}}/zabbix_agentd.log"
- name: Configure nginx for Zabbix
template: src="templates/zabbix.conf" dest="{{nginx_install_path}}/conf/vhosts/zabbix.conf" mode=0644 owner=nginx group=nginx
- name: Check whether fastcgi.conf file exist
shell: ls {{nginx_install_path}}/conf/vhosts/fastcgi.conf
register: result
ignore_errors: True
- name: Copy fastcgi.conf file
copy: src="templates/fastcgi.conf.j2" dest="{{nginx_install_path}}/conf/vhosts/fastcgi.conf" mode=0644 owner=nginx group=nginx mode=0644
when: result|failed
- name: Add zabbix to chkconfig
shell: chkconfig --add zabbix_server && chkconfig --add zabbix_agentd
notify: restart zabbix server
[root@Ansible ansible]#
1
2
3
4
5
6
7
8
9
10
#handlers
[root@Ansible ansible]# cat roles/source_zabbix_server/handlers/main.yml
- name: restart zabbix server
service: name={{item.name}} state=restarted enabled=yes
with_items:
- name: "zabbix_server"
- name: "zabbix_agentd"
- name: "php-fpm"
- name: "nginx"
[root@Ansible ansible]#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#变量
[root@Ansible ansible]# cat roles/source_zabbix_server/vars/main.yml
mysql_user: root
mysql_host: 192.168.31.110
mysql_pass: 123456
mysql_zbx_db: zabbix
mysql_zbx_user: zabbix
mysql_zbx_pass: zabbix
mysql_zbx_hosts: 192.168.31.%
mysql_port: 3306
zabbix_install_path: /usr/local/zabbix
zabbix_log_path: /var/log/zabbix
zabbix_server_ip: 192.168.31.110
nginx_install_path: /usr/local/nginx-1.11.7
nginx_log_path: /var/log/nginx
[root@Ansible ansible]#
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#执行过程
[root@Ansible ~]# ansible-playbook /etc/ansible/site.yml
PLAY [test_hosts] **************************************************************
TASK [setup] *******************************************************************
ok: [192.168.31.110]
TASK [source_zabbix_server : disabled iptables] ********************************
ok: [192.168.31.110]
TASK [source_zabbix_server : Set the selinux value to Permissive] **************
changed: [192.168.31.110]
TASK [source_zabbix_server : Install libselinux-python package] ****************
ok: [192.168.31.110]
TASK [source_zabbix_server : Check whether yum.repo file exist] ****************
changed: [192.168.31.110]
TASK [source_zabbix_server : Copy yum.repo file] *******************************
skipping: [192.168.31.110]
TASK [source_zabbix_server : Rebuild the yum cache] ****************************
skipping: [192.168.31.110]
TASK [source_zabbix_server : Install same devel packages] **********************
changed: [192.168.31.110] => (item={u'name': u'net-snmp'})
changed: [192.168.31.110] => (item={u'name': u'net-snmp-devel'})
changed: [192.168.31.110] => (item={u'name': u'libxml2-devel'})
changed: [192.168.31.110] => (item={u'name': u'libcurl-devel'})
changed: [192.168.31.110] => (item={u'name': u'java-devel'})
ok: [192.168.31.110] => (item={u'name': u'unixODBC'})
changed: [192.168.31.110] => (item={u'name': u'unixODBC-devel'})
changed: [192.168.31.110] => (item={u'name': u'OpenIPMI-devel'})
ok: [192.168.31.110] => (item={u'name': u'curl-devel'})
changed: [192.168.31.110] => (item={u'name': u'libssh2-devel'})
changed: [192.168.31.110] => (item={u'name': u'MySQL-python'})
TASK [source_zabbix_server : Check whether jdk-7u79-linux-x64.tar.gz file exist] ***
fatal: [192.168.31.110]: FAILED! => {"changed": true, "cmd": "ls /tmp/jdk-7u79-linux-x64.tar.gz", "delta": "0:00:00.006056", "end": "2017-03-21 10:42:08.254025", "failed": true, "rc": 2, "start": "2017-03-21 10:42:08.247969", "stderr": "ls: cannot access /tmp/jdk-7u79-linux-x64.tar.gz: No such file or directory", "stdout": "", "stdout_lines": [], "warnings": []}
...ignoring
TASK [source_zabbix_server : download jdk-7u79 source package] *****************
changed: [192.168.31.110]
TASK [source_zabbix_server : Check whether '/usr/local/jdk1.7.0_79' file exist]
fatal: [192.168.31.110]: FAILED! => {"changed": true, "cmd": "ls /usr/local/jdk1.7.0_79", "delta": "0:00:00.004081", "end": "2017-03-21 10:45:48.414682", "failed": true, "rc": 2, "start": "2017-03-21 10:45:48.410601", "stderr": "ls: cannot access /usr/local/jdk1.7.0_79: No such file or directory", "stdout": "", "stdout_lines": [], "warnings": []}
...ignoring
TASK [source_zabbix_server : unarchive jdk-7u79 source package] ****************
changed: [192.168.31.110]
TASK [source_zabbix_server : Configure java variable] **************************
changed: [192.168.31.110]
TASK [source_zabbix_server : unarchive zabbix-3.2.4 source package] ************
changed: [192.168.31.110]
TASK [source_zabbix_server : Install zabbix source package] ********************
changed: [192.168.31.110]
TASK [source_zabbix_server : Check whether zabbix users exist] *****************
fatal: [192.168.31.110]: FAILED! => {"changed": true, "cmd": "id zabbix", "delta": "0:00:00.004139", "end": "2017-03-21 10:46:58.885950", "failed": true, "rc": 1, "start": "2017-03-21 10:46:58.881811", "stderr": "id: zabbix: No such user", "stdout": "", "stdout_lines": [], "warnings": []}
...ignoring
TASK [source_zabbix_server : The zabbix user does not exist, so we need to create it.] ***
changed: [192.168.31.110]
TASK [source_zabbix_server : Replace some files] *******************************
changed: [192.168.31.110] => (item={u'dest': u'/etc/php.ini', u'line': u'max_execution_time = 300', u'reg': u'^max_execution_time'})
changed: [192.168.31.110] => (item={u'dest': u'/etc/php.ini', u'line': u'post_max_size = 16M', u'reg': u'^post_max_size'})
changed: [192.168.31.110] => (item={u'dest': u'/etc/php.ini', u'line': u'max_input_time = 300', u'reg': u'^max_input_time'})
changed: [192.168.31.110] => (item={u'dest': u'/etc/php.ini', u'line': u'date.timezone = "Asia/Shanghai"', u'reg': u'^;date.timezone'})
ok: [192.168.31.110] => (item={u'dest': u'/etc/php-fpm.conf', u'line': u'pid = /var/run/php-fpm/php-fpm.pid', u'reg': u'^;pid ='})
ok: [192.168.31.110] => (item={u'dest': u'/etc/php-fpm.d/www.conf', u'line': u'pm.min_spare_servers = 5', u'reg': u'^;pm.min_spare_servers'})
ok: [192.168.31.110] => (item={u'dest': u'/etc/php-fpm.d/www.conf', u'line': u'pm.max_spare_servers = 35', u'reg': u'^;pm.max_spare_servers'})
ok: [192.168.31.110] => (item={u'dest': u'/etc/php-fpm.d/www.conf', u'line': u'pm.start_servers = 5', u'reg': u'^;^pm.start_servers'})
TASK [source_zabbix_server : Copy authorization.sql file] **********************
changed: [192.168.31.110] => (item={u'name': u'files/authorization.sql'})
changed: [192.168.31.110] => (item={u'name': u'files/initialize.sql'})
TASK [source_zabbix_server : Set up mysql database authorization] **************
changed: [192.168.31.110]
TASK [source_zabbix_server : Authorize Zabbix users] ***************************
changed: [192.168.31.110]
TASK [source_zabbix_server : Initialize the zabbix database] *******************
changed: [192.168.31.110]
TASK [source_zabbix_server : Configure zabbix] *********************************
changed: [192.168.31.110]
TASK [source_zabbix_server : Copy zabbix configure] ****************************
changed: [192.168.31.110] => (item={u'dest': u'/usr/local/zabbix/etc/zabbix_server.conf', u'src': u'templates/zabbix_server.conf'})
changed: [192.168.31.110] => (item={u'dest': u'/usr/local/zabbix/etc/zabbix_agentd.conf', u'src': u'templates/zabbix_agentd.conf'})
TASK [source_zabbix_server : Create some directory] ****************************
changed: [192.168.31.110] => (item={u'path': u'/usr/local/nginx-1.11.7/html/zabbix'})
changed: [192.168.31.110] => (item={u'path': u'/var/log/zabbix'})
changed: [192.168.31.110] => (item={u'path': u'/var/log/nginx/zabbix'})
TASK [source_zabbix_server : Copy zabbix pages] ********************************
changed: [192.168.31.110]
TASK [source_zabbix_server : Modify file permissions] **************************
changed: [192.168.31.110]
TASK [source_zabbix_server : Command Links] ************************************
changed: [192.168.31.110] => (item={u'dest': u'/usr/local/sbin/zabbix_server', u'src': u'/usr/local/zabbix/sbin/zabbix_server'})
changed: [192.168.31.110] => (item={u'dest': u'/usr/local/sbin/zabbix_agentd', u'src': u'/usr/local/zabbix/sbin/zabbix_agentd'})
TASK [source_zabbix_server : touch zabbix log file] ****************************
changed: [192.168.31.110] => (item={u'path': u'/var/log/zabbix/zabbix_server.log'})
changed: [192.168.31.110] => (item={u'path': u'/var/log/zabbix/zabbix_agentd.log'})
TASK [source_zabbix_server : Configure nginx for Zabbix] ***********************
changed: [192.168.31.110]
TASK [source_zabbix_server : Check whether fastcgi.conf file exist] ************
changed: [192.168.31.110]
TASK [source_zabbix_server : Copy fastcgi.conf file] ***************************
skipping: [192.168.31.110]
TASK [source_zabbix_server : Add zabbix to chkconfig] **************************
changed: [192.168.31.110]
RUNNING HANDLER [source_zabbix_server : restart zabbix server] *****************
changed: [192.168.31.110] => (item={u'name': u'zabbix_server'})
changed: [192.168.31.110] => (item={u'name': u'zabbix_agentd'})
changed: [192.168.31.110] => (item={u'name': u'php-fpm'})
changed: [192.168.31.110] => (item={u'name': u'nginx'})
PLAY RECAP *********************************************************************
192.168.31.110 : ok=31 changed=28 unreachable=0 failed=0
[root@Ansible ~]#

image

1
2
3
4
5
6
7
[root@Ansible ~]# ansible test_hosts -m shell -a 'ps -ef | grep zabbix | grep -v grep'
192.168.31.110 | SUCCESS | rc=0 >>
zabbix 11586 1 0 18:01 ? 00:00:00 /usr/local/sbin/zabbix_server
zabbix 11593 11586 0 18:01 ? 00:00:00 /usr/local/sbin/zabbix_server: configuration syncer [synced configuration in 0.006998 sec, idle 60 sec]
zabbix 11594 11586 0 18:01 ? 00:00:00 /usr/local/sbin/zabbix_server: db watchdog [synced alerts config in 0.001220 sec, idle 60 sec]
zabbix 11595 11586 0 18:01 ? 00:00:00 /usr/local/sbin/zabbix_server: poller #1 [got 0 values in 0.000101 sec, idle 5 sec]
···

image
image

Ansible之源码安装Zabbix-agent

  • Zabbix_Agent结构目录
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#结构目录
[root@Ansible ~]# tree /etc/ansible/
/etc/ansible/
├── ansible.cfg
├── hosts
├── roles
│   └── source_zabbix_agent
│   ├── files
│   │   ├── yum.repo
│   │   └── zabbix-3.2.4.tar.gz
│   ├── handlers
│   │   └── main.yml
│   ├── tasks
│   │   └── main.yml
│   ├── templates
│   │   └── zabbix_agentd.conf
│   └── vars
│   └── main.yml
└── site.yml
7 directories, 9 files
[root@Ansible ~]#

环境:
   ansible 2.2.1.0
   Centos 6.6
   zabbix-3.2.4

1
2
3
4
5
6
7
#入口文件
[root@Ansible ansible]# cat site.yml
- hosts: test_hosts2
user: root
roles:
- source_zabbix_agent
[root@Ansible ansible]#
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#tasks
[root@Ansible ansible]# cat roles/source_zabbix_agent/tasks/main.yml
- name: Install libselinux-python package
yum: name=libselinux-python state=installed
- name: Check whether yum.repo file exist
shell: ls /etc/yum.repos.d/yum.repo
register: result
ignore_errors: True
- name: Copy yum.repo file
copy: src="files/yum.repo" dest="/etc/yum.repos.d/yum.repo" mode=0644 owner=root group=root
when: result|failed
- name: Rebuild the yum cache
shell: yum clean all && yum makecache
when: result|failed
- name: Install same devel packages
yum: name="{{item.name}}"
with_items:
- name: "net-snmp"
- name: "net-snmp-devel"
- name: "libxml2-devel"
- name: "libcurl-devel"
- name: "java-devel"
- name: "unixODBC"
- name: "unixODBC-devel"
- name: "OpenIPMI-devel"
- name: "curl-devel"
- name: "libssh2-devel"
- name: "MySQL-python"
- name: unarchive zabbix-3.2.4 source package
unarchive: src="files/zabbix-3.2.4.tar.gz" dest="/tmp" remote_src=no
- name: Install zabbix source package
shell: cd /tmp/zabbix-3.2.4 && ./configure --prefix={{zabbix_install_path}} --enable-agent && make && make install
- name: Configure zabbix
copy: src="/tmp/zabbix-3.2.4/misc/init.d/fedora/core/zabbix_agentd" dest="/etc/init.d/zabbix_agentd" mode=0755 remote_src=True
- name: Check whether zabbix users exist
shell: id zabbix
register: result
ignore_errors: True
- name: The zabbix user does not exist, so we need to create it.
user: name=zabbix createhome=yes home=/home/zabbix shell=/sbin/nologin state=present
when: result|failed
- name: Configure zabbix agent
template: src="templates/zabbix_agentd.conf" dest="{{zabbix_install_path}}/etc/zabbix_agentd.conf"
- name: Command Links
file: src="{{zabbix_install_path}}/sbin/zabbix_agentd" dest="/usr/local/sbin/zabbix_agentd" state=link remote_src=True
- name: touch zabbix log file
file: path={{item.path}} state="{{item.state|default("touch")}}" owner=zabbix group=zabbix mode=0755
with_items:
- path: "{{zabbix_log_path}}"
state: "directory"
- path: "{{zabbix_log_path}}/zabbix_agentd.log"
- name: Add zabbix_agent to chkconfig
shell: chkconfig --add zabbix_agentd
notify: restart zabbix agent
[root@Ansible ansible]#
1
2
3
4
5
#handlers
[root@Ansible ansible]# cat roles/source_zabbix_agent/handlers/main.yml
- name: restart zabbix agent
service: name="zabbix_agentd" state=restarted enabled=yes
[root@Ansible ansible]#
1
2
3
4
5
6
#变量
[root@Ansible ansible]# cat roles/source_zabbix_agent/vars/main.yml
zabbix_install_path: /usr/local/zabbix
zabbix_log_path: /var/log/zabbix
zabbix_server_ip: 192.168.31.110
[root@Ansible ansible]#
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#执行过程
[root@Ansible ~]# ansible-playbook /etc/ansible/site.yml
PLAY [test_hosts2] *************************************************************
TASK [setup] *******************************************************************
ok: [192.168.31.120]
TASK [source_zabbix_agent : Install libselinux-python package] *****************
changed: [192.168.31.120]
TASK [source_zabbix_agent : Check whether yum.repo file exist] *****************
fatal: [192.168.31.120]: FAILED! => {"changed": true, "cmd": "ls /etc/yum.repos.d/yum.repo", "delta": "0:00:00.004811", "end": "2017-03-21 18:22:42.833895", "failed": true, "rc": 2, "start": "2017-03-21 18:22:42.829084", "stderr": "ls: cannot access /etc/yum.repos.d/yum.repo: No such file or directory", "stdout": "", "stdout_lines": [], "warnings": []}
...ignoring
TASK [source_zabbix_agent : Copy yum.repo file] ********************************
changed: [192.168.31.120]
TASK [source_zabbix_agent : Rebuild the yum cache] *****************************
changed: [192.168.31.120]
[WARNING]: Consider using yum module rather than running yum
TASK [source_zabbix_agent : Install same devel packages] ***********************
changed: [192.168.31.120] => (item={u'name': u'net-snmp'})
changed: [192.168.31.120] => (item={u'name': u'net-snmp-devel'})
changed: [192.168.31.120] => (item={u'name': u'libxml2-devel'})
changed: [192.168.31.120] => (item={u'name': u'libcurl-devel'})
changed: [192.168.31.120] => (item={u'name': u'java-devel'})
changed: [192.168.31.120] => (item={u'name': u'unixODBC'})
changed: [192.168.31.120] => (item={u'name': u'unixODBC-devel'})
changed: [192.168.31.120] => (item={u'name': u'OpenIPMI-devel'})
ok: [192.168.31.120] => (item={u'name': u'curl-devel'})
changed: [192.168.31.120] => (item={u'name': u'libssh2-devel'})
changed: [192.168.31.120] => (item={u'name': u'MySQL-python'})
TASK [source_zabbix_agent : unarchive zabbix-3.2.4 source package] *************
changed: [192.168.31.120]
TASK [source_zabbix_agent : Install zabbix source package] *********************
changed: [192.168.31.120]
TASK [source_zabbix_agent : Configure zabbix] **********************************
changed: [192.168.31.120]
TASK [source_zabbix_agent : Check whether zabbix users exist] ******************
fatal: [192.168.31.120]: FAILED! => {"changed": true, "cmd": "id zabbix", "delta": "0:00:00.005663", "end": "2017-03-21 18:35:09.861371", "failed": true, "rc": 1, "start": "2017-03-21 18:35:09.855708", "stderr": "id: zabbix: No such user", "stdout": "", "stdout_lines": [], "warnings": []}
...ignoring
TASK [source_zabbix_agent : The zabbix user does not exist, so we need to create it.] ***
changed: [192.168.31.120]
TASK [source_zabbix_agent : Configure zabbix agent] ****************************
changed: [192.168.31.120]
TASK [source_zabbix_agent : Command Links] *************************************
changed: [192.168.31.120]
TASK [source_zabbix_agent : touch zabbix log file] *****************************
changed: [192.168.31.120] => (item={u'path': u'/var/log/zabbix', u'state': u'directory'})
changed: [192.168.31.120] => (item={u'path': u'/var/log/zabbix/zabbix_agentd.log'})
TASK [source_zabbix_agent : Add zabbix_agent to chkconfig] *********************
changed: [192.168.31.120]
RUNNING HANDLER [source_zabbix_agent : restart zabbix agent] *******************
changed: [192.168.31.120]
PLAY RECAP *********************************************************************
192.168.31.120 : ok=16 changed=15 unreachable=0 failed=0
[root@Ansible ~]#
1
2
3
4
5
6
7
8
9
10
[root@Ansible ~]# ansible test_hosts2 -m shell -a 'ps -ef | grep zabbix_agentd | grep -v grep'
192.168.31.120 | SUCCESS | rc=0 >>
zabbix 9164 1 0 18:35 ? 00:00:00 /usr/local/sbin/zabbix_agentd
zabbix 9167 9164 0 18:35 ? 00:00:00 /usr/local/sbin/zabbix_agentd: collector [idle 1 sec]
zabbix 9168 9164 0 18:35 ? 00:00:00 /usr/local/sbin/zabbix_agentd: listener #1 [waiting for connection]
zabbix 9169 9164 0 18:35 ? 00:00:00 /usr/local/sbin/zabbix_agentd: listener #2 [waiting for connection]
zabbix 9170 9164 0 18:35 ? 00:00:00 /usr/local/sbin/zabbix_agentd: listener #3 [waiting for connection]
zabbix 9171 9164 0 18:35 ? 00:00:00 /usr/local/sbin/zabbix_agentd: active checks #1 [idle 1 sec]
[root@Ansible ~]#

image

附件:
ansible_install_zabbix_server3.2.tar.gz
ansible_install_zabbix_agent3.2.tar.gz


本文出自”Jack Wang Blog”:http://www.yfshare.vip/2017/03/20/Ansible-Playbooks之安装Zabbix/