如何解决Ansible正确的方法是如何先应用过滤器,然后再应用.split?
嘿,我想在变量中应用过滤器,然后用.split对其进行拆分,请尝试以下操作:
- name: Register all images files to import
find:
paths: "{{ remote_build_path }}/{{ dependencies_directory_name }}/docker_images"
patterns: "*"
register: docker_images
在find处注册变量,或者在这里我只能得到不起作用的基本名称。
- debug: var=docker_images
- name: Load image from archive and push to a private registry
docker_image:
name: "{{ item.path.split('/')[0] | basename }}"
tag: "{{ item.path.split('/')[1] | basename }} "
load_path: "{{ item.path }}"
source: load
with_items: "{{ docker_images.files }}"
试图应用|基本名称过滤器,然后拆分但失败
也尝试过:
- name: Load image from archive and push to a private registry
docker_image:
name: "{{ item.path.split('/')[0] }}"
tag: "{{ item.path.split('/')[1] }} "
load_path: "{{ item.path }}"
source: load
with_items: "{{ docker_images.files | basename }}"
在with_items级别上不起作用。
解决方法
应用双重拆分的方法虽然不是很优雅,但是仍然可以:
- name: Load image from archive and push to a private registry
docker_image:
name: "{{ item.path.split('/')[-1].split(':')[0] }}"
tag: "{{ item.path.split('/')[-1].split(':')[1] }}"
load_path: "{{ item.path }}"
source: load
with_items: "{{ docker_images.files }}"
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。