diff --git a/ansible/roles/baremetal/tasks/pre-install.yml b/ansible/roles/baremetal/tasks/pre-install.yml
index bfa277487b..d8632ae4b5 100644
--- a/ansible/roles/baremetal/tasks/pre-install.yml
+++ b/ansible/roles/baremetal/tasks/pre-install.yml
@@ -21,11 +21,18 @@
     dest: /etc/hosts
     marker: "# {mark} ANSIBLE GENERATED HOSTS"
     block: |
-        {% for host in groups['all'] %}
-        {{ hostvars[host]['ansible_' + hostvars[host]['api_interface']]['ipv4']['address'] }} {{ hostvars[host]['ansible_hostname'] }}
+        {% for host in groups['baremetal'] %}
+        {% set api_interface = hostvars[host]['api_interface'] %}
+        {% if host not in groups['bifrost'] or 'ansible_' + api_interface in hostvars[host] %}
+        {{ hostvars[host]['ansible_' + api_interface]['ipv4']['address'] }} {{ hostvars[host]['ansible_hostname'] }}
+        {% endif %}
         {% endfor %}
   become: True
-  when: customize_etc_hosts | bool
+  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
+      'ansible_' + hostvars[inventory_hostname]['api_interface'] in hostvars[inventory_hostname]
 
 - name: Ensure sudo group is present
   group: name=sudo state=present