Ansible:通过 rest api 调用组时出错

Ansible : error in calling the groups through restapi

我正在尝试通过 variable.this 获取清单中的不同组是我试图在剧本中 运行 将主机添加到 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'] }}"}

编辑:代码格式化

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

库存:

[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'] }}"