csmart / ansible-role-virt-infra

Define and manage guests and networks on a KVM host with Ansible
GNU General Public License v3.0
67 stars 48 forks source link

Ubuntu guests hostnames not set properly #51

Closed numenic closed 3 years ago

numenic commented 3 years ago

When setting ubuntu Guests (tested with 20.04), guest's hostname are not properly set:

ansible 4.4.0 (core 2.11.4)

tests.yml :

tests:
  hosts:
    # simple-centos-7-[0:2]:
    #   ansible_python_interpreter: /usr/bin/python
    test-[0:1]:
      ansible_python_interpreter: /usr/bin/python
      virt_infra_state: running
      virt_infra_distro: ubuntu
      virt_infra_distro_image: ubuntu-20.04-server-cloudimg-amd64.img
  vars:

running tests.yml: ansible-playbook virt-infra.yml --limit kvmhost,tests

/etc/host is correctly modified, but /etc/hostname should also be modified (ref: https://askubuntu.com/a/1343976).

By the way, great and impressive work!

numenic commented 3 years ago

I usually use this workaround :

- name: modify /etc/hosts 127.0.1.1
  when: ansible_hostname != hostname 
  lineinfile:
    dest: /etc/hosts
    regexp: '^(127\.0\.1\.1[ \t]+).*$'
    line: '\1{{ hostname }}'
    state: present
    backrefs: yes
csmart commented 3 years ago

Hi, thanks for the issue. Interesting... I'm not sure that it's normal to add the hostname for 127.0.0.1 on a Linux system, I'm not sure why Ubuntu wants that, but I don't think it should need it...

Nevertheless, we are using cloud-init to configure the system which is what's setting all of the hostname information, so we just do whatever Ubuntu has configured cloud-init to do. I think if Ubuntu needs the system to have the hostname resolve to localhost (rather than say, what your DNS says it should be), then they probably need to configure cloud-init to do that, then you'll get it for free on boot.

Have you seen this actually cause you a problem on an Ubuntu machine? If so, it might be a good idea to put in an upstream Ubuntu bug and ask them to modify cloud-init to add the hostname to /etc/hosts also.

(And just to clarify, you mentioned that /etc/hostname does not get populated in your original comment, but your code snippet is for /etc/hosts, so I'm assuming the latter...)

I could put in an option to let you run any custom scripts that you want in cloud-init if you like. This way you could just set custom scripts as a part of your inventory, rather than a separate job?

numenic commented 3 years ago

you're right, it was /etc/hosts that is populated later on.

regarding real-world issue with this strange Ubuntu habits, I had yesterday an issue (sudo didn't work properly), but I cannot reproduce it anymore...

For now, don't bother with cloud-init stuff. Maybe we can close this, and I will reopen if I find a way to reproduce it?

Thanks anyway to have taken this in consideration, and pointed me to this cloud-init I should deep in.

csmart commented 3 years ago

Sure, can close for now. Thanks!