diff --git a/ansible/hostname.yml b/ansible/hostname.yml new file mode 100644 index 000000000..a10c04c6f --- /dev/null +++ b/ansible/hostname.yml @@ -0,0 +1,18 @@ +--- +- name: Ensure hostname is configured + hosts: seed-hypervisor:seed:overcloud:infra-vms:ansible-control + max_fail_percentage: >- + {{ hostname_max_fail_percentage | + default(host_configure_max_fail_percentage) | + default(kayobe_max_fail_percentage) | + default(100) }} + tags: + - hostname + tasks: + - name: Ensure hostname is set + become: true + hostname: + name: "{{ kayobe_custom_hostname }}" + use: "systemd" + when: + - kayobe_customize_hostnames | bool diff --git a/ansible/inventory/group_vars/all/hostname b/ansible/inventory/group_vars/all/hostname new file mode 100644 index 000000000..70104f7b0 --- /dev/null +++ b/ansible/inventory/group_vars/all/hostname @@ -0,0 +1,9 @@ +--- +############################################################################### +# Kayobe custom hostname configuration. + +# Whether to set custom hostnames. Default is false. +kayobe_customize_hostnames: false + +# Custom hostname to set. Default is "{{ inventory_hostname }}". +kayobe_custom_hostname: "{{ inventory_hostname }}" diff --git a/ansible/overcloud-host-configure.yml b/ansible/overcloud-host-configure.yml index 7faf3ccbb..2af8fbd14 100644 --- a/ansible/overcloud-host-configure.yml +++ b/ansible/overcloud-host-configure.yml @@ -15,6 +15,7 @@ - import_playbook: "network.yml" - import_playbook: "firewall.yml" - import_playbook: "fail2ban.yml" +- import_playbook: "hostname.yml" - import_playbook: "etc-hosts.yml" - import_playbook: "tuned.yml" - import_playbook: "sysctl.yml" diff --git a/ansible/roles/etc-hosts/tasks/etc-hosts.yml b/ansible/roles/etc-hosts/tasks/etc-hosts.yml index b90751ec2..dadaa6782 100644 --- a/ansible/roles/etc-hosts/tasks/etc-hosts.yml +++ b/ansible/roles/etc-hosts/tasks/etc-hosts.yml @@ -9,6 +9,7 @@ when: - etc_hosts_gather_facts | default(true) - etc_hosts_hosts | length > 0 + - not kayobe_customize_hostnames | bool - name: Ensure localhost in /etc/hosts lineinfile: @@ -27,7 +28,7 @@ - name: Ensure hostname does not point to 127.0.1.1 in /etc/hosts lineinfile: dest: /etc/hosts - regexp: "^127.0.1.1\\b.*\\s{{ ansible_facts.hostname }}\\b" + regexp: "^127.0.1.1\\b.*\\s{{ kayobe_custom_hostname if (kayobe_customize_hostnames | bool) else ansible_facts.hostname }}\\b" state: absent become: True when: inventory_hostname in etc_hosts_hosts @@ -39,7 +40,7 @@ block: | {% for host in etc_hosts_hosts %} {% if hostvars[host]['etc_hosts_network'] in hostvars[host].network_interfaces %} - {% set hostnames = [hostvars[host].ansible_facts.nodename, hostvars[host].ansible_facts.hostname] %} + {% set hostnames = [hostvars[host].kayobe_custom_hostname] if (kayobe_customize_hostnames | bool) else [hostvars[host].ansible_facts.nodename, hostvars[host].ansible_facts.hostname] %} {{ hostvars[host]['etc_hosts_network'] | net_ip(inventory_hostname=host) }} {{ hostnames | unique | join(' ') }} {% endif %} {% endfor %} diff --git a/ansible/roles/kolla-ansible/templates/overcloud-top-level.j2 b/ansible/roles/kolla-ansible/templates/overcloud-top-level.j2 index ee11d9417..d1cf2f806 100644 --- a/ansible/roles/kolla-ansible/templates/overcloud-top-level.j2 +++ b/ansible/roles/kolla-ansible/templates/overcloud-top-level.j2 @@ -11,7 +11,7 @@ {% for host in groups.get(group, []) %} {%- if hostvars[host].kolla_overcloud_inventory_pass_through_enabled | default(true) | bool -%} {{ host }} -{%- endif -%} +{% endif -%} {% endfor %} {% endfor %} diff --git a/doc/source/configuration/reference/hosts.rst b/doc/source/configuration/reference/hosts.rst index 758225d13..e74aebcc0 100644 --- a/doc/source/configuration/reference/hosts.rst +++ b/doc/source/configuration/reference/hosts.rst @@ -1551,6 +1551,29 @@ AppArmor for the libvirt container On Ubuntu systems running the ``nova_libvirt`` Kolla container, AppArmor rules for libvirt are disabled. +Setting the hostname of kayobe-managed hosts +============================================ + +*tags:* + | ``hostname`` + +Kayobe can configure the hostname of the hosts it manages via systemd. By +default, this will configure the hostname to match the name in the Ansible +inventory. This can be customised further with the variable +``kayobe_custom_hostname``. This feature is disabled by default, but can be +enabled with the variable ``kayobe_customize_hostnames``. + +.. note:: + + Setting hostnames is intended to be a global feature, rather than per host. + As such, ``kayobe_custom_hostname`` should be set as an extra var instead + of a host var. + +.. code-block:: yaml + + kayobe_customize_hostnames: true + kayobe_custom_hostname: "{{ inventory_hostname }}" + Adding entries to /etc/hosts ============================ *tags:* @@ -1587,6 +1610,14 @@ follows: etc_hosts_gather_facts: false +Alternatively, if you are configuring hostnames via Kayobe then this task will +skip gathering facts and directly populate ``/etc/hosts`` files based on +``kayobe_custom_hostname``. + +.. code-block:: yaml + + kayobe_customize_hostnames: true + Custom entries can be added to the ``custom_etc_hosts_entries`` dictionary. Each key is treated as a hostname and each value is the IP, for example: diff --git a/etc/kayobe/hostname.yml b/etc/kayobe/hostname.yml new file mode 100644 index 000000000..34b4b420c --- /dev/null +++ b/etc/kayobe/hostname.yml @@ -0,0 +1,9 @@ +--- +############################################################################### +# Kayobe custom hostname configuration. + +# Whether to set custom hostnames. Default is false. +#kayobe_customize_hostnames: + +# Custom hostname to set. Default is "{{ inventory_hostname }}". +#kayobe_custom_hostname: diff --git a/releasenotes/notes/fix-overcloud-hosts-ad452b432ac2a304.yaml b/releasenotes/notes/fix-overcloud-hosts-ad452b432ac2a304.yaml new file mode 100644 index 000000000..c0ccc524b --- /dev/null +++ b/releasenotes/notes/fix-overcloud-hosts-ad452b432ac2a304.yaml @@ -0,0 +1,6 @@ +--- +fixes: + - | + Fix all hostnames in a group being merged into a single line in the kolla + overcloud hosts inventory file. + `LP#2158773 `__ diff --git a/releasenotes/notes/support-configuring-hostnames-3097c993f8da3459.yaml b/releasenotes/notes/support-configuring-hostnames-3097c993f8da3459.yaml new file mode 100644 index 000000000..af604854b --- /dev/null +++ b/releasenotes/notes/support-configuring-hostnames-3097c993f8da3459.yaml @@ -0,0 +1,7 @@ +--- +features: + - | + Adds support for configuring hostnames via Kayobe. This is disabled by + default; it can be enabled by setting ``kayobe_customize_hostnames`` to + ``true``. By default, Kayobe will use the Ansible inventory hostname. This + can be overriden by setting ``kayobe_custom_hostname``.