Ansible:通过restapi调用组时出错

时间:2018-04-25 14:57:31

标签: ansible ansible-inventory ansible-api

我正在尝试通过变量获取库存中的不同组。这是我试图在剧本中运行以将主机添加到Nagios XI的命令。我试图通过CURL命令使用Rest API来做到这一点。我得到的错误是错误的模式。有人可以就此问题提出建议。或者帮助我解决我们如何在同一命令中从库存中调用两个组的问题。

- name: add host to nagios XI.
      shell: curl -XPOST "http://16.231.22.60/nagiosxi/api/v1/config/host?apikey=qfOQpKFORCNo7HPunDUsSjW7f2rNNmrdVv3kvYpmQcNdSS2grV2jeXKsgbv3QgfL&pretty=1" -d "host_name={{ item.hostname }}&address={{ item.address }}&use=xiwizard_ncpa_host&max_check_attempts=5&check_period=xi_timeperiod_24x7&notification_interval=60&notification_period=xi_timeperiod_24x7&notifications_enabled=0&contacts=nagiosadmin&contact_groups=Candle Admins,Candle-L1-L2-Internal&applyconfig=1"
      with_items:
        - { hostname: "{{ groups['grp1'] }}", address: "{{ groups['grp2'] }}"}

编辑:代码格式

1 个答案:

答案 0 :(得分:0)

了解每个组中的主机名和地址是否相互匹配,您可以执行以下操作:

清单:

[grp1]
host1
host2
host3

[grp2]
10.100.10.1
10.100.10.2
10.100.10.3

玩:

---
- name: Debug Together
  hosts: localhost
  gather_facts: False

  tasks:
    - name: Add host to nagios XI
      shell: shell: curl -XPOST "http://16.231.22.60/nagiosxi/api/v1/config/host?apikey=qfOQpKFORCNo7HPunDUsSjW7f2rNNmrdVv3kvYpmQcNdSS2grV2jeXKsgbv3QgfL&pretty=1" -d "host_name={{ item.0 }}&address={{ item.1 }}&use=xiwizard_ncpa_host&max_check_attempts=5&check_period=xi_timeperiod_24x7&notification_interval=60&notification_period=xi_timeperiod_24x7&notifications_enabled=0&contacts=nagiosadmin&contact_groups=Candle Admins,Candle-L1-L2-Internal&applyconfig=1"
      with_together:
        - "{{ groups['grp1'] }}"
        - "{{ groups['grp2'] }}"

你会得到类似的东西:

TASK [debug] ******************************************************************************************************************
ok: [localhost] => (item=None) => {
    "item.0,  item.1": "(u'host1', u'10.100.10.1')"
}
ok: [localhost] => (item=None) => {
    "item.0,  item.1": "(u'host2', u'10.100.10.2')"
}
ok: [localhost] => (item=None) => {
    "item.0,  item.1": "(u'host3', u'10.100.10.3')"
}

来自我的测试:

- name:
  debug:
    var: item.0,  item.1
  with_together:
    - "{{ groups['grp1'] }}"
    - "{{ groups['grp2'] }}"