Skip to content

Commit 3e57f3c

Browse files
authored
Merge pull request sap-linuxlab#1212 from marcelmamula/etc-hosts
sap_maintain_etc_hosts: Refactor role and implement improved handling from PR 1195
2 parents b4b664b + 80adb3c commit 3e57f3c

14 files changed

Lines changed: 892 additions & 490 deletions

roles/sap_maintain_etc_hosts/README.md

Lines changed: 76 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
## Description
66
<!-- BEGIN Description -->
7-
The Ansible role `sap_maintain_etc_hosts` is used to maintain the `/etc/hosts` file..
7+
The Ansible role `sap_maintain_etc_hosts` is used to maintain the `/etc/hosts` file.
88
<!-- END Description -->
99

1010
<!-- BEGIN Dependencies -->
@@ -22,38 +22,40 @@ The Ansible role `sap_maintain_etc_hosts` is used to maintain the `/etc/hosts` f
2222

2323
### Execution Flow
2424
<!-- BEGIN Execution Flow -->
25-
1. Assert that required inputs were provided.
26-
2. Verify duplicate entries and conflicts;
25+
1. Assert and validate input variables.
26+
2. Verify duplicate entries and conflicts.
2727
3. Update `/etc/hosts` file.
2828
<!-- END Execution Flow -->
2929

3030
### Example
3131
<!-- BEGIN Execution Example -->
32-
Example playbook will update `/etc/hosts`:
32+
The example playbook will update `/etc/hosts`:
3333

