ansible / molecule

Molecule aids in the development and testing of Ansible content: collections, playbooks and roles
https://ansible.readthedocs.io/projects/molecule/
MIT License
3.84k stars 657 forks source link

molecule removes `_` from hostname: `sudo: unable to resolve host` #3351

Open robertdebock opened 2 years ago

robertdebock commented 2 years ago

HAPPY NEW YEAR! And thanks for making and maintaining Molecule. My wished for 2022: Keep up the exceptional work!

Issue Type

Molecule and Ansible details

ansible --version && molecule --version
ansible [core 2.11.1] 
  config file = None
  configured module search path = ['/Users/robertdb/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /opt/homebrew/lib/python3.9/site-packages/ansible
  ansible collection location = /Users/robertdb/.ansible/collections:/usr/share/ansible/collections
  executable location = /opt/homebrew/bin/ansible
  python version = 3.9.2 (default, Mar 26 2021, 15:28:17) [Clang 12.0.0 (clang-1200.0.32.29)]
  jinja version = 2.11.3
  libyaml = True
molecule 3.3.0 using python 3.9 
    ansible:2.11.1
    delegated:3.3.0 from molecule
    docker:0.2.4 from molecule_docker

Molecule installation method (one of):

Ansible installation method (one of):

Detail any linters or test runners used:

Desired Behaviour

Some roles have an underscore _ in the name, like ansible-role-python_pip. Molecule should not change the hostname, I think it does.

Actual Behaviour

Molecule removes the _ from the hostname. This can cause an issue with sudo:

sudo: unable to resolve host pythonpip-debian-bookwormpy39-ansible-2.11: Name or service not known

(So far, only debian:bookworm causes an issue, I guess the sudo is configured a bit more strict.)

This behaviour seems new, I checked the last few molecule releases, but could not find a related change. This makes me unsure if Molecule causes the issue or Debian (or sudo), so please let me know if you think it's something different than Molecule that causes the removal of the _ from the hostname.

(The full error.)

Reproduction

git clone git@github.com:robertdebock/ansible-role-python_pip.git
cd ansible-role-python_pip
molecule test --destroy=never
molecule login
hostname
sudo ls
mafalb commented 2 years ago

I did habe troubles with underscores in hostnames before. According to RFC 952 underscores are not valid, and I think that some versions of linux (glibc?) follow that more strictly than others.

A "name" (Net, Host, Gateway, or Domain name) is a text string up
   to 24 characters drawn from the alphabet (A-Z), digits (0-9), minus
   sign (-), and period (.).

That said, I am not sure that the issue is caused by molecule.

robertdebock commented 2 years ago

That said, I am not sure that the issue is caused by molecule.

I agree, it's not really molecule, on the other hand, I would not know how to proceed with it now...

mafalb commented 2 years ago

FWIW, I managed to trigger the sudo error without using molecule. It happens when there is no entry in /etc/hosts in the container.

I created my test container with podman run --no-hosts ... -> /etc/hosts does not exist at all, and after installing python and sudo and creating a user, when connecting there is the sudo: unable to resolve host ... errror. After creating an entry in /etc/hosts ansible connection was working.

I found a blog post about this topic https://linuxhandbook.com/sudo-unable-resolve-host/

robertdebock commented 2 years ago

Thanks @mafalb, I am seeing this issue;

Have a look at a Docker instance that Molecule created:

root@pythonpip-debian-bookworm:/# cat /etc/hosts
# (non relevant lines removed)
172.17.0.6  python_pip-debian-bookworm
root@pythonpip-debian-bookworm:/# hostname
pythonpip-debian-bookworm

I wonder if Molecule can "fix" this as I'm not sure what sets the hostname.

robertdebock commented 2 years ago

For now, I'll remove the underscores from molecule/default/molecule.yml: platforms.name.

For future readers, this is BROKEN:

platforms:
  - name: "some_name" # BROKEN, an underscore can give issues with setting the hostname.

This is WORKING:

platforms:
  - name: "somename"