title: how can i map a list to a dict with computed values keyed by the list items in ansible?
body: <p>apologies for the awkward title, but i couldn't figure out a better way to phrase what seems like a very common operation:</p>
  
  <p>i have a list like</p>
  
  <pre><code>repos:
  - nrser/x
  - nrser/y
  </code></pre>
  
  <p>and want to transform it to a dict like</p>
  
  <pre><code>repos_dict:
    nrser/x: nrser_x
    nrser/y: nrser_y
  </code></pre>
  
  <p>this is super simple in python</p>
  
  <pre><code>repos = ['nrser/x', 'nrser/y']
  repos_dict = dict((repo, repo.replace('/', '_')) for repo in repos)
  # =&gt; {'nrser/x': 'nrser_x', 'nrser/y': 'nrser_y'}
  </code></pre>
  
  <p>but i can't figure out how to accomplish it with Ansible / Jinja2 (short of dropping into python via a module or plugin, but that seems ridiculous for such a basic use case).</p>
  
  <p>it's easy to map the <code>repos</code> to a new list with the underscored names (i need to use them in file paths)</p>
  
  <pre><code>set_fact:
    repo_filename_segments: "{{ repos | map('replace', '/', '_') | list }}"
  </code></pre>
  
  <p>but then i need zip them together, and i can't find support for that either (see <a href="https://stackoverflow.com/questions/5208252/ziplist1-list2-in-jinja2">ziplist1-list2-in-jinja2</a> and <a href="https://stackoverflow.com/questions/34667091/how-to-combine-two-lists-together">how-to-combine-two-lists-together</a>)</p>
  
  <p>i've tried:  </p>
  
  <pre><code>- set_fact:
        repos:
        - beiarea/www
        - beiarea/relib
  
    - set_fact:
        repos_dict: {}
  
    - with_items: "{{ repos }}"
      set_fact:
        "repos_dict[{{ item }}]: "{{ item | replace('/', '_') }}"
  </code></pre>
  
  <p>but that doesn't work either.</p>
  
  <p>maybe it's not possible in Ansible / Jinja, but it seems like a really elementary operation to have been overlooked.</p>
  
  <p>thanks for any solutions or suggestions.</p>