Reputation: 708
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 }}" )
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)
?
Upvotes: 8
Views: 15690
Reputation: 21
You don’t need to use {{ }} to use variables inside conditionals, as these are already implied.
You can find detailed descriptions here Ansible Conditionals
Upvotes: 0
Reputation: 1986
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)
Upvotes: 12