0

我正在尝试使用以下 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 角色来编写它。有人可以帮我解决这个问题。

4

1 回答 1

0

您可以执行以下操作:

---
- 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一个只是为了回答这个问题。

于 2018-05-15T10:27:19.353 回答