Skip to content

Commit

Permalink
refactored all code to convert results dictionary to object
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvmicrosoft committed Dec 3, 2024
1 parent b21a2f5 commit 85671c6
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,60 +39,60 @@ def run_module():
distribution_full_id=dict(type="str", required=True),
)

result = {
"this_sid": {},
"all_sap_mounts": [],
"first_server_temp": [],
"mnt_options": {},
"nfs_service": "",
}
class Result:
def __init__(self):
self.this_sid = {}
self.all_sap_mounts = []
self.first_server_temp = []
self.mnt_options = {}
self.nfs_service = ""

module = AnsibleModule(argument_spec=module_args, supports_check_mode=True)
result = Result()

try:
distribution_full_id = module.params["distribution_full_id"]

result["this_sid"] = {
"sid": module.params["sap_sid"].upper(),
"dbsid_uid": module.params["hdbadm_uid"],
"sidadm_uid": (
module.params["asesidadm_uid"]
if module.params["platform"] == "SYSBASE"
else module.params["sidadm_uid"]
),
"ascs_inst_no": module.params["scs_instance_number"],
"pas_inst_no": module.params["pas_instance_number"],
"app_inst_no": module.params["app_instance_number"],
result.this_sid = {
"sid": module.params["sap_sid"].upper(),
"dbsid_uid": module.params["hdbadm_uid"],
"sidadm_uid": (
module.params["asesidadm_uid"]
if module.params["platform"] == "SYSBASE"
else module.params["sidadm_uid"]
),
"ascs_inst_no": module.params["scs_instance_number"],
"pas_inst_no": module.params["pas_instance_number"],
"app_inst_no": module.params["app_instance_number"],
}

if module.params["multi_sids"] is not None:
result["all_sap_mounts"] = module.params["multi_sids"]

if module.params["multi_sids"] is not None:
result.all_sap_mounts = module.params["multi_sids"]
else:
result["all_sap_mounts"].append(result["this_sid"])

except Exception as e:
module.fail_json(msg=str(e), **result)
result.all_sap_mounts.append(result.this_sid)

result.first_server_temp.append(module.params["server_name"])

result["first_server_temp"].append(module.params["server_name"])
result.mnt_options = {
"afs_mnt_options": "noresvport,vers=4,minorversion=1,sec=sys",
"anf_mnt_options": "rw,nfsvers=4.1,hard,timeo=600,rsize=262144,wsize=262144,noatime,lock,_netdev,sec=sys"
+ (",nconnect=8" if distribution_full_id in distro_versions else ""),
}

result["mnt_options"] = {
"afs_mnt_options": "noresvport,vers=4,minorversion=1,sec=sys",
"anf_mnt_options": "rw,nfsvers=4.1,hard,timeo=600,rsize=262144,wsize=262144,noatime,lock,_netdev,sec=sys"
+ (",nconnect=8" if distribution_full_id in distro_versions else ""),
}
nfs_service_mapping = {
"redhat8": "nfs-server",
"redhat9": "nfs-server",
"redhat7": "nfs",
"oraclelinux8": "rpcbind",
}
result.nfs_service = nfs_service_mapping.get(
distribution_full_id.split(".")[0], "nfs-server"
)

nfs_service_mapping = {
"redhat8": "nfs-server",
"redhat9": "nfs-server",
"redhat7": "nfs",
"oraclelinux8": "rpcbind",
}
result["nfs_service"] = nfs_service_mapping.get(
distribution_full_id.split(".")[0], "nfs-server"
)
except Exception as e:
module.fail_json(msg=str(e), **result.__dict__)

module.exit_json(**result)
module.exit_json(**result.__dict__)

if __name__ == "__main__":
run_module()
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,9 @@
- "{{ query('inventory_hostnames', '{{ sap_sid | upper }}_DB') }}"
register: config_sap_mounts_output

- name: setting fact for input parameters for readability. Will use input vars throughout the playbook
ansible.builtin.set_fact:
input_vars: "{{ config_sap_mounts_output.results[0] }}"

- name: Set first_server_temp to the value from config_sap_mounts_output
ansible.builtin.set_fact:
first_server_temp: "{{ input_vars['first_server_temp'] }}"
first_server_temp: "{{ config_sap_mounts_output.results.first_server_temp }}"

- name: "AFS Mount: Create configuration file
with additional optimization settings"
Expand Down Expand Up @@ -80,7 +76,7 @@
src: "{{ sap_mnt }}"
path: "/saptmp"
fstype: "nfs4"
opts: "{{ input_vars['mnt_options']['afs_mnt_options'] }}"
opts: "{{ config_sap_mounts_output.results.mnt_options['afs_mnt_options'] }}"
state: mounted

rescue:
Expand All @@ -97,7 +93,7 @@
src: "{{ sap_mnt }}"
path: "/saptmp"
fstype: "nfs4"
opts: "{{ input_vars['mnt_options']['afs_mnt_options'] }}"
opts: "{{ config_sap_mounts_output.results.mnt_options['afs_mnt_options'] }}"
state: mounted

- name: "AFS Mount: Create SAP Directories (AFS)"
Expand All @@ -120,7 +116,7 @@
owner: '{{ item.sidadm_uid }}'
group: sapsys
mode: 0755
loop: "{{ input_vars['all_sap_mounts'] }}"
loop: "{{ config_sap_mounts_output.results.all_sap_mounts }}"

- name: "AFS Mount: Unmount file systems (sapmnt)"
ansible.posix.mount:
Expand All @@ -142,14 +138,14 @@
ansible.builtin.file:
path: "/saptmp/sapmnt{{ item.sid | upper }}"
state: absent
loop: "{{ input_vars['all_sap_mounts'] }}"
loop: "{{ config_sap_mounts_output.results.all_sap_mounts }}"

- name: "AFS Mount: Cleanup fstab and directory (sapmnt)"
ansible.posix.mount:
src: "{{ sap_mnt }}"
path: "/saptmp"
fstype: "nfs4"
opts: "{{ input_vars['mnt_options']['afs_mnt_options'] }}"
opts: "{{ config_sap_mounts_output.results.mnt_options['afs_mnt_options'] }}"
state: absent
when:
- sap_mnt is defined
Expand Down Expand Up @@ -191,7 +187,7 @@
path: "/sapmnt/{{ item.sid }}"
state: directory
register: is_created_now
loop: "{{ input_vars['all_sap_mounts'] }}"
loop: "{{ config_sap_mounts_output.results.all_sap_mounts }}"

- name: "AFS Mount: Change attribute only when we create SAP Directories (sapmnt)"
ansible.builtin.file:
Expand Down Expand Up @@ -263,7 +259,7 @@
src: "{{ item.src }}"
path: "{{ item.path }}"
fstype: "{{ item.type }}"
opts: "{{ input_vars['mnt_options']['afs_mnt_options'] }}"
opts: "{{ config_sap_mounts_output.results.mnt_options['afs_mnt_options'] }}"
state: mounted
loop:
- { type: 'nfs4', src: '{{ sap_mnt }}/sapmnt{{ sap_sid | upper }}', path: '/sapmnt/{{ sap_sid | upper }}' }
Expand All @@ -273,7 +269,7 @@
src: "{{ item.src }}"
path: "{{ item.path }}"
fstype: "{{ item.type }}"
opts: "{{ input_vars['mnt_options']['afs_mnt_options'] }}"
opts: "{{ config_sap_mounts_output.results.mnt_options['afs_mnt_options'] }}"
state: unmounted
loop:
- { type: 'nfs4', src: '{{ sap_mnt }}/sapmnt{{ sap_sid | upper }}', path: '/sapmnt/{{ sap_sid | upper }}' }
Expand All @@ -290,7 +286,7 @@
src: "{{ item.src }}"
path: "{{ item.path }}"
fstype: "{{ item.type }}"
opts: "{{ input_vars['mnt_options']['afs_mnt_options'] }}"
opts: "{{ config_sap_mounts_output.results.mnt_options['afs_mnt_options'] }}"
state: mounted
loop:
- { type: 'nfs4', src: '{{ sap_mnt }}/sapmnt{{ sap_sid | upper }}', path: '/sapmnt/{{ sap_sid | upper }}' }
Expand All @@ -307,7 +303,7 @@
src: "{{ sap_mnt }}/sapmnt{{ item.sid }}"
path: "/sapmnt/{{ item.sid }}"
fstype: 'nfs4'
opts: "{{ input_vars['mnt_options']['afs_mnt_options'] }}"
opts: "{{ config_sap_mounts_output.results.mnt_options['afs_mnt_options'] }}"
state: mounted
loop: "{{ MULTI_SIDS }}"
when:
Expand All @@ -322,7 +318,7 @@
src: "{{ sap_mnt }}/sapmnt{{ sap_sid | upper }}"
path: "/sapmnt/{{ sap_sid | upper }}"
fstype: nfs4
opts: "{{ input_vars['mnt_options']['afs_mnt_options'] }}"
opts: "{{ config_sap_mounts_output.results.mnt_options['afs_mnt_options'] }}"
state: mounted
rescue:
- name: "AFS Mount: Pause for 15 seconds"
Expand All @@ -333,7 +329,7 @@
src: "{{ sap_mnt }}/sapmnt{{ sap_sid | upper }}"
path: "/sapmnt/{{ sap_sid | upper }}"
fstype: nfs4
opts: "{{ input_vars['mnt_options']['afs_mnt_options'] }}"
opts: "{{ config_sap_mounts_output.results.mnt_options['afs_mnt_options'] }}"
state: mounted

when:
Expand Down Expand Up @@ -371,7 +367,7 @@
src: "{{ item.src }}"
path: "{{ item.path }}"
fstype: "{{ item.type }}"
opts: "{{ input_vars['mnt_options']['afs_mnt_options'] }}"
opts: "{{ config_sap_mounts_output.results.mnt_options['afs_mnt_options'] }}"
state: mounted

loop:
Expand All @@ -391,7 +387,7 @@
'type': 'trans',
'temppath': 'saptrans',
'mount': '{{ sap_trans }}',
'opts': '{{ input_vars["mnt_options"]["afs_mnt_options"] }}',
'opts': '{{ config_sap_mounts_output.results.mnt_options["afs_mnt_options"] }}',
'path': '/usr/sap/trans',
'owner': "root",
'permissions': '0775',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,13 @@
ansible.builtin.set_fact:
db_hosts: "{{ query('inventory_hostnames', '{{ sap_sid | upper }}_DB') }}"

- name: setting fact for input parameters for readability. Will use input vars throughout the playbook
ansible.builtin.set_fact:
input_vars: "{{ config_sap_mounts_output.results[0] }}"

- name: Set first_server_temp to the value from config_sap_mounts_output
ansible.builtin.set_fact:
first_server_temp: "{{ input_vars['first_server_temp'] }}"
first_server_temp: "{{ config_sap_mounts_output.results.first_server_temp }}"

- name: "ANF Mount: Ensure the NFS service is stopped"
ansible.builtin.systemd:
name: "{{ input_vars['nfs_service'] }}"
name: "{{ config_sap_mounts_output.results.nfs_service }}"
state: stopped
when:
- "'scs' in supported_tiers"
Expand Down Expand Up @@ -117,7 +113,7 @@
seconds: 5
- name: "ANF Mount: Ensure the NFS service is restarted"
ansible.builtin.systemd:
name: "{{ input_vars['nfs_service'] }}"
name: "{{ config_sap_mounts_output.results.nfs_service }}"
state: restarted
when:
- id_mapping_changed is changed
Expand Down Expand Up @@ -270,7 +266,7 @@
path: "/sapmnt/{{ item.sid }}"
state: directory
register: is_created_now
loop: "{{ input_vars['all_sap_mounts'] }}"
loop: "{{ config_sap_mounts_output.results.all_sap_mounts }}"
when:
- tier == 'sapos'
- node_tier in ['app','scs','ers', 'pas'] or 'scs' in supported_tiers
Expand Down Expand Up @@ -597,7 +593,7 @@
'temppath': 'hanadata',
'folder': 'hanadata',
'mount': '{{ hana_data_mountpoint[0] }}',
'opts': '{{ input_vars["mnt_options"]["anf_mnt_options"] }}',
'opts': '{{ config_sap_mounts_output.results.mnt_options["anf_mnt_options"] }}',
'path': '/hana/data',
'permissions': '0755',
'set_chattr_on_dir': false,
Expand All @@ -619,7 +615,7 @@
'temppath': 'hanalog',
'folder': 'hanalog',
'mount' : '{{ hana_log_mountpoint[0] }}',
'opts': '{{ input_vars["mnt_options"]["anf_mnt_options"] }}',
'opts': '{{ config_sap_mounts_output.results.mnt_options["anf_mnt_options"] }}',
'path' : '/hana/log',
'permissions': '0755',
'set_chattr_on_dir': false,
Expand All @@ -641,7 +637,7 @@
'temppath': 'hanashared',
'folder': 'hanashared',
'mount': '{{ hana_shared_mountpoint[0] }}',
'opts': '{{ input_vars["mnt_options"]["anf_mnt_options"] }}',
'opts': '{{ config_sap_mounts_output.results.mnt_options["anf_mnt_options"] }}',
'path': '/hana/shared',
'permissions': '0775',
'set_chattr_on_dir': false,
Expand All @@ -663,7 +659,7 @@
'temppath': 'hanadata',
'folder': 'hanadata',
'mount': '{{ hana_data_mountpoint[1] }}',
'opts': '{{ input_vars["mnt_options"]["anf_mnt_options"] }}',
'opts': '{{ config_sap_mounts_output.results.mnt_options["anf_mnt_options"] }}',
'path': '/hana/data',
'permissions': '0755',
'set_chattr_on_dir': false,
Expand All @@ -686,7 +682,7 @@
'temppath': 'hanalog',
'folder': 'hanalog',
'mount' : '{{ hana_log_mountpoint[1] }}',
'opts': '{{ input_vars["mnt_options"]["anf_mnt_options"] }}',
'opts': '{{ config_sap_mounts_output.results.mnt_options["anf_mnt_options"] }}',
'path' : '/hana/log',
'permissions': '0755',
'set_chattr_on_dir': false,
Expand All @@ -709,7 +705,7 @@
'temppath': 'hanashared',
'folder': 'hanashared',
'mount': '{{ hana_shared_mountpoint[1] }}',
'opts': '{{ input_vars["mnt_options"]["anf_mnt_options"] }}',
'opts': '{{ config_sap_mounts_output.results.mnt_options["anf_mnt_options"] }}',
'path': '/hana/shared',
'permissions': '0775',
'set_chattr_on_dir': false,
Expand Down Expand Up @@ -775,7 +771,7 @@
# change folder to match the mount folder within the share
'folder': 'shared',
'mount': '{{ hana_shared_mountpoint[0] }}',
'opts': '{{ input_vars["mnt_options"]["anf_mnt_options"] }}',
'opts': '{{ config_sap_mounts_output.results.mnt_options["anf_mnt_options"] }}',
'path': '{{ hana_shared_basepath }}',
'permissions': '0775',
'set_chattr_on_dir': false,
Expand All @@ -802,7 +798,7 @@
# Logic : hana_shared_mountpoint[0] goes on odd numbered HANA hosts and hana_shared_mountpoint[1] goes on even numbered HANA hosts.
# 'mount': "{% if ansible_hostname in query('inventory_hostnames', '{{ sap_sid | upper }}_DB')[0::2] %}{{ hana_shared_mountpoint[0] }}{% else %}{{ hana_shared_mountpoint[1] }}{% endif %}",
'mount': "{% if site | default('SITE1') == 'SITE1' %}{{ hana_shared_mountpoint[0] }}{% else %}{{ hana_shared_mountpoint[1] }}{% endif %}",
'opts': '{{ input_vars["mnt_options"]["anf_mnt_options"] }}',
'opts': '{{ config_sap_mounts_output.results.mnt_options["anf_mnt_options"] }}',
'path': '{{ hana_shared_basepath }}',
'permissions': '0775',
'set_chattr_on_dir': false,
Expand All @@ -828,7 +824,7 @@
'temppath': 'usrsap',
'folder': "usr-sap-hanadb{{ lookup('ansible.utils.index_of', db_hosts, 'eq', ansible_hostname) }}",
'mount': '{{ hana_shared_mountpoint[0] }}',
'opts': '{{ input_vars["mnt_options"]["anf_mnt_options"] }}',
'opts': '{{ config_sap_mounts_output.results.mnt_options["anf_mnt_options"] }}',
'path': '{{ hana_usrsap_basepath }}',
'permissions': '0775',
'set_chattr_on_dir': false,
Expand Down Expand Up @@ -864,7 +860,7 @@
temppath: 'hanadata',
folder: 'hanadata',
mount: "{{ item }}",
opts: "{{ input_vars['mnt_options']['anf_mnt_options'] }}",
opts: "{{ config_sap_mounts_output.results.mnt_options['anf_mnt_options'] }}",
path: "{{ hana_data_basepath + '/mnt0000' + ( my_index + 1 )| string }}",
permissions: '0775',
set_chattr_on_dir: false,
Expand Down Expand Up @@ -916,7 +912,7 @@
temppath: 'hanalog',
folder: 'hanalog',
mount: "{{ item }}",
opts: "{{ input_vars['mnt_options']['anf_mnt_options'] }}",
opts: "{{ config_sap_mounts_output.results.mnt_options['anf_mnt_options'] }}",
path: "{{ '/hana/log/' + (db_sid | upper ) + '/mnt0000' + ( my_index + 1 )| string }}",
permissions: '0775',
set_chattr_on_dir: false,
Expand Down
Loading

0 comments on commit 85671c6

Please sign in to comment.