在Ansible中,可以使用ansible_facts
变量来获取Ansible测试变量的前缀。下面是一个示例代码:
---
- name: Print test variables
hosts: localhost
gather_facts: false
tasks:
- name: Set test variables
set_fact:
test_var1: "value1"
test_var2: "value2"
test_var3: "value3"
other_var: "value4"
- name: Print test variables starting with "test_"
debug:
msg: "{{ item }}"
loop: "{{ ansible_facts.keys() | select('match', '^test_') | list }}"
在上面的示例中,我们设置了几个测试变量test_var1
,test_var2
,test_var3
和other_var
。然后,我们使用ansible_facts.keys()
获取所有的Ansible事实的键,然后使用select
过滤出以"test_"开头的键,最后使用loop
循环输出这些变量。
运行上述代码后,你将看到只有以"test_"开头的变量被打印出来。输出示例:
TASK [Print test variables starting with "test_"] ************************************************************************
ok: [localhost] => (item=test_var1) => {
"msg": "test_var1"
}
ok: [localhost] => (item=test_var2) => {
"msg": "test_var2"
}
ok: [localhost] => (item=test_var3) => {
"msg": "test_var3"
}