I am trying to create a directory in a block device (mounted on /media/ajaved/PIROOT) using Ansible chroot connection and it fails with the following message:
redirecting (type: connection) ansible.builtin.chroot to community.general.chroot
The full traceback is:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/ansible/executor/task_executor.py", line 165, in run
res = self._execute()
File "/usr/lib/python3/dist-packages/ansible/executor/task_executor.py", line 574, in _execute
self._connection = self._get_connection(cvars, templar, current_connection)
File "/usr/lib/python3/dist-packages/ansible/executor/task_executor.py", line 953, in _get_connection
connection, plugin_load_context = self._shared_loader_obj.connection_loader.get_with_context(
File "/usr/lib/python3/dist-packages/ansible/plugins/loader.py", line 936, in get_with_context
obj.__init__(instance, *args, **kwargs) # pylint: disable=unnecessary-dunder-call
File "/usr/lib/python3/dist-packages/ansible_collections/community/general/plugins/connection/chroot.py", line 127, in __init__
if not (is_executable(chrootsh) or (os.path.lexists(chrootsh) and os.path.islink(chrootsh))):
File "/usr/lib/python3/dist-packages/ansible/module_utils/common/file.py", line 74, in is_executable
return ((stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH) & os.stat(path)[stat.ST_MODE])
FileNotFoundError: [Errno 2] No such file or directory: '/media/ajaved/PIROOT/bin/sh'
pikvm3 failed: {
"msg": "Unexpected failure during module execution: [Errno 2] No such file or directory: '/media/ajaved/PIROOT/bin/sh'",
"stdout": ""
}
Ansible is somehow failing to find the default executable /bin/sh inside chroot directory /media/ajaved/PIROOT but I checked, and the file is there already. My host OS is Ubuntu Jammy (22.04) and Ansible version is 2.10.8. I also tried with newest Ansible version 2.16.5 and the problem is still there.
On Ubuntu Focal (20.04), I don’t have this error and everything is fine. Any help is much appreciated.
Could you provide some more details, like the key parts of the inventory, playbook and role you are using? And the ownership and permissions of the chroot directory and the permissions of the user you are running Ansible as?
The chroot directory is mentioned as ansible_host in the Ansible role main.yml file above (see vars in the end of .yml file). We are first flashing with local connection and then chroot into the directory. It works fine with Ubuntu Focal.
The most likely explanation would be some sort of race condition that you’re losing under Jammy, where the mount point /media/ajaved/PIROOT exists but is not yet usable. What happens if you change your wait_for task to check that the filesystem is actually available?
- name: Waiting for SD card mount
wait_for:
path: /media/ajaved/PIROOT/bin/sh