为添加到 nagiosXI 的服务创建 Ansible 角色

Creating Ansible role for services addition to nagiosXI

我正在尝试使用以下 CURL 命令向 NagiosXI 添加服务。

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"

在上面的命令中只有主机名和服务描述描述发生了变化。我正在使用 Item 模块调用主机名。并手动添加服务描述。如果我需要添加 50 个服务,我需要写这个命令 50 次。

我打算用ansible roles来写。谁能帮我解决这个问题。

您可以这样做:

---
- 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 }}"

获得以下输出:

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"
}

尝试 uri 模块,如果它不符合您的要求,请尝试 shell 模块。我反映了 debug 一个只是为了回答这个问题。