为nagiosXI添加服务创建Ansible角色

时间:2018-05-14 10:32:09

标签: ansible yaml ansible-role 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角色写它。有人可以帮我解决这个问题。

1 个答案:

答案 0 :(得分: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只是为了回答这个问题。