Skip to content

Commit 05a86d2

Browse files
author
Markus Koch
committed
most remarks fixed
1 parent 32b06cd commit 05a86d2

File tree

7 files changed

+71
-26
lines changed

7 files changed

+71
-26
lines changed

playbooks/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
# List of playbooks
3+
4+
- prepare-for-hana
5+
- prepare-for-netweaver
6+
- install-sap-hana
7+
- install-sap-hana-cluster
8+
- install-sap-hana-s4

roles/update_etc_hosts/README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Role Name
22
=========
33

4-
This role can be used to reliably update the /etc/hosts file
4+
This role can be used to reliably update the /etc/hosts file.
55

66
<!---
77
Requirements
@@ -13,7 +13,8 @@ Requirements
1313
Role Variables
1414
--------------
1515

16-
This role needs a a dictonary `update_etc_hosts_list` which contains the parameters for the hostfile
16+
This role needs a a dictonary `update_etc_hosts_list` which contains the parameters for the hostfile.
17+
It can be used for adding and deleting entries from `/etc/hosts`
1718

1819
<!---
1920
Dependencies
@@ -43,6 +44,7 @@ If you want to setup/add entries your etc hosts you can use this snippet
4344
node_name: host2
4445
state: absent
4546
```
47+
4648
If you have defined a cluster and the variable `sap_ha_pacemaker_cluster_cluster_nodes` or `sap_hana_cluster_nodes` is set, you can use the following play:
4749

4850
```[yaml]

roles/update_etc_hosts/meta/main.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ galaxy_info:
77

88
license: Apache-2.0
99

10-
min_ansible_version: "2.12"
10+
min_ansible_version: "2.11"
1111

1212
platforms:
1313
- name: "EL"
1414
versions:
15+
- "7"
1516
- "8"
1617
- "9"
1718
- name: "SLES"

roles/update_etc_hosts/tasks/main.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
---
22
# tasks file for update_etc_hosts
3+
- name: Ensure required defaults are read from setup module
4+
when: >
5+
not ((ansible_hostname is defined) and
6+
(ansible_domain is defined) and
7+
(ansible_default_ipv4 is defined))
8+
ansible.builtin.setup:
9+
filter:
10+
- 'ansible_hostname'
11+
- 'ansible_domain'
12+
- 'ansible_default_ipv4'
13+
14+
# TODO: Double Check that list entries with state present do not have duplicate IP adresses
15+
316
- name: Add entry to "{{ __update_etc_hosts_file }}"
417
loop: "{{ update_etc_hosts_list }}"
518
loop_control:

roles/update_etc_hosts/tasks/update_host_absent.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
when:
1515
- thishost.node_ip is defined
1616
- not ansible_check_mode
17+
become_user: root
18+
become: true
1719

1820
- name: Ensure that the entry all enries in hosts file are removed with name {{ thishost.node_name | d('undefined') }}
1921
ansible.builtin.lineinfile:
@@ -25,6 +27,8 @@
2527
- thishost.node_name is defined
2628
- thishost.node_domain is undefined
2729
- not ansible_check_mode
30+
become_user: root
31+
become: true
2832

2933
- name: Ensure that the entry all enries in hosts file are removed with FQDN
3034
ansible.builtin.lineinfile:
@@ -36,3 +40,5 @@
3640
- thishost.node_name is defined
3741
- thishost.node_domain is defined
3842
- not ansible_check_mode
43+
become_user: root
44+
become: true

roles/update_etc_hosts/tasks/update_host_present.yml

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
---
2-
# TODO CHECK/SET Variables
1+
--- # trunk-ignore(checkov/CKV2_ANSIBLE_3)
32
- name: Verify that variable node_ip is set
43
ansible.builtin.assert:
54
that: not( ( thishost.node_ip is undefined) or ( thishost.node_ip is none) or ( thishost.node_ip | trim == '') )
6-
msg: "Variable 'node_ip' is undefined or empty. Please define it your host list"
5+
msg: "Variable 'node_ip' is undefined or empty. Please define it in your host list."
76

87
- name: Verify that variable node_ip is in the correct format
98
ansible.builtin.assert:
@@ -17,15 +16,16 @@
1716

1817
- name: Ensure node_domain is set
1918
ansible.builtin.set_fact:
20-
__update_etc_hosts_domain: "{{ thishost.node_domain | default(sap_domain | default(ansible_domain)) }}"
19+
__update_etc_hosts_domain: "{{ thishost.node_domain | default(sap_domain) | default(ansible_domain) }}"
2120

21+
# Necessary, if defaults are both undefined
2222
- name: Verify that variable domain_name is set
2323
ansible.builtin.assert:
2424
that: >
2525
not( ( __update_etc_hosts_domain is undefined) or
2626
( __update_etc_hosts_domain is none) or
2727
( __update_etc_hosts_domain | trim == '') )
28-
msg: "Variable 'node_name' is undefined or empty. Please define it your host list"
28+
msg: "Variable 'domain_name' is undefined or empty. Please define it your host list"
2929

