Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 544A43F8E2 for ; Fri, 12 Apr 2024 20:50:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712955006; cv=none; b=XMUMJStRJhTHo4ppE5+njaLk35fM/5Q+tUYeYO8xnl5pxR5MqshtpnRjHKx43cuMjX1SmqL9DmkpxwTTRsrpfCv0tkrQxt958cpjGV6eNmeHdt1tRhhlLGnj8SUu21McRBDCWB0ykyqs8XPSj7AS4JzlZj4PEvP795HgPqcA/sY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712955006; c=relaxed/simple; bh=CZLJDbXmnWXp1c0uzsxTI5j2fmYGuRBUlxhFyLZASHg=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Vt6IYdmgALpkmrCQuXAR5za1no8Q1uHaO3zG6JRQsX0i+ITmG1DtUD6ID7XSdEMLvwCrZ9a2rbqYvqLYSyZtavDQ37Lx/idrfhUju7fBcAiblQk8+Gy7I29bIctm/ULuEVg1Muht/Afyon+F4pX5hz9KsrVnY6tGyd/DMbuHOQo= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=ClVLQHQN; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="ClVLQHQN" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1712955003; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=he/Bo7gDlvQSW+o4oe6vYVTGpbURoNRUByDkojERh7w=; b=ClVLQHQNyefq+bgkJLjYpT9KjfPUxgd9lQxarT7MTbPqt0aZhDBH21yV4SwR6x2epGHQtk gh5Lq1zDL+b3YhxsLPoYO8BrD4J/suOj/YOkCANfn1nHA97PYFkJGilhBKFHVETiye21We xfJTRyRHDy5yKTfE/NRBJBJrtljFlG4= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-194-MWJCoqpJODSRXXRt2ufKrw-1; Fri, 12 Apr 2024 16:50:01 -0400 X-MC-Unique: MWJCoqpJODSRXXRt2ufKrw-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 947551C04B59 for ; Fri, 12 Apr 2024 20:50:01 +0000 (UTC) Received: from aion.redhat.com (unknown [10.22.9.2]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 830BF492BC7 for ; Fri, 12 Apr 2024 20:50:01 +0000 (UTC) Received: from aion.redhat.com (localhost [IPv6:::1]) by aion.redhat.com (Postfix) with ESMTP id 378A11483DF for ; Fri, 12 Apr 2024 16:50:01 -0400 (EDT) From: Scott Mayhew To: kdevops@lists.linux.dev Subject: [PATCH 4/6] playbooks: add a playbook to create smb shares Date: Fri, 12 Apr 2024 16:49:58 -0400 Message-ID: <20240412205000.2329837-5-smayhew@redhat.com> In-Reply-To: <20240412205000.2329837-1-smayhew@redhat.com> References: <20240412205000.2329837-1-smayhew@redhat.com> Precedence: bulk X-Mailing-List: kdevops@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.9 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true Yoinked almost entirely from nfsd_add_export, this playbook is for creating share on the SMB server. Signed-off-by: Scott Mayhew --- playbooks/roles/smbd_add_share/README.md | 57 ++++++++++ .../roles/smbd_add_share/defaults/main.yml | 5 + playbooks/roles/smbd_add_share/tasks/main.yml | 103 ++++++++++++++++++ .../roles/smbd_add_share/templates/share.j2 | 7 ++ 4 files changed, 172 insertions(+) create mode 100644 playbooks/roles/smbd_add_share/README.md create mode 100644 playbooks/roles/smbd_add_share/defaults/main.yml create mode 100644 playbooks/roles/smbd_add_share/tasks/main.yml create mode 100644 playbooks/roles/smbd_add_share/templates/share.j2 diff --git a/playbooks/roles/smbd_add_share/README.md b/playbooks/roles/smbd_add_share/README.md new file mode 100644 index 0000000..6d7bb0d --- /dev/null +++ b/playbooks/roles/smbd_add_share/README.md @@ -0,0 +1,57 @@ +smbd_add_share +================ + +The smbd_add_share role lets you create an SMB share on the Samba +server set up by kdevops. Basically a rip-off of nfsd_add_export. + +Requirements +------------ + +None. SMB server and LVM administrative tools are installed on the +kdevops Samba server automatically. + +Role Variables +-------------- + + * server_host: the hostname or IP address of the SMB server where + the new share is to be created + * share_volname: the name of the new share, to be created under + the /shares directory on the SMB server + * share_options: the share options for the new share (currently not used) + * share_fstype: the file system type of the new share + * share_size: the maximum size of the new share + * share_user: the owner of the new share + * share_group: the owner group of the new share + * share_mode: the mode bits for the new share's root directory + +Dependencies +------------ + +None. + +Example Playbook +---------------- + +Below is an example playbook task: + +``` +- name: Create SMB share for test vol + include_role: + name: smbd_add_share + vars: + server_host: "kdevops-smbd" + share_volname: "test" + share_fstype: "btrfs" + share_size: 20g + when: + - test_fstype == "cifs" + - test_cifs_use_kdevops_smbd|bool +``` + +For further examples refer to one of this role's users, the +[https://github.com/linux-kdevops/kdevops](kdevops) project. + +License +------- + +GPL-2.0+ diff --git a/playbooks/roles/smbd_add_share/defaults/main.yml b/playbooks/roles/smbd_add_share/defaults/main.yml new file mode 100644 index 0000000..4cbfa91 --- /dev/null +++ b/playbooks/roles/smbd_add_share/defaults/main.yml @@ -0,0 +1,5 @@ +# SPDX-License-Identifier GPL-2.0+ +--- +share_user: "root" +share_group: "root" +share_mode: "u=rwx,g=rwx,o=rwxt" diff --git a/playbooks/roles/smbd_add_share/tasks/main.yml b/playbooks/roles/smbd_add_share/tasks/main.yml new file mode 100644 index 0000000..4958d6a --- /dev/null +++ b/playbooks/roles/smbd_add_share/tasks/main.yml @@ -0,0 +1,103 @@ +--- +- name: Create a new LVM partition + become: yes + become_flags: 'su - -c' + become_method: sudo + delegate_to: "{{ server_host }}" + community.general.lvol: + vg: shares + lv: "{{ share_volname }}" + size: "{{ share_size }}" + when: + - share_fstype != "tmpfs" + +- name: Format new volume for {{ share_fstype }} + become: yes + become_flags: 'su - -c' + become_method: sudo + delegate_to: "{{ server_host }}" + filesystem: + fstype: "{{ share_fstype }}" + dev: "/dev/shares/{{ share_volname }}" + when: + - share_fstype != "tmpfs" + +- name: Mount volume under {{ smbd_share_path }} + become: yes + become_flags: 'su - -c' + become_method: sudo + delegate_to: "{{ server_host }}" + throttle: 1 # running this in parallel can clobber fstab changes + mount: + path: "{{ smbd_share_path }}/{{ share_volname }}" + src: "/dev/shares/{{ share_volname }}" + fstype: "{{ share_fstype }}" + state: mounted + when: + - share_fstype != "tmpfs" + +- name: Mount tmpfs under {{ smbd_share_path }} + become: yes + become_flags: 'su - -c' + become_method: sudo + delegate_to: "{{ server_host }}" + throttle: 1 # running this in parallel can clobber fstab changes + mount: + path: "{{ smbd_share_path }}/{{ share_volname }}" + src: "tmpfs" + fstype: "tmpfs" + state: mounted + when: + - share_fstype == "tmpfs" + +- name: Ensure {{ share_volname }} has correct permissions + become: yes + become_flags: 'su - -c' + become_method: sudo + delegate_to: "{{ server_host }}" + file: + state: directory + path: "{{ smbd_share_path }}/{{ share_volname }}" + owner: "{{ share_user }}" + group: "{{ share_group }}" + mode: "{{ share_mode }}" + +- name: Test whether SELinux is enabled + command: /usr/sbin/selinuxenabled + register: selinux_status + delegate_to: "{{ server_host }}" + changed_when: False + failed_when: False + +- name: Fix SELinux file context under new mount + become: yes + become_flags: 'su - -c' + become_method: sudo + delegate_to: "{{ server_host }}" + command: /usr/sbin/restorecon -R "{{ smbd_share_path }}/{{ share_volname }}" + when: selinux_status.rc == 0 + +- name: Create an SMB share for the new fs + become: yes + become_flags: 'su - -c' + become_method: sudo + delegate_to: "{{ server_host }}" + throttle: 1 # running this in parallel can clobber smb.conf changes + ansible.builtin.blockinfile: + dest: /etc/samba/smb.conf + block: "{{ lookup('template', 'share.j2') }}" + marker: "; {mark} share configuration for {{ share_volname }}" + marker_begin: "begin" + marker_end: "end" + insertafter: "EOF" + +- name: Reload the smb services + become: yes + become_flags: 'su - -c' + become_method: sudo + delegate_to: "{{ server_host }}" + ansible.builtin.systemd_service: + name: smb.service + enabled: true + state: reloaded + diff --git a/playbooks/roles/smbd_add_share/templates/share.j2 b/playbooks/roles/smbd_add_share/templates/share.j2 new file mode 100644 index 0000000..e40de38 --- /dev/null +++ b/playbooks/roles/smbd_add_share/templates/share.j2 @@ -0,0 +1,7 @@ +[{{ share_volname }}] + path={{ smbd_share_path }}/{{ share_volname }} + browseable = Yes + read only = No + create mask = 0664 + directory mask = 0775 + -- 2.44.0