我在运行Ansible,我试着让这个任务发挥作用。我已经将变量"docker_registries“的默认值定义为空列表:我注意到,如果列表为空,则在运行ansible剧本时会出现错误。这是我得到的错误:
致命的:***.cloudapp.azure.com:=>!=> {"msg":“传递给‘循环’的无效数据,它需要一个列表,而不是这个列表:无。提示:如果您传递了一个仅包含一个元素的list/dict,尝试将wantlist=True添加到查找调用中,或者使用q/query而不是查找}。”
我试图添加这样一个条件:如果"docker_registries“为空,则任务将继续进行,而不会引发错误。以下是此任务的代码:
- name: Log into additional docker registries, when required
command: docker login -u {{item.username}} -p {{item.password}} {{item.server}}
become: true
loop: "{{docker_registries}}"我试图将循环更改为loop: "{{ lookup(docker_registries, {'skip_missing': True})}}",但我得到了错误
任务执行期间发生异常。要查看完整的跟踪,请使用-vvv。错误是: AttributeError:'NoneType‘对象没有属性'lower’致命:***.cloudapp.azure.com: FAILED!=> {"msg":“模块执行过程中的意外失败”,"stdout":“}
我对这件事很陌生。有人能帮忙吗?
发布于 2020-03-17 22:56:08
因为,如果你想循环你的变量,那么它应该是可迭代的。
- name: Log into additional docker registries, when required
command: docker login -u {{ item.username }} -p {{ item.password }} {{ item.server }}
become: true
loop: "{{ docker_registries }}"
when: docker_registries is iterable根据变量的内容,时间条件仍然不能完成这个任务,因为传递给loop的数据的有效性将首先被考虑。
现在,您可以在循环声明本身中使用一个条件表达式来解决这个问题:
- name: Log into additional docker registries, when required
command: docker login -u {{ item.username }} -p {{ item.password }} {{ item.server }}
become: true
loop: "{{ docker_registries if docker_registries is iterable else [] }}"还请注意,字符串是中的一个可迭代的,简单地说,就是一个字符列表。
所以你可能想去:
- name: Log into additional docker registries, when required
command: docker login -u {{ item.username }} -p {{ item.password }} {{ item.server }}
become: true
loop: "{{ docker_registries if docker_registries is iterable and docker_registries is not string else [] }}"发布于 2020-03-19 16:55:47
谢谢各位。我的group_vars文件中有一行,我注释掉了所有的docker_registries用户名和密码,但是我没有注释docker_registries:行本身。这就是为什么给我的是“无”,而不是“空”。
https://stackoverflow.com/questions/60725648
复制相似问题