在 Ansible 中,如何在使用过滤器的变量定义中使用变量 [英] In Ansible, how to use a variable inside a variable definition that uses filters
本文介绍了在 Ansible 中,如何在使用过滤器的变量定义中使用变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从 URL 中提取一些正则表达式匹配项.我能做到这一点的唯一方法是:
I want to extract a few regex matches out of a URL. The only way I could do this was following:
- name: Set regex pattern for github URL
set_fact: pattern="^(git\@github\.com\:|https?\:\/\/github.com\/)(.*)\/([^\.]+)(\.git)?$"
- name: Extract organization name
set_fact: project_repo="{{ deploy_fork | regex_replace( "^(git\@github\.com\:|https?\:\/\/github.com\/)(.*)\/([^\.]+)(\.git)?$", "\\3" ) }}"
when: deploy_fork | match( "{{ pattern }}" )
通过这种方法,我可以在 match
过滤器中重用变量 pattern
,但不能在我的 set_fact
行上重用将提取的文本分配给另一个变量.有没有办法重用 set_fact
中使用 filter(s)
的变量?
With this approach, I'm able to reuse the variable pattern
in the match
filter, but not on the set_fact
line where I assign the extracted text to another variable.
Is there any way to reuse the variable in set_fact
that uses filter(s)
?
推荐答案
您应该能够直接引用定义的变量.尝试这个;它对我有用:
You should just be able to reference the defined variables directly. Try this; it worked for me:
- name: Set regex pattern for github URL
set_fact: pattern="^(git\@github\.com\:|https?\:\/\/github.com\/)(.*)\/([^\.]+)(\.git)?$"
- name: Extract organization name
set_fact: project_repo="{{ deploy_fork | regex_replace(pattern, "\\3" ) }}"
when: deploy_fork | match(pattern)
这篇关于在 Ansible 中,如何在使用过滤器的变量定义中使用变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文