Answers: <p>Yes. It is possible to create a YAML data structure with Jinja and read the variable in with the filter <a href="https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html#filters-for-formatting-data" rel="nofollow noreferrer">from_yaml</a>.</p>
  
  <p>The play</p>
  
  <pre><code>- hosts: group1
    vars:
      script_dir: /tmp
    tasks:
      - debug:
          var: filewatches
  </code></pre>
  
  <p>with the group_vars</p>
  
  <pre><code>$ cat group_vars/group1.yml 
  filewatches: "{{ filewatches_var|from_yaml }}"
  filewatches_var: |
      {% for host in groups['worker'] %}
      - type: "copy"
        directory_name: "{{ host }}"
        shell_script: "{{ script_dir }}/ascript.sh"
      {% endfor %}
  </code></pre>
  
  <p>and with the inventory</p>
  
  <pre><code>$ cat hosts
  ...
  group1:
    hosts:
      test_01
  
  group2:
    hosts:
      test_02
      test_03
  </code></pre>
  
  <p>gives</p>
  
  <pre><code>ok: [test_01] =&gt; {
      "filewatches": [
          {
              "directory_name": "test_02", 
              "shell_script": "/tmp/ascript.sh", 
              "type": "copy"
          }, 
          {
              "directory_name": "test_03", 
              "shell_script": "/tmp/ascript.sh", 
              "type": "copy"
          }
      ]
  }
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2019-09-14T03:32:14.667Z
Answers: 2019-09-14T03:32:14.667Z
Answers: Vladimir Botka
Body: <p>Is it possible to do something like this:</p>
  
  <pre><code># inventories/inventory1/group_vars/group1
  
  filewatches:
      {% for host in groups['group2'] %}
      - type: "copy"
        directory_name: "{{ host }}"
        shell_script: "{{ script_dir }}/ascript.sh"
       {endfor %}
      - type: "anotherone"
        ...
  </code></pre>
  
  <pre><code># inventories/inventory1/hosts
  
  [group1]
  somemachine01
  
  [group2]
  machine1
  machine2
  machine3
  ...
  </code></pre>
  
  <p>Basically, I want to generate a bunch configuration that is similar except for just one variable (in this case the directory_name variable). I would rather do this than making 20+ different entries which all differ by JUST the directory_name variable.</p>
  
  <p>I did not include the playbook or roles information for the sake of clarity/terseness. I hope I was clear enough and gave enough context, and I appreciate any help.</p>
  
  <p>Thanks!</p>
  
Tags: <ansible>
Title: Looping through hosts from an inventory group in a group_vars file
UserDisplayName: FearOfChickens
ThreadText: # Looping through hosts from an inventory group in a group_vars file
  
  (Asked by: username_0 on 2019-09-13)
  
  Is it possible to do something like this:
  
  ```
  # inventories/inventory1/group_vars/group1
  
  filewatches:
      {% for host in groups['group2'] %}
      - type: "copy"
        directory_name: "{{ host }}"
        shell_script: "{{ script_dir }}/ascript.sh"
       {endfor %}
      - type: "anotherone"
        ...
  
  ```
  
  ```
  # inventories/inventory1/hosts
  
  [group1]
  somemachine01
  
  [group2]
  machine1
  machine2
  machine3
  ...
  
  ```
  
  Basically, I want to generate a bunch configuration that is similar except for just one variable (in this case the directory\_name variable). I would rather do this than making 20+ different entries which all differ by JUST the directory\_name variable.
  
  I did not include the playbook or roles information for the sake of clarity/terseness. I hope I was clear enough and gave enough context, and I appreciate any help.
  
  Thanks!
  
  **Tags:** ansible
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2019-09-14. Score: 2
  
  Yes. It is possible to create a YAML data structure with Jinja and read the variable in with the filter [from\_yaml](https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html#filters-for-formatting-data).
  
  The play
  
  ```
  - hosts: group1
    vars:
      script_dir: /tmp
    tasks:
      - debug:
          var: filewatches
  
  ```
  
  with the group\_vars
  
  ```
  $ cat group_vars/group1.yml 
  filewatches: "{{ filewatches_var|from_yaml }}"
  filewatches_var: |
      {% for host in groups['worker'] %}
      - type: "copy"
        directory_name: "{{ host }}"
        shell_script: "{{ script_dir }}/ascript.sh"
      {% endfor %}
  
  ```
  
  and with the inventory
  
  ```
  $ cat hosts
  ...
  group1:
    hosts:
      test_01
  
  group2:
    hosts:
      test_02
      test_03
  
  ```
  
  gives
  
  ```
  ok: [test_01] => {
      "filewatches": [
          {
              "directory_name": "test_02", 
              "shell_script": "/tmp/ascript.sh", 
              "type": "copy"
          }, 
          {
              "directory_name": "test_03", 
              "shell_script": "/tmp/ascript.sh", 
              "type": "copy"
          }
      ]
  }
  
  ```
  
  
  ---
  
  