首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >防止错误: with_items需要一个列表或一个集合

防止错误: with_items需要一个列表或一个集合
EN

Stack Overflow用户
提问于 2017-07-18 15:30:52
回答 1查看 644关注 0票数 0

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

不可见源代码:

代码语言:javascript
复制
- 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

输出

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2017-07-18 15:40:03

这里有什么真正的理由使用with_items循环吗?

替换

代码语言:javascript
复制
with_items: "{{grafana_datasource_body}}"

使用

代码语言:javascript
复制
with_items: "{{ [grafana_datasource_body] }}"

这会管用的。

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45171213

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档