3434
- Remove node with IP `10.10.10.10`.
3535
- Remove node with name `host2`.
3636
- Add node with IP `10.10.10.11`, name `host1`, aliases `alias1, alias2` and comment `host1 comment`.
3737
```yaml
38-
- name: Ansible Play for add entry in /etc/hosts
38+
- name: Ansible Play to manage entries in /etc/hosts
3939
hosts: all
4040
become: true
4141
tasks:
42-
- name: Execute Ansible Role sap_sap_maintain_etc_hosts
42+
- name: Execute Ansible Role sap_maintain_etc_hosts
4343
ansible.builtin.include_role:
44-
name: community.sap_install.sap_sap_maintain_etc_hosts
44+
name: community.sap_install.sap_maintain_etc_hosts
4545
vars:
4646
sap_maintain_etc_hosts_list:
4747
- node_ip: 10.10.10.10
4848
state: absent
49+
4950
- node_name: host2
5051
state: absent
52+
5153
- node_ip: 10.10.10.11
5254
node_name: host1
5355
aliases:
5456
- alias1
5557
- alias2
56-
node_comment: "host1 comment" # Comment is created after hash sign (defaults to hana_site)
58+
node_comment: "host1 comment"
5759
state: present
5860
```
5961
@@ -63,9 +65,9 @@ Example playbook when executed together with [sap_ha_pacemaker_cluster](https://
6365
hosts: all
6466
become: true
6567
tasks:
66-
- name: Execute Ansible Role sap_sap_maintain_etc_hosts
68+
- name: Execute Ansible Role sap_maintain_etc_hosts
6769
ansible.builtin.include_role:
68-
name: community.sap_install.sap_sap_maintain_etc_hosts
70+
name: community.sap_install.sap_maintain_etc_hosts
6971
vars:
7072
sap_maintain_etc_hosts_list: "{{ sap_ha_pacemaker_cluster_cluster_nodes }}"
7173
```
@@ -88,6 +90,7 @@ Apache 2.0
8890
<!-- BEGIN Maintainers -->
8991
- [Markus Koch](https://github.com/rhmk)
9092
- [Bernd Finger](https://github.com/berndfinger)
93+
- [Marcel Mamula](https://github.com/marcelmamula)
9194
<!-- END Maintainers -->
9295

9396
## Role Variables
@@ -96,77 +99,105 @@ Apache 2.0
9699
This role requires the dictionary `sap_maintain_etc_hosts_list` which contains the parameters for the `/etc/hosts` file.
97100

98101
The default value is the definition of the cluster nodes like in the role [sap_ha_pacemaker_cluster](https://github.com/sap-linuxlab/community.sap_install/tree/main/roles/sap_ha_pacemaker_cluster).</br>
99-
If the value `sap_hana_cluster_nodes`or `sap_ha_pacemaker_cluster_cluster_nodes` is not defined, then the role creates a default value from `ansible_facts`.
102+
If the value `sap_hana_cluster_nodes` or `sap_ha_pacemaker_cluster_cluster_nodes` is not defined, then the role expects valid `sap_maintain_etc_hosts_list`.
100103

101-
**NOTE: If you want to use this role to remove entries from /etc/hosts it is a good practice to do this before adding entries. The adding/removal is done in the order the entries are listed.**
104+
**NOTE: If you want to use this role to remove entries from /etc/hosts it is a good practice to do this before adding entries.**<br>
105+
**The adding/removal is done in the order the entries are listed.**
102106

103107
### sap_maintain_etc_hosts_list
104-
105108
- _Type:_ `list` with elements of type `dict`
106109

107110
Mandatory list of nodes in form of dictionaries to be added or removed in `/etc/hosts` file.
108111

109112
Following dictionary keys can be defined:
110113

111114
- **node_ip**<br>
112-
IP address of the managed node.<br>
113-
**Required** for adding new entries to `/etc/hosts`.</br>
114-
_Optional_ for removing entries, where `node_name` and `node_domain` can be used instead.
115-
116115
- _Type:_ `string`
117116

118-
- **node_name**<br>
119-
Hostname of the managed node.<br>
120-
**Required** for adding new entries to `/etc/hosts`.</br>
121-
_Optional_ for removing entries, when `node_ip` is not used.
117+
IP address of the host to manage, depending on `state`.<br>
118+
**Required** for `state: present`.</br>
119+
_Optional_ for `state: absent`, where `node_name` and `node_domain` can be used instead.
122120

121+
- **node_name**<br>
123122
- _Type:_ `string`
124123

125-
- **node_domain**<br>
126-
Domain name of the managed node. Defaults to `sap_domain` if set or `ansible_facts['domain']`.<br>
127-
**Required** for adding new entries to `/etc/hosts`.</br>
128-
_Optional_ for removing entries, when `node_name` is used.
124+
Hostname of the host to manage.<br>
125+
**Required** for `state: present`.</br>
126+
_Optional_ for `state: absent`, where `node_ip` can be used instead.
129127

128+
- **node_domain**<br>
130129
- _Type:_ `string`
131-
- _Default:_ `sap_domain`
130+
- _Default:_ `{{ sap_maintain_etc_hosts_domain }}`
132131

133-
- **aliases**<br>
134-
List of aliases for the managed node.<br>
135-
_Optional_ for adding new entries to `/etc/hosts`.
132+
Domain name of the host to manage.<br>
133+
Defaults to `sap_maintain_etc_hosts_domain` or `ansible_facts['domain']`.<br>
134+
**Required** for `state: present`.</br>
135+
_Optional_ for `state: absent` with `node_name`, where `node_ip` can be used instead.
136136

137+
- **aliases**<br>
137138
- _Type:_ `list` with elements of type `string`
138139

139-
- **alias_mode**<br>
140-
Select method of updating `/etc/hosts` file:<br>
141-
- `merge` : merges the list of aliases with the exiting aliases of the node.<br>
142-
- `overwrite` : overwrites the aliases of the node.
143-
_Optional_ for adding new entries to `/etc/hosts`.
140+
List of aliases of the host to manage.<br>
141+
_Optional_ for `state: present`.<br>
142+
Not used in `state: absent`.
144143

144+
- **alias_mode**<br>
145145
- _Type:_ `string`
146146
- _Default:_ `merge`
147147

148-
- **node_comment**<br>
149-
Node comment is appended at end of line of managed node.<br>
150-
_Optional_ for adding new entries to `/etc/hosts`.
148+
Select method of managing aliases of the host.
149+
150+
- `merge` - merges the list of aliases with the exiting aliases of the host.
151+
- `overwrite` - overwrites the aliases of the host.
151152

153+
_Optional_ for `state: present`.<br>
154+
Not used in `state: absent`.
155+
156+
- **node_comment**<br>
152157
- _Type:_ `string`
153158
- _Default:_ `managed by ansible sap_maintain_etc_hosts role`
154159

155-
- **hana_site**<br>
156-
Used by [sap_ha_pacemaker_cluster](https://github.com/sap-linuxlab/community.sap_install/tree/main/roles/sap_ha_pacemaker_cluster) and it is appended to `node_comment`<br>
157-
_Optional_ for adding new entries to `/etc/hosts`.
160+
String which is appended at end of line of the host to manage.<br>
161+
Replaces existing comment, if present.<br>
162+
_Optional_ for `state: present`.<br>
163+
Not used in `state: absent`.
158164

165+
- **hana_site**<br>
159166
- _Type:_ `string`
160167

161-
- **node_role**<br>
162-
Not used, but mentioned for compatibility reasons for [sap_ha_pacemaker_cluster](https://github.com/sap-linuxlab/community.sap_install/tree/main/roles/sap_ha_pacemaker_cluster) role.<br>
168+
Used by [sap_ha_pacemaker_cluster](https://github.com/sap-linuxlab/community.sap_install/tree/main/roles/sap_ha_pacemaker_cluster) role and it is appended to `node_comment`<br>
169+
_Optional_ for `state: present`.<br>
170+
Not used in `state: absent`.
163171

172+
- **node_role**<br>
164173
- _Type:_ `string`
165174

166-
- **state**<br>
167-
Select `present` for adding new entries, `absent` for removing them.<br>
168-
**Required** for removing entries, otherwise default `present` is used.
175+
Not used, but mentioned for compatibility reasons with [sap_ha_pacemaker_cluster](https://github.com/sap-linuxlab/community.sap_install/tree/main/roles/sap_ha_pacemaker_cluster) role.<br>
169176

177+
- **state**<br>
170178
- _Type:_ `string`
171179
- _Default:_ `present`
180+
181+
Select desired state of host item.
182+
183+
- `present` - Add new hosts entry.
184+
- `absent` - Remove existing hosts entry.
185+
186+
_Optional_ for `state: present`.<br>
187+
**Required** for `state: absent`, because it defaults to `present`.</br>
188+
189+
### sap_maintain_etc_hosts_delimiter
190+
- _Type:_ `str`
191+
- _Default:_ `' '`
192+
- _Choices:_ `' ', '/t'`
193+
194+
Delimiter for `/etc/hosts` used to separate all elements on a line.</br>
195+
Changing delimiter will report as change, even if actual content did not change.</br>
196+
197+
### sap_maintain_etc_hosts_comment
198+
- _Type:_ `str`
199+
- _Default:_ `managed by Ansible role sap_maintain_etc_hosts`
200+
201+
Comment to be appended, if comment is not defined in `sap_maintain_etc_hosts_list`.
202+
Replaces existing comment, if present.<br>
172203
<!-- END Role Variables -->
Lines changed: 13 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,19 @@
11
# SPDX-License-Identifier: Apache-2.0
22
---
3-
# defaults file for sap_maintain_etc_hosts
4-
# BEGIN: Default Variables for sap_maintain_etc_hosts
53

6-
# sap_maintain_etc_hosts_list: (not defined by default)
7-
# List of nodes to be added or removed in /etc/hosts
8-
# Possible options:
9-
# - node_ip:
10-
# IP address of the node.
11-
# It is required for adding a node.
12-
# When deleting a node use only when node_name and node_domain are not defined
13-
# - node_name
14-
# Hostname of the node
15-
# It is required for adding a node.
16-
# When deleting a node use only when node_ip is not defined
17-
# - node_domain
18-
# Domainname of the node
19-
# Defaults to sap_domain, if set, otherwise ansible_facts['domain'] is the default
20-
# When deleting a node use only when node_name is defined
21-
# - aliases
22-
# List of aliases for the node
23-
# Not used when state is absent
24-
# - alias_mode
25-
# `merge` : merges the list of aliases with the exiting aliases of the node. (default)
26-
# `overwrite`: overwrites the aliases of the node.
27-
# Not used when state is absent
28-
# - node_comment
29-
# default: managed by ansible sap_maintain_etc_hosts role`
30-
# String which is appended to line in hosts after comment string
31-
# Not used when state is absent
32-
# - hana_site
33-
# if set (e.g. for configuring cluster) it is appended to the comment
34-
# Not used when state is absent
35-
# - node_role
36-
# Not used. For compatibility reason only.
37-
# - state
38-
# default: present
39-
# Defines, if an entry is added or removed from /etc/hosts
40-
#
41-
# Example: See README.md
4+
# This variable defines the list of hosts to be managed in /etc/hosts.
5+
# Structure definition and examples are available in README.md file.
6+
# It loads variables from other roles, if available, otherwise stays undefined by default.
7+
sap_maintain_etc_hosts_list:
8+
"{{ sap_hana_cluster_nodes | d(sap_ha_pacemaker_cluster_cluster_nodes) | d([]) }}"
429

43-
# END: Default Variables for sap_maintain_etc_hosts
10+
# Delimiter for '/etc/hosts' used to separate all elements on a line.
11+
# Choices: ' ', '\t'
12+
sap_maintain_etc_hosts_delimiter: " "
4413

45-
# Default Value is the definition of the cluster nodes in sap_ha_pacemaker_cluster. If that is not defined
46-
# the role creates a default value from ansible_facts
14+
# Comment to be appended, if comment is not defined in 'sap_maintain_etc_hosts_list'.
15+
sap_maintain_etc_hosts_comment: "managed by Ansible role sap_maintain_etc_hosts"
4716

48-
sap_maintain_etc_hosts_list: "{{ sap_hana_cluster_nodes | default(sap_ha_pacemaker_cluster_cluster_nodes) | default(omit) }}"
49-
50-
# regex patterns for IP address validation:
51-
sap_maintain_etc_hosts_regexp_ipv4: '^((25[0-5]|(2[0-4]|1\d|[1-9]|)\d)\.?\b){4}$'
52-
sap_maintain_etc_hosts_regexp_ipv6: '^(?:[a-fA-F0-9]{0,4}:){7}[a-fA-F0-9]{0,4}$'
17+
# Domain name to be used for hosts in 'sap_maintain_etc_hosts_list' if not defined
18+
# or host item does not have 'node_domain' key defined. This is optional and by default it is not set.
19+
# sap_maintain_etc_hosts_domain: ''

0 commit comments

Comments
 (0)