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.

  • Does this answer your question? Ansible (SSH + LXD) plugin
    – AlexD
    Dec 21, 2021 at 9:27
  • Can I ask why you want to? What is it that needs doing to these containers that requires Ansible?
    – shearn89
    Dec 21, 2021 at 15:35
  • I am trying to create a project for hosting platform. For that, i am using LXD containers and Ansible. As i m having issue to run task inside LXD container through ansible. Can you help me out?
    – gzala
    Dec 21, 2021 at 15:41

1 Answer 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
    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
  • I am running lxd container and it does not seems to be working with the above commands.
    – gzala
    Dec 27, 2021 at 4:28
  • as this is a part of official documentation I suggest you open an issue on official github of ansible community.general.lxc_container module with detailed logs as to what is failing and how Jan 15 at 19:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.