Running tasks inside LXD container through ansible

Trying to figure out the best way to get ansible to do its magic within the containers. Was thinking of using ssh in each container, but then that is server-dependent. or I have to come via the proxy which seems like more work than should be necessary. Again, I am already creating containers, deleting etc… I am talking about running commands within the container with ansible not via lxc exec containername bash which I can do now. Anyone figure out the best way to do it.


Solution 1:

taken from https://docs.ansible.com/ansible/latest/collections/community/general/lxc_container_module.html#examples

you can try the following:

- name: Run a complex command within a "running" container
  community.general.lxc_container:
    name: test-container-started
    container_command: |
      apt-get update
      apt-get install -y curl wget vim apache2
      echo 'hello world.' | tee /opt/started
      if [[ -f "/opt/started" ]]; then
          echo 'hello world.' | tee /opt/found-started
      fi