From dc6dcfb736aacf069cb8328cc2a6fbc686fea097 Mon Sep 17 00:00:00 2001 From: Ricardo Carrillo Cruz Date: Mon, 6 Jun 2016 14:22:17 +0000 Subject: [PATCH] Fix the set_hostnames role when using default inventory behaviour Some time ago, the openstack inventory would show have as hostvars id the server name. That behaviour changed, and now the id is the server uuid, which breaks the set_hostnames logic. This change fixes it. Change-Id: Ie958a9d04fdfeabfab64100e43a4ead44b82a7b8 --- roles/set_hostnames/tasks/main.yml | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/roles/set_hostnames/tasks/main.yml b/roles/set_hostnames/tasks/main.yml index e796916..ecb4016 100644 --- a/roles/set_hostnames/tasks/main.yml +++ b/roles/set_hostnames/tasks/main.yml @@ -1,16 +1,22 @@ --- +- name: Get hostname from server name + set_fact: server_hostname={{ inventory_hostname.partition('.')[0] }} +- name: Get server UUID from dynamic inventory + set_fact: server_uuid={{ groups[inventory_hostname][0] }} +- name: Get server IP + set_fact: server_ip={{ hostvars[server_uuid]['openstack']['public_v4'] }} # Set hostname and /etc/hosts # Inspired by: # https://github.com/ansible/ansible/pull/8482) # https://gist.github.com/rothgar/8793800 - name: Set /etc/hostname - hostname: name="{{ inventory_hostname.split('.', 1)[0] }}" + hostname: name="{{ server_hostname }}" # " lovely lonely double-quote for fixing vim highlighting - name: Add all infra hosts to /etc/hosts lineinfile: dest=/etc/hosts - line='{{ hostvars[item]['openstack']['private_v4'] }} {{ item }} {{ item.split('.', 1)[0] }}' + line='{{ server_ip }} {{ item }} {{ server_hostname }}' insertafter='^127\.0\.0\.1' state=present with_items: groups['infra']