<p>I am new to terraform and devops in general. First I need to get ssh key from url to known host to later use for Flux.</p>
<pre><code>data &quot;helm_repository&quot; &quot;fluxcd&quot; {
  name = &quot;fluxcd&quot;
  url  = &quot;https://charts.fluxcd.io&quot;
}

resource &quot;helm_release&quot; &quot;flux&quot; {
  name      = &quot;flux&quot;
  namespace = &quot;flux&quot;

  repository = data.helm_repository.fluxcd.metadata[0].name
  chart      = &quot;flux&quot;

  set {
    name  = &quot;git.url&quot;
    value = &quot;git.project&quot;
  }

  set {
    name  = &quot;git.secretName&quot;
    value = &quot;flux-git-deploy&quot;
  }

  set {
    name  = &quot;syncGarbageCollection.enabled&quot;
    value = true
  }

  
  set_string {
    name  = &quot;ssh.known_hosts&quot;
    value = Need this value from url
  }

}
</code></pre>
<p>Then I need to generate key and use it to create kubernetes secret to communicate with gitlab repository.</p>
<pre><code>resource &quot;kubernetes_secret&quot; &quot;flux-git-deploy&quot; {
  metadata {
    name      = &quot;flux-git-deploy&quot;
    namespace = &quot;flux&quot;
  }

  type = &quot;Opaque&quot;

  data = {
    identity = tls_private_key.flux.private_key_pem
  }
}

resource &quot;gitlab_deploy_key&quot; &quot;flux_deploy_key&quot; {
    title = &quot;Title&quot;
    project = &quot;ProjectID&quot;
    key = tls_private_key.flux.public_key_openssh
    can_push = true
}
</code></pre>
<p>I am not sure if I am on the right track. Any advice will help.</p>

Jozef Vrana
<p>There are few approaches you could use. These can be divided into &quot;two categories&quot;:</p>
<ul>
<li>generate manually the ssh_known_hosts and use the output through variables or files
<ul>
<li>create the file on the machine where you're running terraform with the command <code>ssh-keyscan &lt;git_domain&gt;</code> and set the path as value for <code>ssh.known_hosts</code>.</li>
<li>You can also use the <a href="https://www.terraform.io/docs/configuration/functions/file.html" rel="nofollow noreferrer">file function</a> directly in the variable or use the file output directly as env variable. Personally I would not recommend it because the value is saved directly in the terraform state but in this case it is not a critical issue. Critical would be if you're using ssh_keys or credentials.</li>
</ul>
</li>
<li>Another approach would be to use the <a href="https://www.terraform.io/docs/provisioners/local-exec.html" rel="nofollow noreferrer">local-exec provisioner</a> with a <a href="https://www.terraform.io/docs/provisioners/null_resource.html" rel="nofollow noreferrer">null_resource</a> before you create the helm resource for flux and create the file directly in terraform. But additional to that you have to take care of accessing the file you created and also managing the triggers to run the command if a setting is changed.</li>
</ul>
<p>In general, I would not use terraform for such things. It is fine to provide infrastructure like aws resources or services which are directly bound to the infrastructure but in order to create and run services you need a provisioning tool like ansible where you can run commands like &quot;ssh-keyscan&quot; directly as module. At the end you need a stable pipeline where you run ansible (or your favorite provisioning tool) after a terraform change.</p>
<p>But if you want to use only terraform you're going to right way.</p>
