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

    Unable to configure Network IP inside the VM throgh API

    Scheduled Pinned Locked Moved Infrastructure as Code
    11 Posts 4 Posters 123 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.
    • A Offline
      amititre331
      last edited by

      Hi team,

      I am unable to configure the IP inside the VM, I am not using enterprise edition and I am doing it through ansible playbook, I am attaching the playbook here. Can you help me here how to configure the IP inside the VM via API.

      I am using netplan for ubuntu write files method. I installed the xen tools as well inside the template of the vm.


      • name: Create Ubuntu VM on XOA with Static IP - FINAL FIX
        hosts: localhost
        connection: local
        gather_facts: no

        Define Variables

        vars:

        XOA/Xen Parameters (Using your provided values)

        xoa_host: "https://169.104.140.195"
        xoa_pool_uuid: "5b260402-5586-2e3f-56c1-7aa5cd801903"
        vm_name: "testing_guest-tools_Network"
        template_uuid: "9cc86438-d492-ed99-0cfc-b912d320f295"
        xoa_token: "v0kzzePGh0ZNYhIv0GsF81FEPBat7EYTLXghl6vL2nU"

        Network Parameters (Target IP: 169.144.104.200)

        static_ip: 169.104.140.201
        netmask_cidr: 26
        gateway: 169.104.140.193
        dns1: 169.104.140.7
        dns2: 8.8.4.4
        interface_name: enX0 # Confirmed name

        Cloud-Init Configuration

        cloud_init_config_yaml: |
        #cloud-config
        hostname: "{{ vm_name }}"
        manage_etc_hosts: true

        # IMPORTANT: Keep this minimal to avoid conflicts with the 'write_files' block
        network:
          version: 2
          renderer: networkd
        
        # 1. WRITE FILES: Explicitly write the static config to a high-priority file (99)
        write_files:
          - path: /etc/netplan/99-static-ansible.yaml
            permissions: '0644'
            owner: root:root
            content: |
              # This file will override 50-cloud-init.yaml
              network:
                version: 2
                ethernets:
                  {{ interface_name }}:
                    dhcp4: false
                    dhcp6: false
                    addresses: [{{ static_ip }}/{{ netmask_cidr }}]
                    routes:
                      - to: default
                        via: "{{ gateway }}"
                    nameservers:
                      addresses: [{{ dns1 }}, {{ dns2 }}]
                      search: []
        
        # 2. RUN COMMAND: Delete the conflicting file and force application
        runcmd:
          # CRITICAL: Delete the conflicting file created by the template/XOA
          - rm -f /etc/netplan/50-cloud-init.yaml || true
          - rm -f /etc/netplan/01-netcfg.yaml || true
        
          # Apply the new high-priority config (99-static-ansible.yaml)
          - netplan generate
          - netplan apply
        
          # Force a service restart to ensure the configuration is picked up
          - systemctl restart systemd-networkd
          - echo "Static IP {{ static_ip }} configured via file overwrite." >> /var/log/cloud-init-status.log
        

        tasks:

        • name: 1. Create VM on XOA via REST API
          ansible.builtin.uri:
          url: "{{ xoa_host }}/rest/v0/pools/{{ xoa_pool_uuid }}/actions/create_vm?sync=true"
          method: POST
          headers:
          Content-Type: application/json
          Accept: application/json
          Cookie: "authenticationToken={{ xoa_token }}"
          body:
          name_label: "{{ vm_name }}"
          template: "{{ template_uuid }}"
          boot: true
          # Pass the Cloud-Init YAML string
          cloud_config: "{{ cloud_init_config_yaml | to_json }}"
          body_format: json
          status_code: [200, 201]
          validate_certs: no
          register: vm_creation_response

        • name: 2. Display VM Creation API Response
          ansible.builtin.debug:
          var: vm_creation_response.json

      AtaxyaNetworkA 1 Reply Last reply Reply Quote 0
      • olivierlambertO olivierlambert moved this topic from REST API
      • olivierlambertO Offline
        olivierlambert Vates πŸͺ Co-Founder CEO
        last edited by

        I just moved the thread in the right category πŸ™‚

        A 1 Reply Last reply Reply Quote 0
        • A Offline
          amititre331 @olivierlambert
          last edited by

          @olivierlambert Thanks Olivier Can I get the resolution on this issue I am trying to resolve this issue from last 40 days but still no resolution

          1 Reply Last reply Reply Quote 0
          • olivierlambertO Offline
            olivierlambert Vates πŸͺ Co-Founder CEO
            last edited by

            You are welcome. This is a community forum, so you have to wait for people getting there on their free time to assist. Hopefully, someone will come in the next days to assist πŸ™‚

            A 1 Reply Last reply Reply Quote 0
            • AtaxyaNetworkA Online
              AtaxyaNetwork Ambassador @amititre331
              last edited by

              @amititre331 Hi !

              Can you try the API request by hand (without ansible) to see if it works ?

              Also, do you have any errors when running ansible ?

              A 1 Reply Last reply Reply Quote 1
              • C Offline
                carloum70
                last edited by carloum70

                @amititre331
                If you want to configure ip settings using cloud-init you have to use the option network_config instead of cloud_config.

                Unbrand_CreateVmBody_{
                memory	[...]
                name_description	[...]
                name_label*	[...]
                clone	[...]
                gpuGroup	[...]
                vgpuType	[...]
                autoPoweron	[...]
                vifs	[...]
                copyHostBiosStrings	[...]
                template*	[...]
                affinity	[...]
                vdis	[...]
                install	{...}
                cloud_config	[...]
                network_config	string
                boot	[...]
                destroy_cloud_config_vdi	[...]
                }
                

                By the way, do not share your tokens on the internet πŸ˜‰

                A 2 Replies Last reply Reply Quote 0
                • A Offline
                  amititre331 @AtaxyaNetwork
                  last edited by

                  @AtaxyaNetwork I didnt get any error while creating the VM, VM Created succesfully but Network is not getting configured and by Hand when I run the API and the API run successfully. but still network not configured

                  1 Reply Last reply Reply Quote 0
                  • A Offline
                    amititre331 @carloum70
                    last edited by

                    @carloum70 said in Unable to configure Network IP inside the VM throgh API:

                    network_config

                    Hi Thanks for your suggestion I will try with network_config Instead of cloud_config then will let you know what will be the response.

                    1 Reply Last reply Reply Quote 0
                    • A Offline
                      amititre331 @olivierlambert
                      last edited by

                      @olivierlambert Hi Thanks for understanding and your response.

                      1 Reply Last reply Reply Quote 0
                      • A Offline
                        amititre331 @carloum70
                        last edited by

                        @carloum70 Hello Carloum Do you have tested sample for the same so I can get the reference.

                        1 Reply Last reply Reply Quote 0
                        • C Offline
                          carloum70
                          last edited by

                          I am using terraform to setup a VM but it's the same approach for cloud-init:
                          cloud_config

                          #cloud-config
                          preserve_hostname: false
                          hostname: ${hostname} 
                          create_hostname_file: true
                          users:
                            - name: ansible
                              groups:
                                - sudo
                              sudo:
                                - ALL=(ALL) NOPASSWD:ALL
                              shell: /bin/bash
                              ssh_authorized_keys:
                                - "ssh-ed25519 somekeyl"
                                - "ssh-ed25519 otherkey"
                          

                          network_config:

                          #cloud-config
                          network:
                            version: 1
                            config:
                              - type: physical
                                name: enX0
                                subnets:
                                  - type: static
                                    address: ${ip_address}
                                    netmask: ${netmask}
                                    gateway: ${gateway}
                                    dns_nameservers:
                          %{ for dns_server in dns ~}
                                      - ${dns_server}
                          %{ endfor ~}
                          

                          Maybe a silly question: Your template supports cloud-init ?

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