ansible-playbook 安装nagios插件,


新建一个yml文件(nagios服务端IP:10.100.2.180),如下:---
  - hosts: all
    vars_files:
      - vars.yml
    tasks:
      - name: setup program
        yum: name={{ item }} state=present
        with_items:
          - gcc
          - gcc-c++
          - make
          - openssl
          - openssl-devel
      - name: addgroup
        group:
          name: nagios
          state: present
      - name: adduser
        user:
          name: nagios
          shell: /sbin/nologin
          groups: nagios
      - name: copy program
        copy: "src={{ item.src }} dest={{ item.dest }}"
        with_items:
          - src: "/opt/nagios/nagios-plugins-2.1.1.tar.gz"
            dest: "/tmp/"
          - src: "/opt/nagios/nrped"
            dest: "/etc/init.d/nrped"
          - src: "/opt/nagios/nrpe-2.15.tar.gz"
            dest: "/tmp/"
      - name: tar files
        command: tar -zxvf /tmp/nrpe-2.15.tar.gz -C /tmp
      - name: tar naiogs
        command: tar -zxvf /tmp/nagios-plugins-2.1.1.tar.gz -C /tmp
      - name: install nagios
        shell: "/tmp/nagios-plugins-2.1.1/configure --with-nagios-user=nagios --with-nagios-group=nagios && make && make install"
      - name: install nrpe
        shell: /tmp/nrpe-2.15/configure --with-nrpe-user=nagios --with-nrpe-group=nagios --with-nagios-user=nagios --with-nagios-group=nagios --enable-command-args --enable-ssl && make all && make install-plugin && make install-daemon && make install-daemon-config
      - name: make a service
        command: chmod +x /etc/init.d/nrped
      - name: set chkconfig
        shell: chkconfig --add nrped && chkconfig nrped on
      - name: copy cfg file
        copy: "src=/opt/nagios/nrpe.cfg dest=/usr/local/nagios/etc/nrpe.cfg"
      - name: start nrped
        service: name=nrped state=started
      - name: add firewalld rule
        shell: firewall-cmd --permanent --add-rich-rule 'rule family=ipv4 source address=10.100.2.180/32 port port=5666 protocol=tcp accept'
        when: ansible_os_family == "RedHat" and ansible_distribution_major_version|int  >= 7
        notify: restart firewalld
      - name: add iptables rule
        shell: iptables -I INPUT -s 10.100.2.180/32 -p tcp --dport 5666 -j ACCEPT
        when: ansible_os_family == "RedHat" and ansible_distribution_major_version|int  <= 6
        notify: restart iptables
    handlers:
      - name: restart firewalld
        service: name=firewalld state=restarted
      - name: restart iptables
        service: name=iptables state=restarted

再创建一个sh脚本,如下:
#!/bin/bash
echo "please input the host name"
read name
#set -x
if  [ -z "$name" ] ;then
    echo "please input the host name! "
else
/usr/bin/ansible-playbook newserver.yml --limit $name
fi

相关内容

    暂无相关文章