[ansible-zabbix]-ansible-playbook推送安装zabbix-agent并调用api自动加入主机

pip install zabbix-api

 

模板zabbix_agentd.conf如下

PidFile=/var/run/zabbix/zabbix_agentd.pid

LogFile=/var/log/zabbix/zabbix_agentd.log

LogFileSize=0

Server=192.168.1.187

ServerActive=192.168.1.187

HostnameItem=system.hostname

Include=/etc/zabbix/zabbix_agentd.d/*.conf

 

hosts文件如下

[test]

192.168.0.232

[test:vars]

ansible_ssh_user=root

[email protected]

 

 

yml文件如下

---

- hosts: test

  tasks:

    - name: copy zabbix_agent.rpm

      copy: src=./zabbix-agent-4.2.6-1.el7.x86_64.rpm  dest=/opt/zabbix-agent-4.2.6-1.el7.x86_64.rpm

    - name: install zabbix-agent-4.2.6-1.el7.x86_64.rpm

      shell: yum -y install /opt/zabbix-agent-4.2.6-1.el7.x86_64.rpm

    - name: copy zabbix_agent.conf

      copy: src=./zabbix_agentd.conf  dest=/etc/zabbix/zabbix_agentd.conf

    - name: start and enable zabbix-agent

      service :

        name: zabbix-agent

        state: started

        enabled: yes

    - name: create a new host

      local_action:

        module: zabbix_host

        server_url: http://192.168.1.187:8080/zabbix/

        login_user: admin

        login_password: passwd

        force: yes

        host_name: "{{ ansible_default_ipv4.address}}"

        visible_name: "{{ ansible_hostname }}"

        host_groups:

          - zabbix_agent

        link_templates:

          - Template Module ICMP Ping

          - Template OS Linux

        status: enabled

        state: present

        inventory_mode: automatic

        interfaces:

          - type: 1

            main: 1

            useip: 1

            ip: "{{ ansible_default_ipv4.address }}"

            dns: ""

            port: 10050

 

 

 

 

测试

[[email protected] ansible]# ansible-playbook -C test3.yml

 

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

 

TASK [Gathering Facts] *******************************************************************************************************************************************************************************************************************************************************

ok: [192.168.0.232]

 

TASK [copy zabbix_agent.rpm] *************************************************************************************************************************************************************************************************************************************************

changed: [192.168.0.232]

 

TASK [install zabbix-agent-4.2.6-1.el7.x86_64.rpm] ***************************************************************************************************************************************************************************************************************************

skipping: [192.168.0.232]

 

TASK [copy zabbix_agent.conf] ************************************************************************************************************************************************************************************************************************************************

changed: [192.168.0.232]

 

TASK [start and enable zabbix-agent] *****************************************************************************************************************************************************************************************************************************************

changed: [192.168.0.232]

 

TASK [create a new host] *****************************************************************************************************************************************************************************************************************************************************

changed: [192.168.0.232 -> localhost]

 

PLAY RECAP *******************************************************************************************************************************************************************************************************************************************************************

192.168.0.232              : ok=5    changed=4    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0  

 

 

[ansible-zabbix]-ansible-playbook推送安装zabbix-agent并调用api自动加入主机

 

 

 

 

执行

 

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

 

TASK [Gathering Facts] *******************************************************************************************************************************************************************************************************************************************************

ok: [192.168.0.232]

 

TASK [copy zabbix_agent.rpm] *************************************************************************************************************************************************************************************************************************************************

changed: [192.168.0.232]

 

TASK [install zabbix-agent-4.2.6-1.el7.x86_64.rpm] ***************************************************************************************************************************************************************************************************************************

 [WARNING]: Consider using the yum module rather than running 'yum'.  If you need to use command because yum is insufficient you can add 'warn: false' to this command task or set 'command_warnings=False' in ansible.cfg to get rid of this message.

 

changed: [192.168.0.232]

 

TASK [copy zabbix_agent.conf] ************************************************************************************************************************************************************************************************************************************************

changed: [192.168.0.232]

 

TASK [start and enable zabbix-agent] *****************************************************************************************************************************************************************************************************************************************

changed: [192.168.0.232]

 

TASK [create a new host] *****************************************************************************************************************************************************************************************************************************************************

changed: [192.168.0.232 -> localhost]

 

PLAY RECAP *******************************************************************************************************************************************************************************************************************************************************************

192.168.0.232              : ok=6    changed=5    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  

 

[ansible-zabbix]-ansible-playbook推送安装zabbix-agent并调用api自动加入主机

 

 

[ansible-zabbix]-ansible-playbook推送安装zabbix-agent并调用api自动加入主机

[ansible-zabbix]-ansible-playbook推送安装zabbix-agent并调用api自动加入主机

[ansible-zabbix]-ansible-playbook推送安装zabbix-agent并调用api自动加入主机

[ansible-zabbix]-ansible-playbook推送安装zabbix-agent并调用api自动加入主机

 

现在修改了效果如下

 

[ansible-zabbix]-ansible-playbook推送安装zabbix-agent并调用api自动加入主机

---

- hosts: test

  tasks:

    - name: copy zabbix_agent.rpm

      copy: src=./zabbix-agent-4.2.6-1.el7.x86_64.rpm  dest=/opt/zabbix-agent-4.2.6-1.el7.x86_64.rpm

    - name: install zabbix-agent-4.2.6-1.el7.x86_64.rpm

      #shell: yum -y install /opt/zabbix-agent-4.2.6-1.el7.x86_64.rpm

      yum:

        name: /opt/zabbix-agent-4.2.6-1.el7.x86_64.rpm

        state: present

    - name: copy zabbix_agent.conf

      copy: src=./zabbix_agentd.conf  dest=/etc/zabbix/zabbix_agentd.conf

    - name: start and enable zabbix-agent

      service :

        name: zabbix-agent

        state: started

        enabled: yes

    - name: create a new host

      local_action:

        module: zabbix_host

        server_url: http://192.168.1.187:8080/zabbix/

        login_user: admin

        login_password: passwd

        force: yes

        host_name: "{{ ansible_default_ipv4.address}}"

        visible_name: "{{ ansible_hostname }}"

        host_groups:

          - zabbix_agent

        link_templates:

          - Template Module ICMP Ping

          - Template OS Linux

        status: enabled

        state: present

        inventory_mode: automatic

        interfaces:

          - type: 1

            main: 1

            useip: 1

            ip: "{{ ansible_default_ipv4.address }}"

            dns: ""

            port: 10050

 

 

 

 

后续测试 可能还是修改成这样好

---

- hosts: ansible-test

  tasks:

    - name: copy zabbix_agent.rpm

      copy: src=./zabbix-agent-4.2.6-1.el7.x86_64.rpm  dest=/opt/zabbix-agent-4.2.6-1.el7.x86_64.rpm

    - name: install zabbix-agent-4.2.6-1.el7.x86_64.rpm

      #shell: yum -y install /opt/zabbix-agent-4.2.6-1.el7.x86_64.rpm

      yum:

        name: /opt/zabbix-agent-4.2.6-1.el7.x86_64.rpm

        state: present

    - name: copy zabbix_agent.conf

      copy: src=./zabbix_agentd.conf  dest=/etc/zabbix/zabbix_agentd.conf

    - name: start and enable zabbix-agent

      service :

        name: zabbix-agent

        state: started

        enabled: yes

    - name: create a new host

      local_action:

        module: zabbix_host

        server_url: http://192.168.1.187:8080/zabbix/

        login_user: admin

        login_password: passwd

        force: yes

        host_name: "{{ ansible_hostname }}"

        #host_name: "{{ ansible_default_ipv4.address}}"

        #visible_name: "{{ ansible_hostname }}"

        host_groups:

          - zabbix_agent

        link_templates:

          - Template Module ICMP Ping

          - Template OS Linux

        status: enabled

        state: present

        inventory_mode: automatic

        interfaces:

          - type: 1

            main: 1

            useip: 1

            ip: "{{ ansible_default_ipv4.address }}"

            dns: ""

            port: 10050

 

现在换成了j2模板

创建/etc/ansible/test.j2如下

PidFile=/var/run/zabbix/zabbix_agentd.pid

LogFile=/var/log/zabbix/zabbix_agentd.log

LogFileSize=0

Server=192.168.1.187

ServerActive=192.168.1.187

{% if ansible_nodename%}

Hostname={{ansible_nodename}}

{% endif %}

Include=/etc/zabbix/zabbix_agentd.d/*.conf

创建/etc/ansible/   如下

---

- hosts: test

  user: root

  gather_facts: true

  vars:

    hostname: "{{ ansible_nodename }}"

  tasks:

    - name: copy zabbix_agent.rpm

      copy: src=./zabbix-agent-4.2.6-1.el7.x86_64.rpm  dest=/opt/zabbix-agent-4.2.6-1.el7.x86_64.rpm

    - name: install zabbix-agent-4.2.6-1.el7.x86_64.rpm

      yum:

        name: /opt/zabbix-agent-4.2.6-1.el7.x86_64.rpm

        state: present

    - name: copy file to client

      template: src=/etc/ansible/test.j2 dest=/etc/zabbix/zabbix_agentd.conf

    - name: start and enable zabbix-agent

      service :

        name: zabbix-agent

        state: started

        enabled: yes

    - name: create a new host

      local_action:

        module: zabbix_host

        server_url: http://192.168.1.187:8080/zabbix/

        login_user: admin

        login_password: password

        force: yes

        host_name: "{{ ansible_nodename }}"

        host_groups:

          - zabbix_agent

        link_templates:

          - Template Module ICMP Ping

          - Template OS Linux

          - Auto Discovery Port

        status: enabled

        state: present

        inventory_mode: automatic

        interfaces:

          - type: 1

            main: 1

            useip: 1

            ip: "{{ ansible_default_ipv4.address }}"

            dns: ""

            port: 10050

    - name: start and enable zabbix-agent

      service:

        name: zabbix-agent

        state: restarted

        enabled: yes