How i can use LXC with Ansible?

Hi all!
I need send a command to lxc container from ansible
When i tried “container_command” ansible not responding
I try “command” and “shell” doesn’t have errors output but when i try wget -O && chmod +x && ./
Error in wget -O
How to run this command on all LXC containers with the same name? Help please

Hi @vanyavorg_devops,
Are you using lxd or lxc? If you are using different containers, first of all create a user which has sudo capability on the containers. And on your host machine you can handle all of the things on containers with standard ansible-playbook yaml files. But you can check the ssh connection on the host to containers first. On the other hand, you can execute bash commands on the containers you dont need to use ansible.

I don’t know LXC so the following how I experimented with LXD+Ansible which might be relevant to you.

The Ansible LXD Connection plugin basically acts like the lxc exec {containername} bash command and can run as root in the container, but I’ve not explored using other user accounts within the instance.

If I remember correctly it requires the LXD/C client installed on the Ansible controller if you want the LXD Connection plugin to be used directly on the Ansible controller, and it will use that to make the connection to the specified instance like you would manually (lxc exec {containername} bash). You can even utilize remote names as you would manually in the LXC client. In this case where you’re running Ansible under a certain account, your LXC client will need to run be configured in the context of the Ansible account and have the remote configured so that the LXD client.

This is what Stéphane explains and uses here:

You could make a normal connection to a LXD host via and run commands like this example where I needed to create a second volume for ArangoDB data for a system container that will sit on a different storage pool backed by a NVMe disk subsystem. This isn’t the recommended Ansible way. Your commands might be to lxc exec.... instead:


    - name: Use shell to issue lxc storage volume create |
        lxc storage volume create "{{ hostvars[item]['acme_lxd_disk01_pool'] }}" "{{ hostvars[item]['ansible_host'] }}_disk01" --target "{{ hostvars[item]['acme_lxd_target'] }}"
      ignore_errors: yes
      loop: "{{ groups['acmesite01_lxd_instances_pd_arangodb_linux_sc_cl01'] }}"

Hope this helps