title: Ansible access same variables from multiple Json files
body: <p>I have multiple .json files on local host where I place my playbook:</p>
  
  <pre><code>json-file-path/{{ testName }}.json
  </code></pre>
  
  <p>{{ testName }}.json are: testA.json, testB.json, testC.json ... etc.</p>
  
  <p>All .json files have same keys with different values like this:</p>
  
  <p>json-file-path/testA.json:</p>
  
  <pre><code>{
     “a_key”: “a_value1”
     “b_key”: “b_value1”
   }
  </code></pre>
  
  <p>json-file-path/testB.json:</p>
  
  <pre><code>{
     “a_key”: “a_value2”
     “b_key”: “b_value2”
   }
  </code></pre>
  
  <p>json-file-path/testC.json:</p>
  
  <pre><code>{
     “a_key”: “a_value3”
     “b_key”: “b_value3”
   }
  
  .....
  </code></pre>
  
  <p>I need to access the key-value variables from all .json files and if the values meet some condition, I will perform some task in target host. For example, I have:</p>
  
  <pre><code>a_value1=3
  a_value2=4
  a_value3=1
  </code></pre>
  
  <p>I go through my .json file one by one, if a_key[value]>3, I will copy this .json file to target host, otherwise skip the task. In this case, I will only copy testC.json to target host.</p>
  
  <p>How would I achieve this? I was thinking of re-constructing my .json files using {{ testName }} as dynamic key of dict like this:</p>
  
  <pre><code> {
      “testName”: “testA”
          {
             “a_key”: “a_value1”
             “b_key”: “b_value1”
    }
  </code></pre>
  
  <p>So I can access my variable as {{ testName}}.a_key. So far I haven’t been able to achieve this. </p>
  
  <p>I have tried the following in my playbook:</p>
  
  <pre><code>—-
  - host: localhost 
    tasks:
      - name: construct json files
        vars:
          my_vars:
            a_key: “{{ a_value }}”
            b_key: “{{ b_value }}”
          with_dict: “{{ testName }}”
        copy:
          content: “{{ my_vars | to_nice_json }}”
          dest: /json-file-path/{{ testName }}.json
  </code></pre>
  
  <p>My updated playbook are:</p>
  
  <pre><code>/mypath/tmp/include.yaml:
  
  —-
  - hosts: remote_hostName
    tasks:
    - name: load json files
      set_fact:
        json_data: “{{ lookup(‘file’, item) | from_json }}”
    - name: copy json file if condition meets
      copy:
        src: “{{ item }}”
        dest: “{{ /remote_host_path/tmp}}/{{item | basename }}”
      delegate_to: “{{ remote_hostName }}”
      when: json_data.a_key|int&gt;5
  
  /mypath/test.yml:
  —-
  - hosts: localhost
    vars:
      local_src_ dir: /mypath/tmp
      remote_host: remote_hostName
      remote_dest_dir: /remote_host_path/tmp
    tasks:
      - name: looping
        include: include.yaml
        with_fileglob:
           - “{{ local_src_dir }}/*json”
  </code></pre>
  
  <p>All json files on localhost under /mypath/tmp/.</p>
  
  <p>Latest version of playbook. It is working now:</p>
  
  <pre><code>/mypath/tmp/include.yaml:
  —-
  - name: loafing json flies
    include_vars:
      file: “{{ item }}”
      name: json_data
  - name: copy json file to remote if condition meets
    copy: 
      src: “{{ item }}”
      dest: ‘/remote_host_path/tmp/{{item | basename}}’
    delegate_to: “{{ remote_host }}”
    when: json_data.a_key &gt; 5
  
  /mypath/test.yml:
  
  —-
  - hosts: localhost
    vars: 
      local_src_dir: /mypath/tmp
      remote_host: remote_hostName
      remote_dest_dir: /remote_host_path/tmp
    tasks:
      - name: looping json files
        include: include.yaml
        with_fileglob:
           - “{{ local_src_dir }}”/*json”
  </code></pre>