VM with Cloud-Init

micmar

New Member
Oct 25, 2022
8
0
1
Hi,
I am trying to create VM with cloud-init configuration. VM is created using Packer and stored as VM Template on one of the nodes. Then creating new VMs with Terraform and everything works great. Then, when machines are up and done configuring using Cloud-Init, I apply Ansible. Now the configuration is great and working properly.

But here is my problem: when changing for example RAM with terraform the Cloud-Init re-runs on the VMs and ruins my configuration. Is there any way to run Cloud-Init only once, when VM is being created?
 
Sure, but this is disabling Cloud-Init after the fact and needs to be done manually in my case or with playbook. I'm looking if there is any switch/variable to tell Cloud-Init to run once and be done forever.
 
Do you not have a cloud-config file as part of your cloud-init setup?

All I do is within the user-data: runcmd: section I just place

- touch /etc/cloud/cloud-init.disabled

So the first time the system starts and the runcmd: section is run the file based disabled flag is set.
 
  • Like
Reactions: micmar
I definitely will try this.
But, as I'm building Template using Packer, wound't it disable Cloud-init too early?
 
I do not know the tools you are using well enough to say, but the runcmd: section is processed the first time a system is restarted after all the configuration work is carried out by the cloud-init. For my environments, I have ansible deployed and scripts run from within the same section, so cloud-init is no longer required.
 
  • Like
Reactions: micmar
Still fighting with this, I have tried using `rancmd`, but I'm probably placing it wrong. Could You have look at it?
Code:
#cloud-config
autoinstall:
  version: 1
  locale: en_US
  # keyboard:
  #   layout: us
  ssh:
    install-server: true
    allow-pw: true
    disable_root: true
    ssh_quiet_keygen: true
    allow_public_ssh_keys: true
  packages:
    - qemu-guest-agent
    - sudo
  storage:
    layout:
      name: direct
    swap:
      size: 0
  user-data:
    package_upgrade: false
    timezone: Europe/Berlin
    users:
      - name: user
        groups: [adm, sudo]
        lock-passwd: false
        sudo: ALL=(ALL) NOPASSWD:ALL
        shell: /bin/bash
        passwd: $2
        # - or -
        # ssh_authorized_keys:
        #   - your-ssh-key
  # write_files:
  # - path: /etc/host
  #   content: |
  #     10.0.1.1 n1 brClone
  #     10.0.1.2 br
  #   append: true
  runcmd:
    - touch /etc/cloud/cloud-init.disabled
    # - cd /etc/cloud
    # - touch cloud-init.disabled