# hostname in chroot

**URL:** <https://forum.ansible.com/t/hostname-in-chroot/38716>\
**Category:** Ansible Project\
**Created:** [March 14, 2024, 5:35pm UTC](https://forum.ansible.com/t/hostname-in-chroot/38716 "2024-03-14T17:35:38Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mdidomenico](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@mdidomenico](https://forum.ansible.com/u/mdidomenico)\
**Post date:** [March 14, 2024, 5:35pm UTC](https://forum.ansible.com/t/hostname-in-chroot/38716/1 "2024-03-14T17:35:38Z")

</div>

i have a series of playbooks that use ansible\_hostname to determine whether tasks should run. on a host these work fine as one would expect, but when running ansible inside a chroot environment with /proc,sys,dev mounted. ansible pulls the hostname from the host outside of the chroot into ansible\_hostname,ansible\_fqdn,ansible\_nodename variables

is there a reliable way to tell ansible, this is what the hostname name regardless outside factors?

i tried settings facts to override the three variables or tricking it through the inventory, but ‘ansible -m setup localhost’ inside the chroot always seems to grab the hostname from outside the chroot

i’m likely doing something wrong, but i’m not sure what

thanks

---

<div class="post-metadata">

**Author:** ![mdidomenico](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@mdidomenico](https://forum.ansible.com/u/mdidomenico)\
**Post date:** [March 15, 2024, 1:29pm UTC](https://forum.ansible.com/t/hostname-in-chroot/38716/2 "2024-03-15T13:29:26Z")

</div>

for what it’s worth i figured out a solution to this using namespaces. i’ll leave the full script upto the reader, but maybe this will help someone later

unshare --uts= chroot /bin/bash

once in the new namespace, you can change the hostname and it does not affect the base system, which i verified with

- hostname outside the chroot  
hosta

- inside the chroot

ansible -m setup localhost | egrep “hostname|nodename|fqdn”  
ansible\_hostname = “hostb”  
ansible\_fqdn = “hostb”  
ansible\_nodename = “hostb”
