Browse Source

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
changes/44/325944/1
Ricardo Carrillo Cruz 3 years ago
parent
commit
dc6dcfb736
1 changed files with 8 additions and 2 deletions
  1. 8
    2
      roles/set_hostnames/tasks/main.yml

+ 8
- 2
roles/set_hostnames/tasks/main.yml View File

@@ -1,16 +1,22 @@
1 1
 ---
2
+- name: Get hostname from server name
3
+  set_fact: server_hostname={{ inventory_hostname.partition('.')[0] }}
4
+- name: Get server UUID from dynamic inventory
5
+  set_fact: server_uuid={{ groups[inventory_hostname][0] }}
6
+- name: Get server IP
7
+  set_fact: server_ip={{ hostvars[server_uuid]['openstack']['public_v4'] }}
2 8
 # Set hostname and /etc/hosts
3 9
 # Inspired by:
4 10
 # https://github.com/ansible/ansible/pull/8482)
5 11
 # https://gist.github.com/rothgar/8793800
6 12
 - name: Set /etc/hostname
7
-  hostname: name="{{ inventory_hostname.split('.', 1)[0] }}"
13
+  hostname: name="{{ server_hostname }}"
8 14
 
9 15
 # " lovely lonely double-quote for fixing vim highlighting
10 16
 
11 17
 - name: Add all infra hosts to /etc/hosts
12 18
   lineinfile: dest=/etc/hosts
13
-              line='{{ hostvars[item]['openstack']['private_v4'] }} {{ item }} {{ item.split('.', 1)[0] }}'
19
+              line='{{ server_ip }} {{ item }} {{ server_hostname }}'
14 20
               insertafter='^127\.0\.0\.1'
15 21
               state=present
16 22
   with_items: groups['infra']

Loading…
Cancel
Save