I am attempting to utilize the LXD Remote Connection in Ansible to setup and configure LXC containers on a server but I am running into an issue.
After setting up the inventory file below, creating the LXC container (which does work fine), and ensuring my computer I am running ansible on has the server added as a LXD remote I get an error when running a task against the LXC container.
Hi… so znas is the name of the container in LXD ?
If Ansible is running as a different user on the controller node it will need to have it’s own LXD Remote added… in the user’s context, otherwise it runs the LXD connection as a user without the Remote you set up for it.
Check whether the Remote name matches, in the user context that it runs in.
Check whether the container name in LXD matches “znas” in lxc list
Sorry starting from the bottom based on my knowledge acquired by the Homer DOH method
yes, znas is the container that has successfully been created using ansible. So that part is done and easy to replicate at least.
As for the user and Remote, I’m not sure how to check that. For example the user Ansible runs as to deploy/run items on the server is “zuser” which does have sudo privilege’s set up correctly. At the same time LXD in the Ansible script is installed using the zuser account (not root).
It does seem like the error is saying the root user is failing to reach the remote container but when changing ansible_user to zuser that also fails and tells me lxd does not support remote users.
“lxc list” does work correctly when running it on the control pc, at least when running “sudo lxc list “remotename”:”
Any advice on how to check some of the items you listed would be great (types of commands to test for them). Thanks for the ideas so far.
do you see the kubuntu remote you are referencing in ansible_lxd_remote ?
It sounds like the user that Ansible is running as (zuser) does not have the LXD Remote setup… i.e. you’ve set it up for your account or the root account but not for the zuser account.
sudo: unknown user zuser
sudo: error initializing audit plugin sudoers_audit
Looks to me like your hunch was right and to add to that, on the control node I do not have zuser as a user (this was intentional). Is there a way to add the remote lxd server (kubuntu) with zuser if that account does not exist on the control node?
I haven’t seen any documentation on doing a remote add with a user on the remote machine. This is a first I’ve seen “lxc list” with a “-u” flag used as well.
So what I understand is that whatever the user that Ansible runs as on the control node, that user account has to have the LXC client configured for it on the control node… I was assuming this was your zuser on the control node.
When LXC Connection connects to your container it’ll do it via the LXD client (lxc) and get into the container as the root user by default… like when you are on the LXD host and run lxc exec znas bash.
Imagine you are Ansible on your control node and you want to open a terminal session in your LXD container, you need to:
have the lxc client and LXD Connection
in lxc set up a remote to your LXD host (looks like you’re wanting to call it kubuntu)
open a session in your container by running lxc exec kubuntu:znas bash
LXD Connection effectively does #3 for you in the above list, putting you into the container as the root user by default, in conjunction with your vars below the containers group, which all looks good to me.
If you are running the ansible-playbook command as yourself or as root (sudo) on the control node, then it could be that this account you’re using doesn’t have lxc remote set up… e.g.:
if you’re running sudo ansible-playbook ... then on the control node try running sudo lxc exec kubuntu:znas bash to see if you get into the container fine. If you do then something else is amiss here.
if you are logged into the control node as zerogravitas and run the anisble-playbook... command as is (no sudo), then on the control node try running lxc exec kubuntu:znas bash to see if you get into the container fine. If you do then something else is amiss here.
Now we’re getting closer I think. Thank you for the detailed explanation. It makes perfect sense after thinking about Ansible, LXD, and the users running the different pieces of software.
Knowing that, I realized that I was logged in as say zerogravitas in the control node and was running ansible-playbook as that user (ssh setup for kubuntu in the Ansible Config is set to log in as zuser which I think is fine).
At the same time the LXC setup/commands required “sud0 lxc list” meaning it was running as privileged. I fixed that and now can run both ansible-playbook and lxc list kubuntu:znas bash without issues under zerogravitas. It is able to remote into the znas container successfully.
Having said that, attempting to run the playbook, it still provides the same exact error: unreachable with the same output as original. Any further ideas/thoughts?
After looking at Stephen Graber’s/LXD Youtube video again I tried something extremely basic which I thought was already done (as usual, always start with the most basic steps when investigating these things).
I re-installed the Ansible Community.General Collection (ansible-galaxy collection install community.general -f). After making sure that was up to date, then re-running the ansible playbook with the znas tag everything worked perfect. So for some reason that collection was not up to date on my controller node.
Thank you @mratt & @cemzafer for helping dig through these setup items and making me think about how I have both systems setup.
By the way, this project and community is awesome. After using docker for quite a few years I was looking for a more permanent solution for long-run services/applications and LXD/LXC fills that exact need. I think with more work on the documentation side, this could very well rival docker containers for most home users and self-hosters.
I glad you have resolved the issue, you dont need to define a group name wheel, just create a specific user for ansible and add to sudo group or wheel group depends on your distribution.
Regards.
Just made this account to say BIG THANK YOU to ZeroGravitas. YOU ARE SO AWESOME i was having trouble with: "Ansible_loop_var attribute error: nonetype object has no attribute items" leading to lxd_container " error when installing xroad using ansible playbook
Here’s the log
line 99, in _ansiballz_main invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)
File "/home/vboxuser/.ansible/tmp/ansible-tmp-1728036128.1841958-3528-12931979 1180256/Ansiballz_lxd_container.py", line 47, in invoke_module runpy.run_module(mod_name='ansible_collections.community.general.plugins.mod
ules.lxd_container', init_globals-dict(_module_fqn='ansible_collections.communit y.general.plugins.modules.lxd_container', _modlib_path=modlib_path), File "/usr/lib/python3.10/runpy.py", line 224, in run_module return _run_module_code(code, init_globals, run_name, mod_spec) File "/usr/lib/python3.10/runpy.py", line 96, in _run_module_code _run_code(code, mod_globals, init_globals,
File "/usr/lib/python3.10/runpy.py", line 86, in _run_code exec(code, run_globals)
File "/tmp/ansible_lxd_container_payload_g1eotpsb/ansible_lxd_container_payloa d.zip/ansible_collections/community/general/plugins/modules/lxd_container.py", l ine 901, in <module>