title: Creating ansible inventory for multiple target hosts
body: <p>I am using terraform to create resources then using <strong>templatefile</strong> approach to create ansible inventory in yaml language. I am creating multiple Virtual Machines in terraform and configure them using ansible in single pipeline.</p>
  <p>Issue I am having is yaml file uses - for lists which is giving error in ansible playbook</p>
  <p>Outputs.tf</p>
  <pre><code>resource &quot;local_file&quot; &quot;AnsibleInventory&quot; {
  content = templatefile(&quot;inventory.tmpl&quot;,
  {
  ansible_port = &quot;5986&quot;
  ansible_connection = &quot;winrm&quot;
  ansible_winrm_server_cert_validation = &quot;ignore&quot;
  ansible_winrm_transport = &quot;ntlm&quot;
  vm-ip = data.azurerm_public_ip.main.*.ip_address,
  username = &quot;testadmin&quot;,
  ansible_password = &quot;abc&quot;
  }
  )
  filename = &quot;inventory.json&quot;
  }
  </code></pre>
  <p>inventory.tmpl</p>
  <pre><code>${jsonencode({
    &quot;all&quot;: {
      &quot;hosts&quot;: {
        &quot;server&quot;: [
          for ip in vm-ip : { 
          &quot;ansible_host&quot;: &quot;${ip}&quot;,
          &quot;ansible_port&quot;: 5986,
          &quot;ansible_user&quot;: &quot;testadmin&quot;,
          &quot;ansible_winrm_transport&quot;: &quot;ntlm&quot;,
          &quot;ansible_connection&quot;: &quot;winrm&quot;,
          &quot;ansible_winrm_server_cert_validation&quot;: &quot;ignore&quot;,
          &quot;ansible_password&quot;: &quot;abc&quot;
          }
        ]
      }
    }
  })}
  </code></pre>
  <p>inventory.json</p>
  <pre><code>{&quot;all&quot;:{&quot;hosts&quot;:{&quot;server&quot;:{&quot;ansible_connection&quot;:&quot;winrm&quot;,&quot;ansible_host&quot;:&quot;343434&quot;,&quot;ansible_password&quot;:&quot;abc&quot;,&quot;ansible_port&quot;:5986,&quot;ansible_user&quot;:&quot;testadmin&quot;,&quot;ansible_winrm_server_cert_validation&quot;:&quot;ignore&quot;,&quot;ansible_winrm_transport&quot;:&quot;ntlm&quot;}}}}
  </code></pre>
  <p>inventory.yaml</p>
  <pre><code>all:
    hosts:
      server:
      - ansible_connection: winrm
        ansible_host: 40.88.14.205
        ansible_password: abc
        ansible_port: 5986
        ansible_user: testadmin
        ansible_winrm_server_cert_validation: ignore
        ansible_winrm_transport: ntlm
  </code></pre>
  <p>Is my approach right then how to remove &quot;-&quot; from code or am I doing something wrong.</p>
  <p>Edited:</p>
  <p>I am converting json file to yaml file using bash in pipeline.</p>
  <pre><code>python -c 'import sys, yaml, json; yaml.safe_dump(json.load(sys.stdin), sys.stdout, default_flow_style=False)' &lt; inventory.json &gt; inventory.yaml
  </code></pre>