title: Ansible Undefined variable in vars_files item but the variable was defined using set_fact
body: <p>I'm trying to select a local file based on what motherboard version exists on the machine on which Ansible will deploy those files.</p>
  <p>So my approach was to use dictionary in format {&quot;<em>&lt;board_name&gt;</em>&quot; : &quot;<em>&lt;local_file&gt;.yml</em>&quot;}. And use this dictionary to populate a new variable (<code>pcu_config</code> here) that will finally store the name of the file to be used by ansible. I've gotten so far as shown in my implementation, and I'm get undefined variable error at line</p>
  <pre class="lang-yaml prettyprint-override"><code>vars_files:
      - &quot;{{ playbook_dir }}/pcu_config/{{ pcu_config }}&quot;
  </code></pre>
  <p>But as can be seen in the <em><strong>debug</strong></em> msg in the image of the output just before this play, <code>pcu_config</code> is indeed defined(and is the correct filename too).</p>
  <pre><code>- name: Find the pcu_config
    hosts: vehicle
    gather_facts : no
    vars:
      pcu_config_dict:
        VirtualBox: virtual.yml
        n1: n1.yml
        n2: n2.yml
        n3: n3.yml
        n4a: n4a.yml
        n4b: n4b.yml
        
    tasks:
          - name: Find the motherboard name
            shell: cat /sys/devices/virtual/dmi/id/board_name
            register: board_name
  
          - name: Find the motherboard version
            shell: cat /sys/devices/virtual/dmi/id/chassis_version
            register: board_version
  
          - debug:
              msg: &quot;board_name : {{ board_name.stdout }}, board_version: {{ board_version.stdout }}, {{pcu_config_dict}} &quot;
  
          - name: Assign the PCU_config file for other than n4's
            set_fact:
              pcu_config: '{{pcu_config_dict[ board_name.stdout | default(&quot;this cpu does not exist in the dict&quot;)] | default(&quot;&quot;) }}'
            when: board_name.stdout != &quot;n4&quot;
  
          - name: Assign the PCU_config file for n4
            set_fact:
              pcu_config: '{{pcu_config_dict[board_version.stdout | default(&quot;this cpu_version does not exist in the dict&quot;)] | default(&quot;&quot;) }}'
            when: board_name.stdout == &quot;n4&quot;
  
  
  - name : Check pcu_config is available elsewhere in playbook
    hosts: vehicle
    tasks: 
      - debug:
          msg: &quot;{{ pcu_config }}, {{ playbook_dir }}&quot;
  
  - name: Deploy software to vehicle
    hosts: vehicle
    vars_files:
      - &quot;{{ playbook_dir }}/pcu_config/{{ pcu_config }}&quot;
      - &quot;{{ playbook_dir }}/os_config/deb-files-cache.yml&quot;
      - &quot;{{ playbook_dir }}/os_config/python_dep.yml&quot;
  
    roles:
        - role: logger_network
          tags: base
  </code></pre>
  <p>Here is the output when I run this playbook on my virtual machine.
  <a href="https://i.stack.imgur.com/U90rd.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/U90rd.jpg" alt="Output of ansible playbook" /></a></p>
  <p>Suggestions welcome to solve the X or Y problem.(ref @Zeitounator's comment)</p>