Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sap_maintain_etc_hosts: Fix wrong assert messages #668

Merged
merged 3 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions roles/sap_maintain_etc_hosts/tasks/update_host_absent.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
ansible.builtin.assert:
that: thishost.node_ip | regex_search(sap_maintain_etc_hosts_regexp_ipv4) or
thishost.node_ip | regex_search(sap_maintain_etc_hosts_regexp_ipv6)
msg: "Variable 'node_ip' is not an IP address. Please use the correct format"
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"
when: thisnode.node_ip is defined

- name: Ensure that either IP address or hostname is defined
Expand All @@ -13,7 +17,7 @@
((thishost.node_ip is undefined) and (thishost.node_name is defined))
msg: "Invalid delete item. Please define either node_ip only or node_name. In the latter case node_domain is optional."

- name: Ensure that the entry all entries in hosts file are removed with IP {{ thishost.node_ip | d('undefined') }}
- name: Ensure that all entries with IP {{ thishost.node_ip | d('undefined') }} in /etc/hosts are absent
berndfinger marked this conversation as resolved.
Show resolved Hide resolved
ansible.builtin.lineinfile:
path: "{{ __sap_maintain_etc_hosts_file }}"
regexp: '^{{ thishost.node_ip }}\s'
Expand All @@ -26,7 +30,7 @@
become_user: root
become: true

- name: Ensure that the entry all entries in hosts file are removed with name {{ thishost.node_name | d('undefined') }}
- name: Ensure that all entries with name {{ thishost.node_name | d('undefined') }} in /etc/hosts are absent
berndfinger marked this conversation as resolved.
Show resolved Hide resolved
ansible.builtin.lineinfile:
path: "{{ __sap_maintain_etc_hosts_file }}"
regexp: '^.*\s{{ thishost.node_name }}\s'
Expand All @@ -40,7 +44,7 @@
become_user: root
become: true

- name: Ensure that the entry all enries in hosts file are removed with FQDN
- name: Ensure that all enries with the specified FQDN in /etc/hosts are absent
berndfinger marked this conversation as resolved.
Show resolved Hide resolved
ansible.builtin.lineinfile:
path: "{{ __sap_maintain_etc_hosts_file }}"
regexp: '^.*\s{{ thishost.node_name + "." + thishost.node_domain }}\s'
Expand Down
31 changes: 23 additions & 8 deletions roles/sap_maintain_etc_hosts/tasks/update_host_present.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,46 @@
- 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: "Variable 'node_ip' is undefined or empty. Please define it in your host list."
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 in the correct format
- 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) or
thishost.node_ip | regex_search(sap_maintain_etc_hosts_regexp_ipv6)
msg: "Variable 'node_ip' is not an IP address. Please use the correct format"
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: "Variable 'node_name' is undefined or empty. Please define it your host list"
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 | default(sap_domain) | default(ansible_domain) }}"

# Necessary, if defaults are both undefined
- 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: "Variable 'domain_name' is undefined or empty. Please define it your host list"
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:
Expand All @@ -41,10 +56,10 @@
# 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
# 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
# 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
#
Expand Down
Loading