XCP-ng
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Register
    • Login

    Terraform with cloud-init

    Scheduled Pinned Locked Moved Xen Orchestra
    25 Posts 7 Posters 8.0k Views 5 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • M Offline
      m4xm0rris
      last edited by

      Hi all,

      New to the IaC world, trying to get Terraform to create a couple of VMs through Xen Orchestra but having a little trouble with cloud-init functionality. Network config gets parsed fine and the expected IP is applied.

      However using the cloud-init user config that I usually use on the XO Web UI does not work. Within this cloud-init config, I'm using the name variable as shown below:
      f9903baf-0bc1-487d-b397-872a012880c0-image.png

      However it appears when this is used via Terraform, the variable does not get parsed and the cloud config drive ends up being generated and given to the VM as

      #cloud-config
      hostname: {name}

      If anyone can help with this, would be much appreciated.

      D 1 Reply Last reply Reply Quote 0
      • olivierlambertO Offline
        olivierlambert Vates 🪐 Co-Founder CEO
        last edited by olivierlambert

        Hello!

        Hmm likely a question for @ddelnano 🤔

        1 Reply Last reply Reply Quote 0
        • D Offline
          ddelnano Terraform Team @m4xm0rris
          last edited by ddelnano

          @m4xm0rris Xen Orchestra's web application is doing this templating on the client side (reference). Since terraform already has a nice interface for templating (using its template_file data source), you can do the substitution yourself.

          Here is an example below. Note: this code is not tested but was pulled from an internal project I have.

          # template_file.tpl
          #cloud-config
          
          hostname: ${name}
          
          # any other cloud-config you need
          [...]
          
          # vm.tf
          data "template_file" "cloud_config" {
            template = file("${path.module}/template_file.tpl")
            vars = {
              name = "your_hostname_value"
            }
          }
          
          resource "xenorchestra_cloud_config" "cloud_config" {
            name = "cloud_config"
            # This performs the templating
            template = data.template_file.cloud_config.rendered
          }
          
          resource "xenorchestra_vm" "vm" {
            [ ... ]
            cloud_config = xenorchestra_cloud_config.cloud_config.template
            
          }
          
          

          Please share your terraform code if you would like more specific advice on how to recreate exactly what you are trying to do.

          M 1 Reply Last reply Reply Quote 2
          • M Offline
            m4xm0rris @ddelnano
            last edited by

            Thanks @ddelnano , that worked perfectly!

            Appreciate the help!

            1 Reply Last reply Reply Quote 1
            • olivierlambertO Offline
              olivierlambert Vates 🪐 Co-Founder CEO
              last edited by

              Interesting indeed, we should probably document this 🙂 If you have time @ddelnano obviously 🙂

              1 Reply Last reply Reply Quote 0
              • M Offline
                m4xm0rris
                last edited by

                Just stumbled upon this provider too which may offer a more streamlined approach, not that I am familiar enough with Terraform yet to say this for sure: https://github.com/hashicorp/terraform-provider-cloudinit

                D 1 Reply Last reply Reply Quote 0
                • D Offline
                  ddelnano Terraform Team @m4xm0rris
                  last edited by

                  I created terraform-provider-xenorchestra #181 to track updating the docs.

                  Cool, I didn't realize that there was a cloudinit terraform provider :). I'll be giving that a try and may use that in the examples if its better than using template_file directly.

                  M D 2 Replies Last reply Reply Quote 1
                  • M Offline
                    m4xm0rris @ddelnano
                    last edited by

                    @ddelnano Yeah, I only found it as template_file is deprecated apparently and as it hasn't seen any updates, I couldn't use the code you posted from my M1 Macbook 😞

                    Anyways, thanks for the help!

                    D 1 Reply Last reply Reply Quote 0
                    • D Offline
                      ddelnano Terraform Team @m4xm0rris
                      last edited by

                      @m4xm0rris it looks like there is a builtin function to do templating now (templatefile). Apologies, it's been a while since I've written any new templated terraform code.

                      1 Reply Last reply Reply Quote 1
                      • M Offline
                        m4xm0rris
                        last edited by

                        In case anyone comes here looking for an example (as it took me days of banging head against the proverbial wall to get this working with templatefile function):

                        cloud_config.tftpl

                        #cloud-config
                        hostname: ${hostname}
                        user: ops-user
                        ssh_authorized_keys:
                          - OPS_USER_SSH_KEY
                        manage_etc_hosts: true
                        fqdn: ${hostname}.${domain}
                        package_upgrade: true
                        users:
                         - default
                        

                        cloud_network_config.tftpl

                        #cloud-config
                        version: 1
                        config:
                            - type: physical
                              name: eth0
                              subnets:
                              - type: static
                                address: '${ip}'
                                netmask: '255.255.255.0'
                                gateway: '10.1.50.1'
                        

                        vm.tf

                        resource "xenorchestra_vm" "unc-lab" {
                        ......
                         cloud_config = templatefile("cloud_config.tftpl", {
                          hostname = "unc-lab"
                          domain = "morris.lan"
                        })
                        cloud_network_config = templatefile("cloud_network_config.tftpl", {
                          ip = "10.1.55.34"
                        })
                        .......
                        

                        Thanks @ddelnano for your help!

                        D B D 3 Replies Last reply Reply Quote 5
                        • D Offline
                          ddelnano Terraform Team @ddelnano
                          last edited by

                          I was able to confirm that using the cloudinit data source does work with Xen Orchestra.

                          Here is the following terraform code that I used (essential pieces like SR and network are excluded for the example):

                          data "cloudinit_config" "cloud_config" {
                            gzip = false
                            base64_encode = false
                          
                            part {
                              content_type = "text/cloud-config"
                              content = <<EOF
                          users:
                            - name: ddelnano
                              sudo: ALL=(ALL) NOPASSWD:ALL
                              ssh_import_id:
                                - gh:ddelnano
                          
                          packages:
                          - make
                          - build-essential
                          
                          runcmd:
                          - echo 'this is a test'
                          EOF
                            }
                          }
                          
                          resource "xenorchestra_vm" "vm" {
                            memory_max = 2147467264
                            cpus = 1
                            name_label = "XO terraform tutorial"
                            template = data.xenorchestra_template.vm_template.id
                            cloud_config = data.cloudinit_config.cloud_config.rendered
                          
                            network {
                              network_id = data.xenorchestra_network.network.id
                            }
                          
                            disk {
                              sr_id = data.xenorchestra_sr.sr.id
                              name_label = "VM root volume"
                              size = 4294967296
                            }
                          }
                          
                          

                          This creates a VM and runs the cloudinit I specified, however, it doesn't allow you to do any templating. So you would still need to use the templatfile function built into Terraform.

                          I'm going to update the docs to use the templatefile function since that is the most streamlined approach.

                          1 Reply Last reply Reply Quote 1
                          • D Offline
                            ddelnano Terraform Team @m4xm0rris
                            last edited by

                            @m4xm0rris thanks for posting your solution. Would you mind reviewing my changes to the terraform registry documentation (here)?

                            Since I've been working with terraform long before Xen Orchestra, having your opinion on the documentation would be valuable 🙂

                            M 1 Reply Last reply Reply Quote 0
                            • ppikna97P Offline
                              ppikna97
                              last edited by

                              Hi, I have question to this topic. I figured out to use templatefiles in terraform to provision my VMs. But when I try to provision network alongside with userdata configuration (2 templatefiles) it did not work. It just apply network configuration. My use case was to firstly configure static networking and than install some packages, hostname and so on. I was resolving this few months ago, but did not understand how execution sequence works or if it is even possible. Cloud-init official documentation is not very clear.

                              I would appreciate any help with this.

                              M 1 Reply Last reply Reply Quote 0
                              • M Offline
                                m4xm0rris @ddelnano
                                last edited by

                                @ddelnano Sir, the only thing I could point out from that pull request is the block in your vm.md where you are templating the variables in a separate block.

                                "

                                # Template the cloudinit if needed
                                template = templatefile("cloud_config.tftpl", {
                                  hostname = "your-hostname"
                                  domain = "your.domain.com"
                                })
                                

                                "
                                I have no where near enough experience with Terraform to tell if makes any practical difference, but I am able to redner the template inline with the rest of my VM resource definition. eg.

                                cloud_network_config = templatefile("cloud_network_config.tftpl", {
                                   ip = "10.1.55.37"
                                   netmask = var.network["netmask"]
                                   gateway = var.network["gateway"]
                                 })
                                

                                Just a little tidier IMO, but again have no idea if the way you have done this is better practice etc.

                                Thanks 🙂

                                1 Reply Last reply Reply Quote 0
                                • M Offline
                                  m4xm0rris @ppikna97
                                  last edited by

                                  @ppikna97 Hi! How are you calling the templates in your VM resources? As you can see above from my examples I'm simply pulling both user data and network inline with the rest of my config e.g.

                                  cloud_config = templatefile("cloud_config.tftpl", {
                                    hostname = "bw-lab"
                                    domain = var.network["domain"]
                                  })
                                  cloud_network_config = templatefile("cloud_network_config.tftpl", {
                                    ip = "10.1.55.37"
                                    netmask = var.network["netmask"]
                                    gateway = var.network["gateway"]
                                  })
                                  
                                  ppikna97P 1 Reply Last reply Reply Quote 1
                                  • ppikna97P Offline
                                    ppikna97 @m4xm0rris
                                    last edited by

                                    @m4xm0rris thanks. I was using template_file data source with xenorchestra_cloud_config data source like this:

                                    905476d3-fd92-4cf1-8d10-2611cd7c8a31-image.png

                                    But I found answer to both of us... Some video linked me here templatefile vs template_file. Resulting that it is better to use templatefile function to match terraform version and for working with different data types. But in the essence templatefile and template_file do the same thing.

                                    M 1 Reply Last reply Reply Quote 2
                                    • M Offline
                                      m4xm0rris @ppikna97
                                      last edited by

                                      @ppikna97 Np, I was also using template_file originally but as its now deprecated there have been no builds released for M1 based Mac (leading/forcing me down the templatefile function route).

                                      1 Reply Last reply Reply Quote 0
                                      • B Offline
                                        bxen @m4xm0rris
                                        last edited by

                                        @m4xm0rris This was very helpful. Thank you!

                                        1 Reply Last reply Reply Quote 0
                                        • D Offline
                                          dydomby @m4xm0rris
                                          last edited by

                                          @m4xm0rris thanks, it helped much.

                                          1 Reply Last reply Reply Quote 1
                                          • olivierlambertO Offline
                                            olivierlambert Vates 🪐 Co-Founder CEO
                                            last edited by

                                            Hello there!

                                            If you think some extra doc would be important to avoid searching for the solution, please do so 🙂

                                            antoniolfdacruzA 1 Reply Last reply Reply Quote 0
                                            • First post
                                              Last post