如何使用 terrafrom 在远程实例中克隆 git 存储库?

问题描述

我正在使用 terraform 在 Oracle 云基础架构中创建一个实例(虚拟机)。 我想将 git 存储库(尤其是从 azure devops)克隆到新创建的实例。

是否有任何 terraform 模块可以实现此目的?

或者任何可以在供应商中使用的 shell/ansible 脚本来做到这一点?

解决方法

您可以使用地形 null_resource。使用 remote-exec 配置器,它就像 ssh 进入盒子一样,提供凭据,并列出 shell 命令。最好的部分是依赖模型。您可以指示在其他依赖资源可用后触发 null_resource。此示例为连接部分指定跳转主机/堡垒主机。这是可选的。

resource "null_resource" "demo_webserver1_httpd" {
 depends_on = [oci_core_instance.demo_webserver1,oci_core_instance.demo_bastionserver,null_resource.demo_webserver1_shared_filesystem]
 provisioner "remote-exec" {
        connection {
                type     = "ssh"
                user     = "opc"
                host     = data.oci_core_vnic.demo_webserver1_vnic1.private_ip_address
                private_key = file(var.private_key_oci)
                script_path = "/home/opc/myhttpd.sh"
                agent = false
                timeout = "10m"
                bastion_host = data.oci_core_vnic.demo_bastionserver_vnic1.public_ip_address
                bastion_port = "22"
                bastion_user = "opc"
                bastion_private_key = file(var.private_key_oci)
        }

  inline = ["echo '== 1. Installing HTTPD package with yum'","sudo -u root yum -y -q install httpd","echo '== 2. Creating /sharedfs/index.html'","sudo -u root touch /sharedfs/index.html","sudo /bin/su -c \"echo 'Welcome to demo.com! These are both WEBSERVERS under LB umbrella with shared index.html ...' > /sharedfs/index.html\"","echo '== 3. Adding Alias and Directory sharedfs to /etc/httpd/conf/httpd.conf'","sudo /bin/su -c \"echo 'Alias /shared/ /sharedfs/' >> /etc/httpd/conf/httpd.conf\"","sudo /bin/su -c \"echo '<Directory /sharedfs>' >> /etc/httpd/conf/httpd.conf\"","sudo /bin/su -c \"echo 'AllowOverride All' >> /etc/httpd/conf/httpd.conf\"","sudo /bin/su -c \"echo 'Require all granted' >> /etc/httpd/conf/httpd.conf\"","sudo /bin/su -c \"echo '</Directory>' >> /etc/httpd/conf/httpd.conf\"","echo '== 4. Disabling SELinux'","sudo -u root setenforce 0","echo '== 5. Disabling firewall and starting HTTPD service'","sudo -u root service firewalld stop","sudo -u root service httpd start"
           ]
  }

}

访问以下资源,您将找到很棒的 OCI 和 terraform 示例:https://github.com/mlinxfeld/foggykitchen_tf_oci_course

祝你好运!