title: Ansible regex_replace does not recognize the variable
body: <p>I am trying to replace a string using regex_replace but I am having an issue with parameters.</p>
  
  <pre><code>vars:
      **mongoURI**: "mongodb://shard1:27017,shard2:27017,shard3:27017/?ssl=true&amp;authSource=admin&amp;replicaSet=TestCluster-shard-0"
  
  - name: Create Mongo connection string    
        set_fact:        
          **readinput**: "mongodb://{{ (userinfo_json.Users | first).username }}:{{ (userinfo_json.Users | first).password }}@" 
          readconnstring: "{{ mongoURI | regex_replace('mongodb://', '{{ readinput }}') | regex_replace('\\?ssl', 'test?ssl') + ('&amp;retryWrites=true') }}"
  </code></pre>
  
  <p>The above works fine and I get the desired output if I initialize the mongoURI variable with the URI. Here is the output.</p>
  
  <p>mongodb://testuser:password@shard1:27017,shard1:27017,shard1:27017/?ssl=true&amp;authSource=admin&amp;replicaSet=TestCluster-shard-0</p>
  
  <p>However I need to read the mongoURI value from JSON input. When I do that, the first regex_replace does not recognize the variable(readinput).</p>
  
  <pre><code>- name: Fetch Mongo URI
        set_fact:
          mongoURI: "{{ (cluster_status_check.content | from_json).mongoURIWithOptions }}"
        when: cluster_status_check.status == 200
  </code></pre>
  
  <p>In this scenario, the last two regex_replaces are working fine but the first one is being replaced with the variable name as below.</p>
  
  <p>*{{ readinput }}*shard1:27017,shard1:27017,shard1:27017/?ssl=true&amp;authSource=admin&amp;replicaSet=TestCluster-shard-0</p>
  
  <p>I tried to use urlsplit('hostname') but 'mongodb://' is not being recognized as hostname.</p>
  
  <p>I also tried removing single quotes around readinput variable 
  readconnstring: "{{ mongoURI | regex_replace('mongodb://', {{ readinput }})) and removing {{ }} for the variable
  readconnstring: "{{ mongoURI | regex_replace('mongodb://', readinput)) </p>
  
  <p>but both are giving syntax errors.</p>
  
  <p>Since from_json converts the output to a dict, I tried using with_items but no luck.</p>
  
  <pre><code>- name: Create Mongo connection string    
        set_fact:        
          readinput: "mongodb://{{ (userinfo_json.Users | first).username }}:{{ (userinfo_json.Users | first).password }}@" 
          readconnstring: "{{ item | regex_replace('mongodb://', '{{ readinput }}') | regex_replace('\\?ssl', 'test?ssl') + ('&amp;retryWrites=true') }}"
    with_items:
          - "{{ mongoURI }}"
  </code></pre>
  
  <pre><code>---
  - hosts: localhost
    gather_facts: no
    vars:
      mongoURI: ""
  
  - name: Check whether cluster already exists
        uri:
          url: https://cloud.mongodb.com/api/atlas/v1.0/groups/{{ groupid }}/clusters/{{ clustername }}
          return_content: yes
          method: GET
          user: "{{ user }}"
          password: "{{ apikey }}"
          status_code: 404, 200
        register: cluster_status_check
  - name: Fetch Mongo URI
        set_fact:
          mongoURI: "{{ (cluster_status_check.content | from_json).mongoURIWithOptions }}"
        when: cluster_status_check.status == 200
  
  - name: Create Mongo connection string    
        set_fact:        
          readinput: "mongodb://{{ (userinfo_json.Users | first).username }}:{{ (userinfo_json.Users | first).password }}@" 
          readconnstring: "{{ mongoURI | regex_replace('mongodb://', '{{ readinput }}') | regex_replace('\\?ssl', 'test?ssl') + ('&amp;retryWrites=true') }}"
  </code></pre>
  
  <p>Instead of replacing mongodb:// with the readinput value, it is being replaced as {{ readinput }}</p>
  
  <p>Desired output: </p>
  
  <p>mongodb://testuser:password@shard1:27017,shard1:27017,shard1:27017/?ssl=true&amp;authSource=admin&amp;replicaSet=TestCluster-shard-0</p>
  
  <p>Actual output:</p>
  
  <p>{{ readinput }}shard1:27017,shard1:27017,shard1:27017/?ssl=true&amp;authSource=admin&amp;replicaSet=TestCluster-shard-0</p>