ansibleyamlansible-rolenagiosxi

Creating Ansible role for services addition to nagiosXI


I am trying to add a service to the NagiosXI with the below CURL command.

curl -k -XPOST "https://16.231.22.60/nagiosxi/api/v1/config/service?apikey=qfOQpKFORCNo7HPunDUsSjW7f2rNNmrdVv3kvYpmQcNdSS2grV2jeXKsgbv3QgfL&pretty=1" -d "host_name=***{{ item }}***&***service_description=Service status for: sshd***&use=xiwizard_ncpa_service&check_command=check_xi_ncpa\! -t 5nidNag -P 5693 -M services -q service=sshd,status=running&check_interval=5&retry_interval=1"

In the above command only hostname and service description description changes. I am calling hostname with Item module. and adding service description manually. if i need to add 50 services i neeed to write this command for 50 times.

i am planning to write it by ansible roles. can someone help me out with this.


Solution

  • You can do something like:

    ---
    - name: Nagios Config
      gather_facts: False
      hosts: localhost
    
      vars: 
    
        servers: 
          - 10.100.10.5
          - 10.100.10.6
          - 10.100.10.7
    
        services: 
          - ssh
          - https
          - smtp
    
      tasks:
    
        - name: Add Nagios services
          debug:
            msg: "curl -host {{item.0}} with service {{ item.1 }}"
          with_nested:
            - "{{ servers }}"
            - "{{ services }}"
    

    Getting the following output:

    TASK [Add Nagios services] ********************************************************************************************************
    ok: [localhost] => (item=None) => {
        "msg": "curl -host 10.100.10.5 with service ssh"
    }
    ok: [localhost] => (item=None) => {
        "msg": "curl -host 10.100.10.5 with service https"
    }
    ok: [localhost] => (item=None) => {
        "msg": "curl -host 10.100.10.5 with service smtp"
    }
    ok: [localhost] => (item=None) => {
        "msg": "curl -host 10.100.10.6 with service ssh"
    }
    ok: [localhost] => (item=None) => {
        "msg": "curl -host 10.100.10.6 with service https"
    }
    ok: [localhost] => (item=None) => {
        "msg": "curl -host 10.100.10.6 with service smtp"
    }
    ok: [localhost] => (item=None) => {
        "msg": "curl -host 10.100.10.7 with service ssh"
    }
    ok: [localhost] => (item=None) => {
        "msg": "curl -host 10.100.10.7 with service https"
    }
    ok: [localhost] => (item=None) => {
        "msg": "curl -host 10.100.10.7 with service smtp"
    }
    

    Try the uri module if it doesn't fit your requirements, go for the shell one. I have reflected the debug one just to answer the question.