title: How do I dynamically register variables with Ansible?
body: <p>I want to define an Ansible role and register dynamic variables:</p>
  
  <pre><code>---
  - name: Check for {{ package }}
    stat: path=/opt/packages/{{ package }}
    register: "{{ package | regex_replace('-', '_') }}"
  - name: Install {{ package }} {{ package_version }}
    command: "custom-package-installer {{ package }} {{ package_version }}"
    when: "not {{ package | regex_replace('-', '_') }}.stat.exists"
  </code></pre>
  
  <p>Usage looks like this:</p>
  
  <pre><code>- include: install_package.yml package=foo package_version=1.2.3
  </code></pre>
  
  <p>However, Ansible doesn't recognise the conditional:</p>
  
  <pre><code>TASK: [example | Install foo 1.2.3] *********************************** 
  fatal: [my-server] =&gt; error while evaluating conditional: not foo.stat.exists
  
  FATAL: all hosts have already failed -- aborting
  </code></pre>
  
  <p>How can I define variables dynamically, expanding the <code>{{</code> <code>}}</code>?</p>