我想从逗号分隔的字符串创建一个列表以传递给 ansible 中的循环,有时变量也只能有一个值
var1=test1,test2 也可以是 var1=test1
这是我的代码
- name: Separate facts
set_fact: groups="{{ var1.split(',') }}"
- name: delete
gcp_compute_instance_group:
name: "{{ item }}"
zone: xxx
project: xxx
auth_kind: serviceaccount
service_account_file: xxx
state: absent
loop: "{{ groups }}"
Run Code Online (Sandbox Code Playgroud)
这不起作用,我怎样才能达到我的要求
你的过滤器是正确的,你确实得到了一个列表变量。请参见下面的 PB 和输出:
---
- hosts: localhost
gather_facts: false
vars:
var1: test1,test2
var2: test3
tasks:
- name: Create the list
set_fact:
list_var1: "{{ var1.split(',') }}"
list_var2: "{{ var2.split(',') }}"
- debug:
var: list_var1
- debug:
var: list_var2
Run Code Online (Sandbox Code Playgroud)
结果:
[is@orangehat-29 temp]$ ansible-playbook test.yml
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
PLAY [localhost] ***********************************************************************************************************************************************************************************************************************
TASK [Create the list] *****************************************************************************************************************************************************************************************************************
ok: [localhost]
TASK [debug] ***************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
"list_var1": [
"test1",
"test2"
]
}
TASK [debug] ***************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
"list_var2": [
"test3"
]
}
PLAY RECAP *****************************************************************************************************************************************************************************************************************************
localhost : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[is@orangehat-29 temp]$
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10889 次 |
| 最近记录: |