0
votes

I am creating an instance(virtual machine) in Oracle cloud infrastructure using terraform. I want to clone a git repository (especially, from azure devops) to that newly created instance.

Is there any terraform module to achieve this?

Or any shell/ansible scripts that can be used in provisioner to do this?

1

1 Answers

1
votes

You can make use of a terraform null_resource. With the remote-exec provisioner it is just like ssh into the box, providing credentials, and list the shell commands. The best part is the dependency model. You can instruct the null_resource to be fired off after other dependent resources are available.This example specifies a jump-host/bastion-host for the connection section. This is optional.

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"
           ]
  }

}

You will find great OCI and terraform examples by visiting this resource: https://github.com/mlinxfeld/foggykitchen_tf_oci_course

Best of luck!