0

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.

  • Hey @dude you have plenty of places to understand how to create a role. What is your idea? Are you going to have the services in a DB in Variables? – imjoseangel May 14 '18 at 11:34
  • Hi @imjoseangel i am adding these hosts and services to NagiosXI. the variable item in the command fills the host name from the inventory, the same will be reflected in the nagios server –  May 15 '18 at 09:23
  • If you know what the services are, keep them in a var file and import it with include_vars. Then, iterate over it with a loop. – imjoseangel May 15 '18 at 09:32
  • if you see in my curl command there is service description there i want to do it for 3 services, there one item will call the hosts, how can i iterate the service there ?? –  May 15 '18 at 09:49
  • Use with_nested. For every item.0 you will apply services item.1 – imjoseangel May 15 '18 at 09:51

1 Answers1

0

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.

imjoseangel
  • 3,543
  • 3
  • 22
  • 30
  • thanks for this, Here am trying from Ansible Tower not CLI. where i have created 3 groups, grp1 for hostname, grp2 for IPAddress, Grp3 for services. But in tower after creating group3 how can i add these services in the inventory.? –  May 15 '18 at 10:42
  • Same way with item.2 – imjoseangel May 15 '18 at 12:38
  • Yes, but it is also useful using an external var files. If the role is going to be common I always prefer it. – imjoseangel May 16 '18 at 08:28
  • Hi, here am trying to add 25 serverices in a group and 1 single host in a group. but its not adding all the services, only one service is added –  May 18 '18 at 11:09