Skip to content

Instantly share code, notes, and snippets.

@sky-joker
Created February 2, 2020 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sky-joker/accd93c4e3d906eeba43fefab9c11557 to your computer and use it in GitHub Desktop.
Save sky-joker/accd93c4e3d906eeba43fefab9c11557 to your computer and use it in GitHub Desktop.
Zabbix Serverコンテナを複数のバージョン指定で起動させるPlaybook
---
- name: zabbix containers provisione playbook
hosts: localhost
gather_facts: no
vars:
state: present
#state: absent
subnet: "192.168.100.0/24"
zabbix_version:
- "3.0.29"
- "3.2.11"
- "3.4.15"
- "4.0.0"
- "4.0.17"
- "4.2.8"
- "4.4.5"
- "trunk"
bind_port: "80"
tasks:
- name: set services variable
set_fact:
services: >-
{{ services | default({})
| combine(data)
}}
vars:
port: >-
{%- set version = [] -%}
{%- if item.split('.') | join('') | int -%}
{%- set _ = version.append(item.split('.') | join('') | string) -%}
{%- else -%}
{%- set _ = version.append('500') -%}
{%- endif -%}
{{ version | first }}
data: >-
{%- set zabbix_db_name = "zabbix" + item | string + "-db" -%}
{%- set zabbix_server_name = "zabbix" + item | string + "-server" -%}
{%- set zabbix_web_name = "zabbix" + item | string + "-web" -%}
{%- set zabbix_containers = {
zabbix_db_name: {
"image": "mysql:5.7",
"environment": {
"MYSQL_DATABASE": "zabbix",
"MYSQL_USER": "zabbix",
"MYSQL_PASSWORD": "zabbix",
"MYSQL_ROOT_PASSWORD": "zabbix"
},
"networks": {
"zabbix-nw": {
"ipv4_address": ""
}
}
},
zabbix_server_name: {
"image": "zabbix/zabbix-server-mysql:centos-" + item,
"environment": {
"DB_SERVER_HOST": "",
"MYSQL_USER": "zabbix",
"MYSQL_PASSWORD": "zabbix",
"MYSQL_DATABASE": "zabbix",
"MYSQL_ROOT_PASSWORD": "zabbix"
},
"depends_on": [
zabbix_db_name
],
"networks": {
"zabbix-nw": {
"ipv4_address": ""
}
}
},
zabbix_web_name: {
"image": "zabbix/zabbix-web-apache-mysql:centos-" + item,
"environment": {
"DB_SERVER_HOST": "",
"MYSQL_USER": "zabbix",
"MYSQL_PASSWORD": "zabbix",
"MYSQL_DATABASE": "zabbix",
"MYSQL_ROOT_PASSWORD": "zabbix",
"MYSQL_ROOT_PASSWORD": "zabbix",
"ZBX_SERVER_HOST": "",
"PHP_TZ": "Asia/Tokyo"
},
"depends_on": [
zabbix_db_name,
zabbix_server_name
],
"networks": {
"zabbix-nw": {
"ipv4_address": ""
}
},
"ports": [
"5" + port + ":" + bind_port if port | length == 4 else "5" + port + "0" + ":" + bind_port
]
}
}
-%}
{{ zabbix_containers }}
loop: "{{ zabbix_version }}"
- name: update services variable
set_fact:
services: >-
{%- set zabbix_ip_dict = {} -%}
{%- set base_ip = {'base_ip': (( subnet.split('/') | first | ipaddr('int') + 1 )) | ipaddr } -%}
{%- for key in services -%}
{%- set zabbix_ip = (( base_ip['base_ip'] | ipaddr('int') + 1 )) | ipaddr -%}
{%- set _ = base_ip.update({'base_ip': zabbix_ip }) -%}
{%- set _ = zabbix_ip_dict.update({
key: zabbix_ip
})
-%}
{%- endfor -%}
{%- for key in services -%}
{%- set zabbix_version = key | regex_search('zabbix(.*)-.*', '\\1') | first -%}
{%- if key | regex_search('server|web') -%}
{%- set _ = services[key]['environment'].update({'DB_SERVER_HOST': zabbix_ip_dict['zabbix' + zabbix_version + '-db']}) -%}
{%- endif -%}
{%- if key | regex_search('web') -%}
{%- set _ = services[key]['environment'].update({'ZBX_SERVER_HOST': zabbix_ip_dict['zabbix' + zabbix_version + '-server']}) -%}
{%- endif -%}
{%- set _ = services[key]['networks']['zabbix-nw'].update({'ipv4_address': zabbix_ip_dict[key]}) -%}
{%- endfor -%}
{{ services }}
- name: zabbix container servers operation
docker_compose:
project_name: zabbix servers
definition:
version: '3.7'
networks:
zabbix-nw:
name: zabbix-nw
ipam:
driver: default
config:
- subnet: "{{ subnet }}"
services: "{{ services }}"
timeout: 120
state: "{{ state }}"
- when:
- state == "present"
block:
- name: check login to zabbix
uri:
url: >-
http://127.0.0.1:{{ "5" + port if port | length == 4 else "5" + port + "0" }}/api_jsonrpc.php
method: POST
body:
jsonrpc: "2.0"
method: "user.login"
params:
user: "admin"
password: "zabbix"
id: "1"
body_format: json
status_code: 200
vars:
port: >-
{%- set version = [] -%}
{%- if item.split('.') | join('') | int -%}
{%- set _ = version.append(item.split('.') | join('') | string) -%}
{%- else -%}
{%- set _ = version.append('500') -%}
{%- endif -%}
{{ version | first }}
retries: 60
delay: 5
until: result is defined and 'json' in result and 'result' in result.json
register: result
loop: "{{ zabbix_version }}"
- name: set zabbix locale
shell: |
for c in $( docker ps --format '{% raw %}{{.Names}}{% endraw %}' | grep web ) ; do
docker exec $c localedef -f UTF-8 -i {{ item }} {{ item }}
done
loop:
- zh_CN
- cs_CZ
- fr_FR
- he_IL
- it_IT
- ko_KR
- ja_JP
- nb_NO
- pl_PL
- pt_BR
- pt_PT
- ru_RU
- sk_SK
- tr_TR
- uk_UA
- name: set bind_port_result variable
set_fact:
bind_port_result: >-
{{ bind_port_result | default([])
+ [item.key + ' : ' + item.value.ports | first | string]
}}
with_dict: "{{ services }}"
when:
- "'ports' in services[item.key]"
- name: bind ports for zabbix container
debug: var=bind_port_result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment