본문 바로가기

DevOps/Zabbix

Asible을 통한 자빅스 설치

Ansible  설치

# 최신 패키지 업데이트
sudo yum update -y

# Python 및 pip 설치
sudo yum install -y python3 python3-pip

# Ansible 설치
sudo pip3 install ansible

 

 

yml 작성

- name: Install and configure Zabbix server with Docker
  hosts: localhost
  connection: local
  become: yes
  vars:
    db_name: zabbix
    db_user: zabbix_user
    db_password: soobeen0624!
    mysql_root_password: rootpassword
    zabbix_server_version: "alpine-6.4-latest"
    zabbix_web_version: "alpine-6.4-latest"
    zabbix_agent_version: "latest"
    mysql_version: "8.0"
    timezone: "Asia/Seoul"
    zabbix_network: "zabbix-network"

  tasks:
    - name: Create Docker network for Zabbix
      docker_network:
        name: "{{ zabbix_network }}"

    - name: Install Docker and Docker SDK for Python
      package:
        name:
          - docker
          - python3-pip
        state: present

    - name: Install Docker Python SDK
      pip:
        name: docker
        state: present

    - name: Start and enable Docker service
      service:
        name: docker
        state: started
        enabled: yes

    - name: Run MySQL container
      community.docker.docker_container:
        name: mysql-server
        image: mysql:{{ mysql_version }}
        env:
          MYSQL_ROOT_PASSWORD: "{{ mysql_root_password }}"
          MYSQL_DATABASE: "{{ db_name }}"
          MYSQL_USER: "{{ db_user }}"
          MYSQL_PASSWORD: "{{ db_password }}"
        command: >
          --character-set-server=utf8mb4
          --collation-server=utf8mb4_bin
          --default-authentication-plugin=caching_sha2_password
          --log-bin-trust-function-creators=1
        volumes:
          - /var/run/mysqld:/var/run/mysqld
        state: started
        privileged: true
                                                                                                                                                                                                                                        1,1           Top
        state: started
        privileged: true
        restart_policy: always
        published_ports:
          - "3306:3306"
        networks:
          - name: "{{ zabbix_network }}"

    - name: Wait for MySQL to be ready
      wait_for:
        host: "127.0.0.1"
        port: 3306
        delay: 10
        timeout: 60
      delegate_to: localhost

    - name: Insert initial data into Zabbix users table
      shell: |
        docker exec -i mysql-server mysql --user=root --password={{ mysql_root_password }} -e "
        USE {{ db_name }};
        INSERT INTO users (userid, username, name, surname, passwd, lang, theme, autologout, refresh, rows_per_page, roleid, attempt_failed, attempt_clock, timezone) VALUES (1, 'Admin', 'Administrator', 'User', MD5('zabbix'), 'ko_KR', 'default', '15m', '30s', 50, 1, 0, 0, 'default');"
      args:
        executable: /bin/bash
      ignore_errors: yes

    - name: Run Zabbix Server container
      docker_container:
        name: zabbix-server
        image: zabbix/zabbix-server-mysql:{{ zabbix_server_version }}
        env:
          DB_SERVER_HOST: mysql-server
          MYSQL_DATABASE: "{{ db_name }}"
          MYSQL_USER: "{{ db_user }}"
          MYSQL_PASSWORD: "{{ db_password }}"
          TZ: "{{ timezone }}"
        state: started
        privileged: true
        restart_policy: always
        networks:
          - name: "{{ zabbix_network }}"
        published_ports:
          - "10051:10051"

    - name: Run Zabbix Web container
      docker_container:
        name: zabbix-web
        image: zabbix/zabbix-web-nginx-mysql:{{ zabbix_web_version }}
        env:
          ZBX_SERVER_HOST: zabbix-server
          DB_SERVER_HOST: mysql-server
          MYSQL_DATABASE: "{{ db_name }}"
          MYSQL_USER: "{{ db_user }}"
          MYSQL_PASSWORD: "{{ db_password }}"
          PHP_TZ: "{{ timezone }}"
        state: started
        restart_policy: always
                                                                                                                                                                                                                                        61,9          42%
        state: started
        restart_policy: always
        networks:
          - name: "{{ zabbix_network }}"
        published_ports:
          - "8080:8080"
          - "8443:8443"

    - name: Run Zabbix Agent container
      docker_container:
        name: zabbix-agent
        image: zabbix/zabbix-agent2:{{ zabbix_agent_version }}
        env:
          ZBX_SERVER_HOST: zabbix-server
          ZBX_HOSTNAME: soobeen
        state: started
        restart_policy: always
        networks:
          - name: "{{ zabbix_network }}"
        published_ports:
          - "10050:10050"
        volumes:
          - "/var/run/docker.sock:/var/run/docker.sock"
        privileged: true

    - name: Get Zabbix Agent Docker IP
      command: docker inspect -f "{% raw %}{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}{% endraw %}" zabbix-agent
      register: zabbix_agent_ip

    - name: Get Mysql Docker IP
      command: docker inspect -f "{% raw %}{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}{% endraw %}" mysql-server
      register: mysql_ip

    - name: Set credentials to access Zabbix Server API, Create Host
      ansible.builtin.set_fact:
        ansible_user: Admin
        ansible_httpapi_pass: zabbix

    - name: Create a new host or update it
      vars:
        ansible_network_os: community.zabbix.zabbix
        ansible_connection: ansible.netcommon.httpapi
        ansible_httpapi_port: 8080
        ansible_httpapi_use_ssl: false
        ansible_httpapi_validate_certs: false
        ansible_zabbix_url_path: ""
      become: false
      community.zabbix.zabbix_host:
        host_name: soobeen
        host_groups:
          - Zabbix servers
        link_templates:
          - Docker by Zabbix agent 2
          - MySQL by Zabbix agent 2
          - Linux by Zabbix agent
        interfaces:
          - type: 1
                                                                                                                                                                                                                                        115,11        85%
          - name: "{{ zabbix_network }}"
        published_ports:
          - "10050:10050"
        volumes:
          - "/var/run/docker.sock:/var/run/docker.sock"
        privileged: true

    - name: Get Zabbix Agent Docker IP
      command: docker inspect -f "{% raw %}{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}{% endraw %}" zabbix-agent
      register: zabbix_agent_ip

    - name: Get Mysql Docker IP
      command: docker inspect -f "{% raw %}{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}{% endraw %}" mysql-server
      register: mysql_ip

    - name: Set credentials to access Zabbix Server API, Create Host
      ansible.builtin.set_fact:
        ansible_user: Admin
        ansible_httpapi_pass: zabbix

    - name: Create a new host or update it
      vars:
        ansible_network_os: community.zabbix.zabbix
        ansible_connection: ansible.netcommon.httpapi
        ansible_httpapi_port: 8080
        ansible_httpapi_use_ssl: false
        ansible_httpapi_validate_certs: false
        ansible_zabbix_url_path: ""
      become: false
      community.zabbix.zabbix_host:
        host_name: soobeen
        host_groups:
          - Zabbix servers
        link_templates:
          - Docker by Zabbix agent 2
          - MySQL by Zabbix agent 2
          - Linux by Zabbix agent
        interfaces:
          - type: 1
            main: 1
            useip: 1
            ip: "{{ zabbix_agent_ip.stdout }}"
            dns: ""
            port: 10050
        macros:
          - macro: "{$MYSQL.USER}"
            value: "{{ db_user }}"
          - macro: "{$MYSQL.PASSWORD}"
            value: "{{ db_password }}"
          - macro: "{$MYSQL.DSN}"
            value: "tcp://{{ mysql_ip.stdout }}:3306"
    - name: Display completion message
      debug:
        msg: >
          Zabbix Server is now running in Docker containers.
          Access Zabbix Web Interface at http://{{ansible_default_ipv4}}:8080
          MySQL database container is running with user '{{ db_user }}'.

 

 

 

- 위 명령어를 통해 yml 파일 기반으로 자동으로 설치 


- WEB, MYSQL, Zabbix-Server, Agent 모두 설치된것을 확인 할 수 있다

- 확인이 완료되면 해당 아이피의 8080포트로 아래와 같은 페이지로 접근 가능한것을 확인 할 수 있다

 

 

 

'DevOps > Zabbix' 카테고리의 다른 글

자빅스에 대해서  (0) 2025.01.01