Ansible and community.general.lxc_container

I’m trying to connect to a remote host and then jump into a lxc container. I’m using Ubuntu 20.04 and I have installed lxc using snap.

Here’s my Ansible task:

---
- hosts: all
  become: yes
  become_method: sudo

  tasks:
    - name: Run a complex command within a "running" container
      community.general.lxc_container:
        name: mycontainer
        container_command: |
          apt-get update
          touch we_have_access.txt

I’m stumped at this error message:

{"changed": false, "msg": "Failed to find required executable \"lxc-create\" in paths: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin"}

I thought lxc-create was an old command as there doesn’t seem to be any lxc-utils in snap. Is the Ansible package maintained up to the latest version of lxc or should I go back to using the lxc version from apt that’s supported by lxc-utils?

ansible version:

ansible [core 2.11.6] 
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/home/cgibson/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3/dist-packages/ansible
  ansible collection location = /home/cgibson/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/bin/ansible
  python version = 3.8.10 (default, Sep 28 2021, 16:10:42) [GCC 9.3.0]
  jinja version = 2.10.1
  libyaml = True

lxc version:
4.0.8

Thanks!
Casey.

I solved this bit pretty quickly. On Ubuntu you can still install lxc-utils along with these packages:
lxctl
lxc-templates

It was an underlining issue that was solved with the above packages.

I’m struggling to get the above to work where it comes to the container_command. I’m not able to get any of the commands to run however it says that all the tasks have completed successfully.

In the documentation is says:

When using “container_command” a log file is created in the /tmp/ directory which contains both stdout and stderr of any command executed.

I’ve checked the host and container, neither have anything useful in /tmp that can give me a hint or any errors of what’s going on.

Is there any known reasons ‘container_command’ would fail to run?
Thanks!

Hello!

I also try to find answers for this questions, but no one respond :frowning: I try to execute command in LXC container.
I have playbook:

- name: Run a command in a container and ensure its it in a "frozen" state.
  community.general.lxc_container:
    name: Nginx
    container_command: |
      echo 'hello world.' | tee /opt/frozen

But got error:
root@Ansible:/etc/ansible/playbooks# ansible-playbook test_commands.yaml

PLAY [devel-lxd01] ****************************************************************************************************************************

TASK [Gathering Facts] ************************************************************************************************************************
ok: [devel-lxd01]

TASK [Run a command in a container and ensure its it in a "frozen" state.] ********************************************************************
fatal: [devel-lxd01]: FAILED! => {"changed": false, "msg": "Failed to find required executable \"lxc-create\" in paths: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin"}

PLAY RECAP ************************************************************************************************************************************
devel-lxd01                : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0

Did you solved the issue?

Hi,
That community module should be community.general.lxd_container
Here is the related link, https://docs.ansible.com/ansible/latest/collections/community/general/lxd_container_module.html
Humm, if that containers are lxd. :thinking:
Regards.

@unico,
I suppose in your case, have you installed lxc-utils package.
Regards.

I installed lxc-utils package.

root@Ansible:~# which lxc-ls
/usr/bin/lxc-ls

- name: Run a command in a container and ensure its it in a "frozen" state.
  community.general.lxc_container:
    name: Nginx
    container_command: |
      echo 'hello world.' | tee /opt/frozen

TASK [Run a command in a container and ensure its it in a "frozen" state.] ********************************************************************
fatal: [devel-lxd01]: FAILED! => {"changed": false, "msg": "Failed to find required executable \"lxc-create\" in paths: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin"}

PLAY RECAP ************************************************************************************************************************************
devel-lxd01                : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0   

When I use community.general.lxd_container:

- name: Run a command in a container and ensure its it in a "frozen" state.
  community.general.lxd_container:
    name: Nginx
    container_command: |
      echo 'hello world.' | tee /opt/frozen

I get other error

TASK [Run a command in a container and ensure its it in a "frozen" state.] ********************************************************************
fatal: [devel-lxd01]: FAILED! => {"changed": false, "msg": "Unsupported parameters for (community.general.lxd_container) module: container_command. Supported parameters include: config, client_key (key_file), target, state, url, ephemeral, architecture, snap_url, source, type, wait_for_ipv4_addresses, timeout, force_stop, name, profiles, devices, client_cert (cert_file), trust_password, ignore_volatile_options."}

PLAY RECAP ************************************************************************************************************************************
devel-lxd01                : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0

Hi @unico,
I think my bad, if you are using lxc instead of lxd then you should use community.general.lxc_container
Regards.