3030
- name: Set default values
3131
ansible.builtin.set_fact:
@@ -37,6 +37,17 @@
3737
ansible.builtin.set_fact:
3838
__update_etc_hosts_comment: "# {{ __update_etc_hosts_comment }}"
3939

40+
# The following block reads the existing aliases of a host from /etc/hosts
41+
# and merges it with the defined aliases in the struct
42+
#
43+
# 1. select the line, where the first entry is the ip-adress thishost.node_ip
44+
# 2. loop over all hostname entries in the selected line (2 bis NF=last element in line)
45+
# 3. stop looping when a comment sign is found (because these are comments)
46+
# 4. print an element, if it is not the hostname or FQDN we want to add
47+
#
48+
# => __update_etc_hosts_register_aliases.stdout contains a list of aliases of thishost.node_ip
49+
#
50+
# trunk-ignore(checkov/CKV2_ANSIBLE_3)
4051
- name: Merge existing aliases with new alis list
4152
when: __update_etc_hosts_alias_mode != "overwrite"
4253
block:
@@ -50,6 +61,7 @@
5061
}' "{{ __update_etc_hosts_file }}"
5162
register: __update_etc_hosts_register_aliases
5263
changed_when: false
64+
5365
- name: Add defined aliases
5466
ansible.builtin.set_fact:
5567
__update_etc_hosts_aliases: "{{ (__update_etc_hosts_register_aliases.stdout.split(' ') + thishost.aliases | d([])) | unique | join(' ') }}"
@@ -101,6 +113,8 @@
101113
when:
102114
- not ansible_check_mode
103115
- __update_etc_hosts_register_duplicate_ip_check.rc == 0
116+
become_user: root
117+
become: true
104118

105119
- name: Check for duplicate or missing entries of hostname and fqdn in {{ __update_etc_hosts_file }}
106120
ansible.builtin.shell: |
@@ -111,8 +125,8 @@
111125
exit 1
112126
fi
113127
loop:
114-
- '{{ thishost.node_name }}.{{ __update_etc_hosts_domain }}'
115-
- '{{ thishost.node_name }}'
128+
- "{{ thishost.node_name }}.{{ __update_etc_hosts_domain }}"
129+
- "{{ thishost.node_name }}"
116130
changed_when: false
117131
loop_control:
118132
loop_var: line_item
@@ -127,13 +141,14 @@
127141

128142
- name: Display the output of the hosts file completeness check
129143
ansible.builtin.debug:
130-
var: __update_etc_hosts_register_ipv4_fqdn_sap_hostname_once_check.stdout_lines,
131-
__update_etc_hosts_register_ipv4_fqdn_sap_hostname_once_check.stderr_lines
144+
var:
145+
__update_etc_hosts_register_ipv4_fqdn_sap_hostname_once_check.stdout_lines,
146+
__update_etc_hosts_register_ipv4_fqdn_sap_hostname_once_check.stderr_lines
132147

133148
- name: Display the expected output of the hosts file completeness check
134149
ansible.builtin.debug:
135150
msg:
136-
- "Expected:"
151+
- "The minimum expected entries in {{ __update_etc_hosts_file }} are:"
137152
- "{{ thishost.node_ip }} {{ thishost.node_name }}.{{ __update_etc_hosts_domain }} {{ thishost.node_name }}"
138153
when:
139154
- __update_etc_hosts_register_ipv4_fqdn_sap_hostname_once_check.stdout != "1"

roles/update_etc_hosts/tests/test.yml

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
---
22
# you can run 'ansible-playbook test.yml -e __update_etc_hosts_file=./test.hosts' for testing this role
3-
- name: test play
3+
- name: Test play
44
hosts: localhost
55
remote_user: root
66
become: true
77

88
vars:
9-
update_etc_hosts_list:
10-
- node_ip: 1.2.3.4
11-
node_name: host1
12-
node_domain: abc.de
13-
aliases:
14-
- alias1
15-
- anotheralias2
16-
node_comment: "Here comes text after hashsign"
17-
state: present
18-
- node_ip: 1.2.3.5
19-
node_name: host2
20-
state: absent
9+
update_etc_hosts_list:
10+
- node_ip: 1.2.3.4
11+
node_name: host1
12+
node_domain: abc.de
13+
aliases:
14+
- alias1
15+
- anotheralias2
16+
node_comment: "Here comes text after hashsign"
17+
state: present
18+
- node_ip: 1.2.3.5
19+
node_name: host2
20+
state: absent
2121

2222
roles:
2323
- update_etc_hosts

0 commit comments

Comments
 (0)