[英]how to split string to array and loop over it?
我有一個像RCD_APIS=backend,api-alerting,api-tracking,api-versioning
這樣的字符串變量,其中包含我的docker映像的名稱。 我需要將其拆分為一個數組並循環遍歷,以便可以提取每個docker映像
我已經嘗試過with_sequence循環,但是我只得到索引(1,2,3,..)
- name: pull images from registry
docker_image:
name: "hostname:5000/{{ RCD_APIS.split(',') }}"
pull: true
state: present
tag: "{{RCD_VERSION_CURRENT}}"
with_sequence: count={{ RCD_APIS|count }}
我也嘗試了with_item循環,但是它不起作用,所以我嘗試調試:
vars:
- container: "{{ RCD_APIS }}"
tasks:
- name: pull images from registry debug
debug: var={{item|basename}}
with_items: container.split(',')
我得到類似的東西:
(item=container.split(',')) => {
"container.split(',')": [
"backend",
"api-alerting",
"api-tracking",
"api-versioning",
"connecteur-gdfa",
"api-batch",
"ihm"
],
"item": "container.split(',')"
}
所以我該如何遍歷該數組(像foreach一樣),然后docker pull backend, docker pull api-alerting
...?
干得好:
- name: pull images from registry
docker_image:
name: hostname:5000/{{ item }}
pull: true
state: present
tag: "{{RCD_VERSION_CURRENT}}"
with_items: "{{ RCD_APIS.split(',') }}"
要在debug
模塊中打印列表,其中x.content
是帶換行符的字符串,並且您希望在換行符上拆分,請使用:
debug:
msg: "{{ x.content.split('\n') }}"
要么
debug:
var: "x.content.split('\n')"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.