DEV Community

Norm Dong
Norm Dong

Posted on • Originally published at beedog.cc

Learning Ansible, Proxmox and LXC, Part 1

I've been running a server at home for a few years and gone through many iterations of hardware and different software/virtualization stacks. On this iteration I landed on Proxmox which supports LXC, an interesting virtualization technology.

Now, I have been a fan of IaC and I wanted to learn Ansible for a while. I'd prefer Terraform but the Terraform module for Proxmox did not work very well when I tried it a while ago, so it's a good opportunity for me to try Ansible!

This blog series is my learning notes for using Ansible to provision LXCs (and a few VMs) and configure the software/service running in said LXCs.

Or so I thought - the series is no more, but I'm keeping the "Part 1" in the title just for the lols, here is what happened:

Step 1, I create a snippet to quickly get an LXC up, it looks like this:

- name: test playbook
  hosts: localhost

  tasks:
  - name: Create new container with minimal options
    community.general.proxmox:
      vmid: 200
      node: my-proxmox-node
      api_user: ansible-usr@pve
      api_password: 123
      api_host: 192.168.x.x
      hostname: test-lxc
      password: 456
      ostemplate: 'local:vztmpl/ubuntu-24.04-standard_24.04-2_amd64.tar.zst'
      storage: local-lvm
Enter fullscreen mode Exit fullscreen mode

Then I run ansible-playbook playbook.yml to create the LXC. So far, so good.

Step 2, the LXC is up and running but the config is quite loose. I'd like to tighten it up a bit. I add disk_volume block to the template, replacing storage. As per example in the documentation

- name: test playbook
  hosts: localhost

  tasks:
  - name: Create new container with minimal options
    community.general.proxmox:
      vmid: 200
      node: my-proxmox-node
      api_user: ansible-usr@pve
      api_password: 123
      api_host: 192.168.x.x
      hostname: test-lxc
      password: 456
      ostemplate: 'local:vztmpl/ubuntu-24.04-standard_24.04-2_amd64.tar.zst'
      disk_volume:
        storage: local-lvm
        size: 20
Enter fullscreen mode Exit fullscreen mode

What could possibly go wrong?

Step 3, Ansible complains: "disk_volume is not a valid parameter". Huh? But the doc contains the definition and examples, how could...oh, "added in community.general 9.2.0", I see. Let me upgrade my local Ansible version.

Step 3.5 I'll spare you (and myself!) the details of me trying to figure out my local module's version, things are already bad enough.

Step 4, now the error message is something like "400 Bad Request, disk_volume is not defined in schema". OK, maybe I need to upgrade and reboot my Proxmox installation as well. Too bad though, next step please!

Step 5, OK I've given up on trying to make disk_volume work. Let me revert that property and move on to the next thing, adding cores property to limit the CPU resource.

- name: test playbook
  hosts: localhost

  tasks:
  - name: Create new container with minimal options
    community.general.proxmox:
      vmid: 200
      node: my-proxmox-node
      api_user: ansible-usr@pve
      api_password: 123
      api_host: 192.168.x.x
      hostname: test-lxc
      password: 456
      ostemplate: 'local:vztmpl/ubuntu-24.04-standard_24.04-2_amd64.tar.zst'
      storage: local-lvm
      cores: 4
Enter fullscreen mode Exit fullscreen mode

Works like a charm. If we just ignore the whole disk_volume thing for a second...

Step 6, I want to try updating the LXC through Ansible. I change cores from 4 to 6, save, rerun, surely this should update my LXC's CPU to 6, right? Right?

PLAY RECAP **********************************************************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
Enter fullscreen mode Exit fullscreen mode

Step 7, I think it's time to revisit Terraform. Wish me luck!

Top comments (2)

Collapse
 
janvv profile image
Jan van Veldhuizen

No... when the lxc is already there, then it won't update anything. There is a separate option 'update' for that. Breaking the whole omnipotence idea of Ansible :-( Annoying...

By the way, I see you wanted to have a 20G disk in the earlier version of the playbook. How did you do that when disk_volume didn't work?

Collapse
 
janvv profile image
Jan van Veldhuizen

Oh... just after I posted this I found out that this works disk: <storage>:<size>
Wow, finally, after hours of struggling the same way you were