|
| 1 | +# An ansible playbook to configure the SSSD configuration to work with GLauth LDAP. |
| 2 | + |
| 3 | +- hosts: openhpc |
| 4 | + become: true |
| 5 | + vars: |
| 6 | + ldap_server: "{{ groups['login'] | first }}" |
| 7 | + tasks: |
| 8 | + - name: Install ldapsearch and sssd-ldap |
| 9 | + dnf: |
| 10 | + name: |
| 11 | + - openldap-clients |
| 12 | + - sssd-ldap |
| 13 | + state: present |
| 14 | + |
| 15 | + - name: Read LDAP configuration |
| 16 | + ansible.builtin.slurp: |
| 17 | + src: /etc/glauth/config.cfg |
| 18 | + register: file_content |
| 19 | + delegate_to: "{{ ldap_server }}" |
| 20 | + |
| 21 | + - name: Decode the LDAP configuration content |
| 22 | + ansible.builtin.set_fact: |
| 23 | + ldap_config_decoded: "{{ file_content['content'] | b64decode }}" |
| 24 | + |
| 25 | + - name: Extract LDAP URI |
| 26 | + ansible.builtin.set_fact: |
| 27 | + ldap_uri: "{{ ldap_config_decoded | regex_search('listen\\s*=\\s*\"([^\"]+)\"', '\\1') | first | regex_replace('0.0.0.0', ansible_default_ipv4.address) }}" |
| 28 | + |
| 29 | + - name: Extract Base DN |
| 30 | + ansible.builtin.set_fact: |
| 31 | + ldap_search_base: "{{ ldap_config_decoded | regex_search('baseDN\\s*=\\s*\"([^\"]+)\"', '\\1') | first }}" |
| 32 | + |
| 33 | + - name: Extract LDAP Group format |
| 34 | + ansible.builtin.set_fact: |
| 35 | + ldap_group_name: "{{ ldap_config_decoded | regex_search('groupformat\\s*=\\s*\"([^\"]+)\"', '\\1') | first }}" |
| 36 | + |
| 37 | + # LDAP Service Account Password to be used |
| 38 | + # for ldapsearch and sssd configuration. |
| 39 | + - name: Read LDAP admin configuration |
| 40 | + ansible.builtin.slurp: |
| 41 | + src: /etc/glauth/refresher.env |
| 42 | + register: admin_file_content |
| 43 | + delegate_to: "{{ ldap_server }}" |
| 44 | + |
| 45 | + - name: Decode the LDAP admin configuration content |
| 46 | + ansible.builtin.set_fact: |
| 47 | + ldap_admin_config_decoded: "{{ admin_file_content['content'] | b64decode }}" |
| 48 | + |
| 49 | + - name: Extract LDAP admin password |
| 50 | + ansible.builtin.set_fact: |
| 51 | + ldap_admin_password: "{{ ldap_admin_config_decoded | regex_search('^LDAP_ADMIN_PASSWORD=([^\\n]+)', '\\1', multiline=True) | first }}" |
| 52 | + |
| 53 | + # Gather 'groups' variables from LDAP |
| 54 | + # structure by probing with ldapsearch. |
| 55 | + - name: Execute ldapsearch to get group information |
| 56 | + ansible.builtin.command: |
| 57 | + cmd: "ldapsearch -x -H ldap://{{ ldap_uri }} -D 'cn=admin,{{ ldap_search_base }}' -w {{ ldap_admin_password }} -b '{{ ldap_group_name }}=groups,{{ ldap_search_base }}'" |
| 58 | + # Should result in a command like: ldapsearch -x -H ldap://0.0.0.0:3893 -D "cn=admin,dc=glauth,dc=com" -w Adm1n! -b "ou=groups,dc=glauth,dc=com" |
| 59 | + register: ldapsearch_output |
| 60 | + ignore_errors: yes |
| 61 | + |
| 62 | + - name: Check if gidNumber is present and set ldap_group_gid_number |
| 63 | + ansible.builtin.set_fact: |
| 64 | + ldap_group_gid_number: "{{ 'ldap_group_gid_number = gidNumber' if ('gidNumber: ' in ldapsearch_output.stdout) else ' ' }}" |
| 65 | + |
| 66 | + - name: Extract ldap_group_object_class for entries with gidNumber and not organizationalUnit |
| 67 | + ansible.builtin.set_fact: |
| 68 | + ldap_group_object_class: "{{ ('ldap_group_object_class = ' + result) if result != '' else '' }}" |
| 69 | + vars: |
| 70 | + result: "{{ ldapsearch_output.stdout | regex_findall('(?ms)^dn:.+?(?:objectClass: ((?!organizationalUnit)[\\w-]+)).+?gidNumber: \\d+', '\\1') | reject('search', '^(organizationalUnit|top)$') | first | default('', true) }}" |
| 71 | + |
| 72 | + - name: Template sssd.conf file |
| 73 | + ansible.builtin.template: |
| 74 | + src: sssd.conf.j2 |
| 75 | + dest: /etc/sssd/sssd.conf |
| 76 | + owner: root |
| 77 | + group: root |
| 78 | + mode: '0600' |
| 79 | + register: sssd_configured |
| 80 | + |
| 81 | + - name: Restart SSSD service |
| 82 | + ansible.builtin.systemd: |
| 83 | + name: sssd |
| 84 | + state: restarted |
| 85 | + when: sssd_configured.changed |
| 86 | + |
| 87 | +# # The login node requires some special previlages in order to |
| 88 | +# # allow the login node to create the home directories for the users. |
| 89 | +# - hosts: login |
| 90 | +# become: true |
| 91 | +# tasks: |
| 92 | +# - name: Create 'common-session' file for pam_mkhomedir.so |
| 93 | +# ansible.builtin.copy: |
| 94 | +# content: | |
| 95 | +# # Ensure session management is handled through SSSD, which interfaces with LDAP |
| 96 | +# session required pam_mkhomedir.so skel=/etc/skel/ umask=0022 |
| 97 | +# session required pam_sss.so |
| 98 | + |
| 99 | +# # This line is optional but recommended for logging |
| 100 | +# #session optional pam_syslog.so |
| 101 | + |
| 102 | +# # Include this for environment variable management |
| 103 | +# #session required pam_env.so |
| 104 | + |
| 105 | +# # This line is for setting user limits |
| 106 | +# #session required pam_limits.so |
| 107 | +# dest: /etc/pam.d/common-session |
| 108 | +# owner: root |
| 109 | +# group: root |
| 110 | +# mode: '0644' |
| 111 | +# register: common_session_created |
| 112 | + |
| 113 | +# - name: Ensure 'sss' is configured in nsswitch.conf for passwd and group |
| 114 | +# ansible.builtin.lineinfile: |
| 115 | +# path: /etc/nsswitch.conf |
| 116 | +# regexp: '^({{ item }}:\s+)' |
| 117 | +# line: '\1:files sss systemd' |
| 118 | +# backrefs: yes |
| 119 | +# register: sss_added |
| 120 | + |
| 121 | +# - name: Ensure 'sss' is configured in nsswitch.conf for group |
| 122 | +# ansible.builtin.lineinfile: |
| 123 | +# path: /etc/nsswitch.conf |
| 124 | +# regexp: '^(group):\s+files' |
| 125 | +# line: '\1: files sss systemd' |
| 126 | +# backrefs: yes |
| 127 | +# register: sss_added |
| 128 | + |
| 129 | +# # May not need this, but just in case. |
| 130 | +# # - name: Ensure 'sss' is configured in nsswitch.conf for various services |
| 131 | +# # ansible.builtin.lineinfile: |
| 132 | +# # path: /etc/nsswitch.conf |
| 133 | +# # regexp: "^{{ item.service }}:\\s+files" |
| 134 | +# # line: "{{ item.service }}: sss files" |
| 135 | +# # loop: |
| 136 | +# # - { service: "netgroup" } |
| 137 | +# # - { service: "automount" } |
| 138 | +# # - { service: "services" } |
| 139 | +# # register: sss_added_two |
| 140 | + |
| 141 | +# # Restart Waldur site agents and Glauth services and make |
| 142 | +# # sure that the Waldur config scripts have been exceuted. |
| 143 | +# # This should only run if the LDAP configuration has been |
| 144 | +# # changed. |
| 145 | +# - name: Restart Services |
| 146 | +# block: |
| 147 | +# - name: Restart Waldur site agents |
| 148 | +# ansible.builtin.systemd: |
| 149 | +# name: waldur-agent-{{ item }} |
| 150 | +# state: restarted |
| 151 | +# loop: |
| 152 | +# - membership-sync |
| 153 | +# - order-process |
| 154 | +# - report |
| 155 | + |
| 156 | +# # Only way to refresh the GLauth config.cfg file is to first |
| 157 | +# # delete the tmp files and then restart the refresher service. |
| 158 | +# - name: Delete offering-users-config.cfg and prev-offering-users-config.cfg tmp files |
| 159 | +# ansible.builtin.file: |
| 160 | +# path: /tmp/{{ item }} |
| 161 | +# state: absent |
| 162 | +# with_items: |
| 163 | +# - offering-users-config.cfg |
| 164 | +# - prev-offering-users-config.cfg |
| 165 | + |
| 166 | +# - name: Restart GLauth service |
| 167 | +# ansible.builtin.systemd: |
| 168 | +# name: refresh-glauth-config |
| 169 | +# state: restarted |
| 170 | + |
| 171 | +# - name: Restart GLauth service |
| 172 | +# ansible.builtin.systemd: |
| 173 | +# name: glauth |
| 174 | +# state: restarted |
| 175 | +# when: common_session_created.changed or sss_added.changed #or sss_added_two.changed |
| 176 | + |
| 177 | +# - name: Load the Slurm resources types to Waldur |
| 178 | +# ansible.builtin.shell: /opt/waldur-env/bin/waldur_site_load_components |
| 179 | +# args: |
| 180 | +# chdir: /etc/waldur |
| 181 | + |
| 182 | +# - name: Make sure that there isn't a blocked workflow to create home directories. |
| 183 | +# ansible.builtin.shell: /opt/waldur-env/bin/waldur_slurm_create_homedirs |
| 184 | +# args: |
| 185 | +# chdir: /etc/waldur |
0 commit comments