forked from sap-linuxlab/community.sap_install
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathupdate_host_present.yml
More file actions
205 lines (186 loc) · 9.01 KB
/
Copy pathupdate_host_present.yml
File metadata and controls
205 lines (186 loc) · 9.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# SPDX-License-Identifier: Apache-2.0
---
- name: Verify that variable node_ip is set
ansible.builtin.assert:
that: not( ( thishost.node_ip is undefined) or ( thishost.node_ip is none) or ( thishost.node_ip | trim == '') )
msg: |
"The IP address of this host not known. You can solve this problem by
configuring your managed node accordingly or by setting one of the following variables:
- sap_ip
- sap_maintain_etc_hosts_list, member node_ip"
- name: Verify that variable node_ip is using the correct IP address format
ansible.builtin.assert:
that:
- (thishost.node_ip | regex_search(sap_maintain_etc_hosts_regexp_ipv4) is not none
and thishost.node_ip | regex_search(sap_maintain_etc_hosts_regexp_ipv4) | length > 0)
or (thishost.node_ip | regex_search(sap_maintain_etc_hosts_regexp_ipv6) is not none
and thishost.node_ip | regex_search(sap_maintain_etc_hosts_regexp_ipv6) | length > 0)
msg: |
"The IP address of this host does not have a correct format.
Configure the IP address appropriately in of the following variables:
- sap_ip
- sap_maintain_etc_hosts_list, member node_ip"
- name: Verify that variable node_name is set
ansible.builtin.assert:
that: not( ( thishost.node_name is undefined) or ( thishost.node_name is none) or ( thishost.node_name | trim == '') )
msg: |
"The hostname of this host not known. You can solve this problem by
configuring your managed node accordingly or by setting one of the following variables:
- sap_hostname
- sap_maintain_etc_hosts_list, member node_name"
- name: Ensure node_domain is set
ansible.builtin.set_fact:
__sap_maintain_etc_hosts_domain:
"{{ thishost.node_domain if thishost.node_domain is defined and thishost.node_domain != ''
else (sap_domain if sap_domain is defined and sap_domain != '' else ansible_facts['domain'] | d('')) }}"
- name: Verify that variable domain_name is set
ansible.builtin.assert:
that: >
not( ( __sap_maintain_etc_hosts_domain is undefined) or
( __sap_maintain_etc_hosts_domain is none) or
( __sap_maintain_etc_hosts_domain | trim == '') )
msg: |
"The DNS domain of this host not known. You can solve this problem by
configuring your DNS accordingly or by setting one of the following variables:
- sap_domain
- sap_maintain_etc_hosts_list, member node_domain"
- name: Set default values
ansible.builtin.set_fact:
__sap_maintain_etc_hosts_comment: "{{ (thishost.node_comment | d('') + ' ' + thishost.hana_site | d('')) | trim }}"
__sap_maintain_etc_hosts_alias_mode: "{{ thishost.alias_mode | default('merge') }}"
- name: Prepend Hashtag to comment
when: __sap_maintain_etc_hosts_comment|length > 0
ansible.builtin.set_fact:
__sap_maintain_etc_hosts_comment: "# {{ __sap_maintain_etc_hosts_comment }}"
# The following block reads the existing aliases of a host from /etc/hosts
# and merges it with the defined aliases in the struct
#
# 1. select the line where the first entry is the ip-adress thishost.node_ip
# 2. loop over all hostname entries in the selected line (2 bis NF=last element in line)
# 3. stop looping when a comment sign is found (because these are comments)
# 4. print an element if it is not the hostname or FQDN we want to add
#
# => __sap_maintain_etc_hosts_register_aliases.stdout contains a list of aliases of thishost.node_ip
#
# trunk-ignore(checkov/CKV2_ANSIBLE_3)
- name: Merge existing aliases with new alias list
when: __sap_maintain_etc_hosts_alias_mode != "overwrite"
block:
- name: Get all existing hostname aliases of {{ thishost.node_ip }}
ansible.builtin.shell: |
awk '( $1 == "{{ thishost.node_ip }}" ) {
for (i=2; i<=NF; ++i) {
if ( $i == "#" ) { break }
if (( $i != "{{ thishost.node_name }}" ) && ( $i != "{{ thishost.node_name }}.{{ __sap_maintain_etc_hosts_domain }}" )) { printf("%s ",$i) }
}
}' "{{ __sap_maintain_etc_hosts_file }}"
register: __sap_maintain_etc_hosts_register_aliases
changed_when: false
- name: Add defined aliases
ansible.builtin.set_fact:
__sap_maintain_etc_hosts_aliases: "{{ (__sap_maintain_etc_hosts_register_aliases.stdout.split(' ')
+ thishost.aliases | d([])) | unique | join(' ') | trim }}"
- name: Overwrite existing aliases
when: __sap_maintain_etc_hosts_alias_mode == "overwrite"
ansible.builtin.set_fact:
__sap_maintain_etc_hosts_aliases: "{{ thishost.node_aliases | d([]) | unique | join(' ') }}"
- name: Display host and domain name, and IP address before the modification
ansible.builtin.debug:
msg:
- "hostname = '{{ thishost.node_name }}'"
- "domain = '{{ __sap_maintain_etc_hosts_domain }}'"
- "ip = '{{ thishost.node_ip }}'"
- "comment = '{{ __sap_maintain_etc_hosts_comment }}'"
- "aliases = '{{ __sap_maintain_etc_hosts_aliases }}'"
- "alias mode = '{{ __sap_maintain_etc_hosts_alias_mode }}'"
# We allow more than one line containing sap_ip:
- name: Check for duplicate entries of {{ thishost.node_ip }}
ansible.builtin.shell: |
n=$(grep "^{{ thishost.node_ip }}\s" {{ __sap_maintain_etc_hosts_file }} | wc -l)
if [ $n -gt 1 ]; then
echo "Duplicate IP entry in {{ __sap_maintain_etc_hosts_file }}!"
exit 1
else
exit 0
fi
register: __sap_maintain_etc_hosts_register_duplicate_ip_check
changed_when: false
ignore_errors: true
when: not ansible_check_mode
- name: Report if there is more than one line with the IP address
ansible.builtin.debug:
msg:
- "More than one line containing {{ thishost.node_ip }}. File {{ __sap_maintain_etc_hosts_file }} will not be modified."
when:
- not ansible_check_mode
- __sap_maintain_etc_hosts_register_duplicate_ip_check.rc == 1
- name: Ensure that thisthost.node_name is not part of the localhost entry
ansible.builtin.replace:
path: "{{ __sap_maintain_etc_hosts_file }}"
regexp: '^(127.0.0.1 .*)\s{{ line_item }}(\s.*)$'
replace: '\1\2'
backup: true
when:
- not ansible_check_mode
- __sap_maintain_etc_hosts_register_duplicate_ip_check.rc == 0
become_user: root
become: true
loop:
- "{{ thishost.node_name }}.{{ __sap_maintain_etc_hosts_domain }}"
- "{{ thishost.node_name }}"
loop_control:
loop_var: line_item
- name: Ensure that the entry in hosts file is correct
ansible.builtin.lineinfile:
path: "{{ __sap_maintain_etc_hosts_file }}"
regexp: '^{{ thishost.node_ip }}\s'
line: "{{ thishost.node_ip }} {{ thishost.node_name }}.{{ __sap_maintain_etc_hosts_domain }} {{ thishost.node_name }} {{ __sap_maintain_etc_hosts_aliases }} {{ __sap_maintain_etc_hosts_comment }}"
backup: true
when:
- not ansible_check_mode
- __sap_maintain_etc_hosts_register_duplicate_ip_check.rc == 0
become_user: root
become: true
# After all nodes are added or deleted, run the consistency check against the hosts file
- name: Check for duplicate or missing entries of hostname and fqdn in {{ __sap_maintain_etc_hosts_file }}
ansible.builtin.shell: |
n=$(awk 'BEGIN{a=0}!/^#/&&(/^{{ line_item }}\s/||/\s{{ line_item }}\s/||/\s{{ line_item }}$/){a++}END{print a}' {{ __sap_maintain_etc_hosts_file }})
if [ $n -eq 1 ]; then
exit 0
else
exit 1
fi
loop:
- "{{ thishost.node_name }}.{{ __sap_maintain_etc_hosts_domain }}"
- "{{ thishost.node_name }}"
changed_when: false
loop_control:
loop_var: line_item
when: not ansible_check_mode
- name: Perform the hosts file completeness check
ansible.builtin.command: awk 'BEGIN{a=0}!/^#/&&(/{{ thishost.node_ip }}/&&/{{ thishost.node_name }}.{{ __sap_maintain_etc_hosts_domain }}/&&/{{ thishost.node_name }}/){a++}END{print a}' {{ __sap_maintain_etc_hosts_file }}
register: __sap_maintain_etc_hosts_register_ipv4_fqdn_sap_hostname_once_check
changed_when: false
- name: Display the output of the hosts file completeness check
ansible.builtin.debug:
msg: |
stdout:
{{ __sap_maintain_etc_hosts_register_ipv4_fqdn_sap_hostname_once_check.stdout | d('') }}
stderr:
{{ __sap_maintain_etc_hosts_register_ipv4_fqdn_sap_hostname_once_check.stderr | d('') }}
- name: Display the expected output of the hosts file completeness check
ansible.builtin.debug:
msg:
- "The expected entries in {{ __sap_maintain_etc_hosts_file }} are at least only once:"
- "{{ thishost.node_ip }} {{ thishost.node_name }}.{{ __sap_maintain_etc_hosts_domain }} {{ thishost.node_name }}"
when:
- __sap_maintain_etc_hosts_register_ipv4_fqdn_sap_hostname_once_check.stdout != "1"
- name: Fail if ip4 address, FQDN, or hostname are not in hosts file
ansible.builtin.fail:
msg:
- "Server's ip4 address, FQDN, or hostname are not in {{ __sap_maintain_etc_hosts_file }}!"
- "Expected:"
- "{{ thishost.node_ip }} {{ thishost.node_name }}.{{ __sap_maintain_etc_hosts_domain }} {{ thishost.node_name }}"
when:
- __sap_maintain_etc_hosts_register_ipv4_fqdn_sap_hostname_once_check.stdout != "1"
ignore_errors: "{{ ansible_check_mode }}"