Creating Container with Ansible

Hello there!
I have been experimenting with making a container with Ansible in WSL.
I am experiencing problems with an unclear (to me) error message.
Current message is “not found”.
I tried creating a container with the example code in the module examples using:

- name: Create a started container
  community.general.lxd_container:
    name: mycontainer
    url: "unix:/var/lib/incus/unix.socket"
    ignore_volatile_options: true
    state: started
    source:
      type: image
      mode: pull
      # Provides Ubuntu minimal images
      server: https://cloud-images.ubuntu.com/minimal/releases/
      protocol: simplestreams
      alias: "22.04"
    profiles: ["default"]
    wait_for_ipv4_addresses: true
    timeout: 600
  connection: local
  delegate_to: localhost

with some extra info in the error message being:

The full traceback is:
  File "/tmp/ansible_community.general.lxd_container_payload_p34j783m/ansible_community.general.lxd_container_payload.zip/ansible_collections/community/general/plugins/modules/lxd_container.py", line 744, in run
    action()
  File "/tmp/ansible_community.general.lxd_container_payload_p34j783m/ansible_community.general.lxd_container_payload.zip/ansible_collections/community/general/plugins/modules/lxd_container.py", line 612, in _started
    self._create_instance()
  File "/tmp/ansible_community.general.lxd_container_payload_p34j783m/ansible_community.general.lxd_container_payload.zip/ansible_collections/community/general/plugins/modules/lxd_container.py", line 555, in _create_instance
    self.client.do('POST', url, config, wait_for_container=self.wait_for_container)
  File "/tmp/ansible_community.general.lxd_container_payload_p34j783m/ansible_community.general.lxd_container_payload.zip/ansible_collections/community/general/plugins/module_utils/lxd.py", line 81, in do
    resp_json = self._send_request(method, url, body_json=body_json, ok_error_codes=ok_error_codes, timeout=timeout)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/tmp/ansible_community.general.lxd_container_payload_p34j783m/ansible_community.general.lxd_container_payload.zip/ansible_collections/community/general/plugins/module_utils/lxd.py", line 115, in _send_request
    self._raise_err_from_json(resp_json)
  File "/tmp/ansible_community.general.lxd_container_payload_p34j783m/ansible_community.general.lxd_container_payload.zip/ansible_collections/community/general/plugins/module_utils/lxd.py", line 124, in _raise_err_from_json
    raise LXDClientException(self._get_err_from_resp_json(resp_json), **err_params)
fatal: [localhost]: FAILED! => changed=false
  actions: []
  diff:
    after:
      instance:
        profiles:
        - default
        source:
          alias: '22.04'
          mode: pull
          protocol: simplestreams
          server: https://cloud-images.ubuntu.com/minimal/releases/
          type: image
      state: started
    before:
      instance: {}
      state: absent
  invocation:
    module_args:
      architecture: null
      client_cert: null
      client_key: null
      config: null
      devices: null
      ephemeral: null
      force_stop: false
      ignore_volatile_options: true
      name: mycontainer
      profiles:
      - default
      project: null
      snap_url: unix:/var/snap/lxd/common/lxd/unix.socket
      source:
        alias: '22.04'
        mode: pull
        protocol: simplestreams
        server: https://cloud-images.ubuntu.com/minimal/releases/
        type: image
      state: started
      target: null
      timeout: 600
      trust_password: null
      type: container
      url: unix:/var/lib/incus/unix.socket
      wait_for_container: false
      wait_for_ipv4_addresses: true
  msg: not found

Any direction anyone could point me in? Thanks in advance!

Kind regards

This is showing you’re trying to use the Ubuntu image server which doesn’t provide Incus images, so that may be at least one part of the issue. It’s also possible that this plugin uses the legacy container API, I can’t tell from the errors above.

Well, I was actually trying to create an Alma container, but because that did not work I thought about using the example code which gave me the same error.

My actual piece of code was this:

community.general.lxd_container:
    name: "alma"
    state: started
    url: "unix:/var/lib/incus/unix.socket"
    source:
      type: image
      mode: pull
      server: "https://images.linuxcontainers.org/"
      protocol: simplestreams
      alias: "almalinux/9/cloud"
    wait_for_container: true
    wait_for_ipv4_addresses: true
  register: state
  connection: local
  delegate_to: localhost 

This gives the same error. I even did a full reinstallation of WSL and Incus (and redid the init) but ended up with the same error. I can manually create an Alma container so it seems that Incus is working correctly.

Unfortunately the stack trace is rather useless to figure out what’s going on.

It could be that it’s using the old /1.0/containers/XYZ endpoint and so doesn’t work with Incus as we never had that legacy endpoint. If that’s the case all the code needs is an update to use /1.0/instances instead which would then work on LXD and Incus.

I would also like to experiment with Ansible to create and provision Incus containers. Which version of Ansible and which Incus Ansible plugin are you using?

Changing from /1.0/containers to /1.0/instances, is that something I can do by myself? If so, how?

For Ansible in combination with Incus I’m using:
Ansible [core 2.16.3]
Incus 6.0.0

Looking at community.general/plugins/modules/lxd_container.py at main · ansible-collections/community.general · GitHub it seems to have the correct logic.

This change dates from 4 months ago, so maybe your system doesn’t have it yet.

You could look for lxd_container.py on your system and then manually apply that part of the logic.

Hello Stéphane,
Thank you very much for thinking along. I checked the file which seems to be up-to-date with the correct “/1.0/containers” set, so the problem is somewhere else. Any other idea what could cause the issue? Thank you for your time and work.

Does Ansible Core support Incus directly or must you install a plugin?

The LXD container module should work for Incus too. You can find this via:
https://docs.ansible.com/ansible/latest/collections/community/general/lxd_container_module.html
You do have to change the socket and server. You can see my previous post to see what should be the input.