Skip to content

Commit

Permalink
Add a list of additional hosts to /etc/hosts
Browse files Browse the repository at this point in the history
  • Loading branch information
Thilo Solbrig committed Apr 19, 2024
1 parent 610f983 commit fec2c98
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
17 changes: 17 additions & 0 deletions roles/orahost/defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,23 @@ sudoers_template: sudoers.j2
# @end
etc_hosts_ip: "{% if 'virtualbox' in ansible_virtualization_type %}{{ ansible_all_ipv4_addresses[1] }}{% else %}{{ ansible_default_ipv4.address }}{% endif %}"

# @var etc_hosts_entries:description: >
# List of additional entries, optionally along with aliases, to be put into /etc/hosts. E.g. on non-DNS environments or if we don't rely on DNS
# @end
# @var etc_hosts_entries:example: >
# etc_hosts_entries:
# - fqdn: clusternode1.example.com
# ip: 192.168.1.1
# - fqdn: clusternode2.example.com
# ip: 192.168.1.2
# aliases:
# - myalias.example.com
# would create following entries in /etc/hosts:
# 192.168.1.1 clusternode1 clusternode1.example.com
# 192.168.1.2 clusternode2 clusternode2.example.com myalias.example.com
# @end
etc_hosts_entries: []

# Extra repositories that should be enabled
extrarepos_enabled: "{%- if ansible_distribution == 'OracleLinux' -%}\
ol{{ ansible_distribution_major_version }}_addons\
Expand Down
19 changes: 19 additions & 0 deletions roles/orahost/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,25 @@
when: configure_etc_hosts
tags: etchosts

- name: Add manually defined hosts to /etc/hosts
# e.g. on non-DNS environments or if we don't rely on DNS
ansible.builtin.lineinfile:
dest: /etc/hosts

Check warning on line 51 in roles/orahost/tasks/main.yml

View workflow job for this annotation

GitHub Actions / build

jinja[spacing]

Jinja2 spacing could be improved: {{ __orahost_etc_hosts_entry.ip }} {{ (__orahost_etc_hosts_entry.fqdn|split('.'))[0] }} {{ __orahost_etc_hosts_entry.fqdn is search('\.') | ternary(__orahost_etc_hosts_entry.fqdn, '') }}{% for alias in __orahost_etc_hosts_entry.aliases|default([]) %} {{ alias }}{% endfor %} # ANSIBLE managed -> {{ __orahost_etc_hosts_entry.ip }} {{ (__orahost_etc_hosts_entry.fqdn | split('.'))[0] }} {{ __orahost_etc_hosts_entry.fqdn is search('\.') | ternary(__orahost_etc_hosts_entry.fqdn, '') }}{% for alias in __orahost_etc_hosts_entry.aliases | default([]) %} {{ alias }}{% endfor %} # ANSIBLE managed
regexp: '.*{{ __orahost_etc_hosts_entry.fqdn }}$'
line:
"{{ __orahost_etc_hosts_entry.ip }}
{{ (__orahost_etc_hosts_entry.fqdn|split('.'))[0] }}
{{ __orahost_etc_hosts_entry.fqdn is search('\\.') | ternary(__orahost_etc_hosts_entry.fqdn, '') }}\
{% for alias in __orahost_etc_hosts_entry.aliases|default([]) %} {{ alias }}{% endfor %} # ANSIBLE managed"
state: present
loop: "{{ etc_hosts_entries }}"
loop_control:
loop_var: __orahost_etc_hosts_entry
when:
- configure_etc_hosts
- etc_hosts_entries|type_debug == "list"
tags: etchosts

# - name: Add local node's ip & hostname to /etc/hosts
# lineinfile: dest=/etc/hosts line="{{ ansible_default_ipv4.address }} {{ ansible_fqdn }} {{ ansible_hostname }} " state=present
# when: configure_etc_hosts and ansible_default_ipv4.address is defined
Expand Down

0 comments on commit fec2c98

Please sign in to comment.