In Ansible, how to use a variable inside a variable definition that uses filters

I want to extract multiple regex urls from a url. The only thing I could do was the 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 can reuse the pattern variable in match , but not on the set_fact line, where I assign the selected text to another variable. Is there a way to reuse a variable in set_fact that uses filter(s) ?

+6
source share
2 answers

You should just be able to refer directly to certain variables. Try it; this 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) 
+8
source

You do not need to use {{}} to use variables inside conditional expressions, as they are already implied.

Detailed descriptions can be found here. Optional conditions

0
source

All Articles