From 6236e557fdf25755a6c2fbb5fa889a69337a65cb Mon Sep 17 00:00:00 2001 From: Mark Goddard Date: Tue, 7 Dec 2021 15:41:01 +0000 Subject: [PATCH] baremetal: Refactor /etc/hosts configuration into a separate role Change-Id: If65657860fc607b92dca62337fc24dd7b5d21589 --- roles/baremetal/defaults/main.yml | 2 - roles/baremetal/tasks/main.yml | 3 ++ roles/baremetal/tasks/pre-install.yml | 64 --------------------------- roles/etc_hosts/defaults/main.yml | 3 ++ roles/etc_hosts/tasks/etc-hosts.yml | 60 +++++++++++++++++++++++++ roles/etc_hosts/tasks/main.yml | 4 ++ 6 files changed, 70 insertions(+), 66 deletions(-) create mode 100644 roles/etc_hosts/defaults/main.yml create mode 100644 roles/etc_hosts/tasks/etc-hosts.yml create mode 100644 roles/etc_hosts/tasks/main.yml diff --git a/roles/baremetal/defaults/main.yml b/roles/baremetal/defaults/main.yml index 597c98d..de1520d 100644 --- a/roles/baremetal/defaults/main.yml +++ b/roles/baremetal/defaults/main.yml @@ -18,8 +18,6 @@ ceph_yum_gpgcheck: true ceph_yum_package: "ceph-common" epel_yum_package: "epel-release" -customize_etc_hosts: True - create_kolla_user: False kolla_user: "kolla" diff --git a/roles/baremetal/tasks/main.yml b/roles/baremetal/tasks/main.yml index 69e93dc..fb2ad93 100644 --- a/roles/baremetal/tasks/main.yml +++ b/roles/baremetal/tasks/main.yml @@ -1,4 +1,7 @@ --- +- import_role: + name: openstack.kolla.etc_hosts + - import_tasks: pre-install.yml - import_tasks: install.yml diff --git a/roles/baremetal/tasks/pre-install.yml b/roles/baremetal/tasks/pre-install.yml index 4438626..79b7e62 100644 --- a/roles/baremetal/tasks/pre-install.yml +++ b/roles/baremetal/tasks/pre-install.yml @@ -1,48 +1,4 @@ --- -- name: Ensure localhost in /etc/hosts - lineinfile: - dest: /etc/hosts - regexp: "^127.0.0.1.*" - line: "127.0.0.1 localhost" - create: true - mode: 0644 - state: present - become: True - when: customize_etc_hosts | bool - -# NOTE(mgoddard): Ubuntu may include a line in /etc/hosts that makes the local -# hostname and fqdn point to 127.0.1.1. This can break -# RabbitMQ, which expects the hostname to resolve to the API network address. -# Remove the troublesome entry. -# see https://bugs.launchpad.net/kolla-ansible/+bug/1837699 -# and https://bugs.launchpad.net/kolla-ansible/+bug/1862739 -- 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" - state: absent - become: True - when: customize_etc_hosts | bool - -- name: Generate /etc/hosts for all of the nodes - blockinfile: - dest: /etc/hosts - marker: "# {mark} ANSIBLE GENERATED HOSTS" - block: | - {% for host in groups['baremetal'] %} - {% set api_interface = hostvars[host]['api_interface'] %} - {% if host not in groups['bifrost'] or api_interface in hostvars[host].ansible_facts %} - {% set hostnames = [hostvars[host].ansible_facts.nodename, hostvars[host].ansible_facts.hostname] %} - {{ 'api' | kolla_address(host) }} {{ hostnames | unique | join(' ') }} - {% endif %} - {% endfor %} - become: True - when: - - customize_etc_hosts | bool - # Skip hosts in the bifrost group that do not have a valid api_interface. - - inventory_hostname not in groups['bifrost'] or - hostvars[inventory_hostname]['api_interface'] | replace('-', '_') in hostvars[inventory_hostname].ansible_facts - - name: Ensure unprivileged users can use ping become: true sysctl: @@ -51,23 +7,3 @@ state: present sysctl_file: "{{ kolla_sysctl_conf_path }}" when: ansible_facts.os_family == 'RedHat' - -# NOTE(osmanlicilegi): The distribution might come with cloud-init installed, and manage_etc_hosts -# configuration enabled. If so, it will override the file /etc/hosts from cloud-init templates at -# every boot, which will break RabbitMQ. To prevent this happens, first we check whether cloud-init -# has been installed, and then set manage_etc_hosts to false. -- name: Check whether cloud-init has been installed, and ensure manage_etc_hosts is disabled - block: - - name: Ensure /etc/cloud/cloud.cfg exists - stat: - path: /etc/cloud/cloud.cfg - register: cloud_init - - - name: Disable cloud-init manage_etc_hosts - copy: - content: "manage_etc_hosts: false" - dest: /etc/cloud/cloud.cfg.d/99-kolla.cfg - mode: "0660" - when: cloud_init.stat.exists - become: True - when: customize_etc_hosts | bool diff --git a/roles/etc_hosts/defaults/main.yml b/roles/etc_hosts/defaults/main.yml new file mode 100644 index 0000000..519f126 --- /dev/null +++ b/roles/etc_hosts/defaults/main.yml @@ -0,0 +1,3 @@ +--- +# Whether to add entries to /etc/hosts. +customize_etc_hosts: true diff --git a/roles/etc_hosts/tasks/etc-hosts.yml b/roles/etc_hosts/tasks/etc-hosts.yml new file mode 100644 index 0000000..5c1b26a --- /dev/null +++ b/roles/etc_hosts/tasks/etc-hosts.yml @@ -0,0 +1,60 @@ +--- +- name: Ensure localhost in /etc/hosts + lineinfile: + dest: /etc/hosts + regexp: "^127.0.0.1.*" + line: "127.0.0.1 localhost" + create: true + mode: 0644 + state: present + become: True + +# NOTE(mgoddard): Ubuntu may include a line in /etc/hosts that makes the local +# hostname and fqdn point to 127.0.1.1. This can break +# RabbitMQ, which expects the hostname to resolve to the API network address. +# Remove the troublesome entry. +# see https://bugs.launchpad.net/kolla-ansible/+bug/1837699 +# and https://bugs.launchpad.net/kolla-ansible/+bug/1862739 +- 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" + state: absent + become: True + +- name: Generate /etc/hosts for all of the nodes + blockinfile: + dest: /etc/hosts + marker: "# {mark} ANSIBLE GENERATED HOSTS" + block: | + {% for host in groups['baremetal'] %} + {% set api_interface = hostvars[host]['api_interface'] %} + {% if host not in groups['bifrost'] or api_interface in hostvars[host].ansible_facts %} + {% set hostnames = [hostvars[host].ansible_facts.nodename, hostvars[host].ansible_facts.hostname] %} + {{ 'api' | kolla_address(host) }} {{ hostnames | unique | join(' ') }} + {% endif %} + {% endfor %} + become: True + when: + # Skip hosts in the bifrost group that do not have a valid api_interface. + - inventory_hostname not in groups['bifrost'] or + hostvars[inventory_hostname]['api_interface'] | replace('-', '_') in hostvars[inventory_hostname].ansible_facts + +# NOTE(osmanlicilegi): The distribution might come with cloud-init installed, and manage_etc_hosts +# configuration enabled. If so, it will override the file /etc/hosts from cloud-init templates at +# every boot, which will break RabbitMQ. To prevent this happens, first we check whether cloud-init +# has been installed, and then set manage_etc_hosts to false. +- name: Check whether cloud-init has been installed, and ensure manage_etc_hosts is disabled + block: + - name: Check whether /etc/cloud/cloud.cfg exists + stat: + path: /etc/cloud/cloud.cfg + register: cloud_init + + - name: Disable cloud-init manage_etc_hosts + copy: + content: "manage_etc_hosts: false" + dest: /etc/cloud/cloud.cfg.d/99-kolla.cfg + mode: "0660" + when: cloud_init.stat.exists + become: True diff --git a/roles/etc_hosts/tasks/main.yml b/roles/etc_hosts/tasks/main.yml new file mode 100644 index 0000000..09de785 --- /dev/null +++ b/roles/etc_hosts/tasks/main.yml @@ -0,0 +1,4 @@ +--- +- name: Include etc-hosts.yml + include_tasks: etc-hosts.yml + when: customize_etc_hosts | bool