How to use Ansible to manage remote lxc containers?

How can I use Ansible to execute tasks on lxc containers on a remote server?

We use Ansible to deploy our code on several servers (physical and virtual machines).

Until now, every instance had a public ip address and a running ssh server, so everything worked like a charm. But recently, we had to deploy on two lxc containers on a remote server.

The two containers are natted and don't run a ssh server (and we'd like to keep it that way). I can only connect to them using ssh to reach the physical host then lxc-attach'ing to them.

The only way I found is a custom plugin that was never updated for the latest Ansible 2.0 version. I also reached to the mailing list with no result so far.

Has anybody ever succeeded in using Ansible in such a configuration?


Solution 1:

Try the ansible-lxc-ssh connection plugin from Pierre Chifflier, which works with Ansible 2.x. I put the plugin in the default location defined in ansible.cfg, /usr/share/ansible_plugins/connection_plugins/

In the inventory hosts file, /etc/ansible/hosts, I put the following

[containers]
container_01 ansible_host=lxc_server ansible_connection=lxc_ssh ansible_ssh_extra_args=container_01

Note that you have to pass the container name as an ssh extra arg. Be sure to replace lxc_server with the name of your lxc host.