Skip to content

Commit 0edff90

Browse files
committed
[multiple] Add short hostnames into the /etc/hosts
In some cases, we need to be able to resolve short host names like controller-0 and not always relay on ssh configuration file. Normally on executing the reproducer playbook, we are reading the libvirt_inventory file, that contains most of the host on which we want to connect. That feature gives just developers to connect to VMs using other tools than SSH. Signed-off-by: Daniel Pawlik <[email protected]>
1 parent 9ad92d9 commit 0edff90

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

roles/dnsmasq/defaults/main.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,4 @@ cifmw_dnsmasq_listen_addresses: []
2727
cifmw_dnsmasq_enable_dns: false
2828
cifmw_dnsmasq_exclude_lo: false
2929
cifmw_dnsmasq_addresses: []
30+
cifmw_dnsmasq_shortnames_etc_hosts: true

roles/dnsmasq/tasks/manage_host_record.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,23 @@
3434
state: "{{ item.state }}"
3535
validate: "/usr/sbin/dnsmasq -C %s --test"
3636
loop: "{{ cifmw_dnsmasq_host_record }}"
37+
38+
# NOTE(dpawlik): Add also short names like controller-0, master-0
39+
# into the /etc/hosts file, to avoid errors when all the configuration
40+
# is only stored in ssh config files.
41+
# It has been done into /etc/hosts, due dnsmasq require to contain a domain.
42+
- name: Add short hostname records when 'utility' in the name into /etc/hosts
43+
when:
44+
- cifmw_dnsmasq_shortnames_etc_hosts
45+
- record.names | select('search', 'utility') | list | length > 0
46+
become: true
47+
ansible.builtin.lineinfile:
48+
path: "/etc/hosts"
49+
line: "{{ ip }} {{ short_name }}"
50+
state: "{{ record.state }}"
51+
loop: "{{ cifmw_dnsmasq_host_record }}"
52+
loop_control:
53+
loop_var: record
54+
vars:
55+
short_name: "{{ record.names[0] | regex_replace('^([^.]+)\\..*$', '\\1') }}"
56+
ip: "{{ record.ips[0] }}"

0 commit comments

Comments
 (0)