Ansible错误:with_items需要列表或集合

时间:2017-07-18 15:30:53

标签: ansible

这是Ansible源代码,其中with_items链接到名为"grafana_datasource_body"的字典类型变量。我总是收到错误消息"ith_items expects a list or a set"。这是源代码和相关输出。

Ansible源代码:

- name: Configure datasource creation http body
  set_fact:
    grafana_datasource_body:
      name: "{{Ass_grafana_Datasource_Name}}"
      type: "{{Ass_grafana_Datasource_Type}}"
      isDefault: "{{Ass_grafana_Datasource_IsDefault}}"
      access: "{{Ass_grafana_Datasource_Access}}"
      basicAuth: "{{Ass_grafana_Datasource_BasicAuth}}"
      url: "{{InfluxDB_Server}}:{{Ass_grafana_Datasource_Http_Port}}"
      database: "{{Ass_grafana_Datasource_Db}}"
      username: "{{Ass_grafana_Datasource_DbUser}}"
      password: "{{Ass_grafana_Datasource_DbPassword}}"
  when: Ass_grafana_Datasource_Name not in grafana_datasources_output.json|map(attribute='name')|list

- debug: msg="Datasource creation http body = {{grafana_datasource_body}}"
  when: Ass_grafana_Datasource_Name not in grafana_datasources_output.json|map(attribute='name')|list

# Create non existing datasources
- name: datasource > Create datasource
  register: grafana_datasources_create_output
  failed_when: "'Datasource added' not in grafana_datasources_create_output|to_json"
  uri:
    url: "http://127.0.0.1:{{Ass_grafana_Listen_Http_Port}}/api/datasources"
    method: POST
    HEADER_Content-Type: application/json
    body: '{{ item|to_json }}'
    force_basic_auth: true
    user: "{{Ass_grafana_Api_User}}"
    password: "{{Ass_grafana_Api_Password}}"
    status_code: 200
  with_items: "{{grafana_datasource_body}}"
  when: item.name not in grafana_datasources_output.json|map(attribute='name')|list

输出

TASK: [./grafana/grafana_Role | Configure datasource creation http body] ******
<10.0.20.7> ESTABLISH CONNECTION FOR USER: centos
ok: [10.0.20.7] => {"ansible_facts": {"grafana_datasource_body": {"access": "proxy", "basicAuth": "false", "database": "webcom-int", "isDefault": "true", "name": "MonTonton", "password": "webcom", "type": "influxdb", "url": "http://localhost:8086", "username": "webcom"}}}

TASK: [./grafana/grafana_Role | debug msg="Datasource creation http body = {{grafana_datasource_body}}"] ***
<10.0.20.7> ESTABLISH CONNECTION FOR USER: centos
ok: [10.0.20.7] => {
"msg": "Datasource creation http body = {'username': u'webcom', 'name': u'MonTonton', 'database': u'webcom-int', 'url': u'http://localhost:8086', 'basicAuth': u'false', 'access': u'proxy', 'password': u'webcom', 'type': u'influxdb', 'isDefault': u'true'}"
}

TASK: [./grafana/grafana_Role | datasource > Create datasource] ***************
fatal: [10.0.20.7] => with_items expects a list or a set

FATAL: all hosts have already failed -- aborting

1 个答案:

答案 0 :(得分:0)

在这里使用with_items循环的任何真正原因?

替换

with_items: "{{grafana_datasource_body}}"

with_items: "{{ [grafana_datasource_body] }}"

这将有效。

但您可以使用body: '{{ grafana_datasource_body|to_json }}'而不使用with_items