对每个人而言,真正的职责只有一个:找到自我。然后在心中坚守其一生,全心全意,永不停息。所有其它的路都是不完整的,是人的逃避方式,是对大众理想的懦弱回归,是随波逐流,是对内心的恐惧 ——赫尔曼·黑塞《德米安》
涉及到的文件
[student@workstation filestorage-automation]$ tree .
.
├── ansible.cfg
├── inventory
├── smb_client.yml
├── smb_server.yml
├── smb_vars.yml
└── templates
└── smb.conf.j2
[student@workstation filestorage-automation]$
涉及到的 主机清单
[student@workstation filestorage-automation]$ cat inventory
[servers]
serverd.lab.example.com
[clients]
servera.lab.example.com
serverb.lab.example.com
serverc.lab.example.com
[student@workstation filestorage-automation]$
这里我们使用 serverd 做服务端,使用 servera,b,c 做客户端
[student@workstation filestorage-review]$ cat ansible.cfg
[defaults]
inventory=inventory
remote_user=devops
ansible 配置文件 ,使用 devops 作ssh 用户
samba 对应的配置文件的 jija 模版
[student@workstation filestorage-automation]$ cat templates/smb.conf.j2
[global]
workgroup = SAMBA
security = user
passdb backend = tdbsam
smb encrypt = required
server min protocol = SMB3
[{{ share_name }}]
path = {{ shared_dir }}
write list = @{{ allowed_group }}
[student@workstation filestorage-automation]$
通过模版配置文件,我们可以看到使用的是最基本的配置文件,下面为涉及到的变量
[student@workstation filestorage-review]$ cat smb_vars.yml
---
shared_dir: /srv/developers
share_name: devdata
mount_point: /devs_data
# User account for mounting the share
samba_usermount: sambamount
samba_passmount: redhat
allowed_group: developers
[student@workstation filestorage-review]$
服务端需要执行的剧本
Samba
软件包:使用yum模块安装Samba软件包。[student@workstation filestorage-automation]$ cat smb_server.yml
---
- name: Share a directory with SMB
hosts: serverd.lab.example.com
become: true
vars_files:
- smb_vars.yml
tasks:
- name: the samba package is installed
yum:
name: samba
state: present
# Creating the Linux and Samba user for the multiuser mount.
# That user is only used to mount the share.
- name: the Linux user for Samba mount exists
user:
name: "{{ samba_usermount }}"
shell: /sbin/nologin
create_home: no
system: yes
- name: the Samba user for Samba mount exists
command: smbpasswd -s -a {{ samba_usermount }}
args:
stdin: "{{ samba_passmount }}\n{{ samba_passmount }}"
# Group and users with write access to the share
- name: the Linux group exists
group:
name: "{{ allowed_group }}"
system: yes
- name: the Linux users exist for Samba users
user:
name: "{{ item['name'] }}"
shell: /sbin/nologin
groups:
- "{{ allowed_group }}"
loop: "{{ samba_users }}"
no_log: true
- name: the Samba users exist
command: smbpasswd -s -a {{ item['name'] }}
args:
stdin: "{{ item['password'] }}\n{{ item['password'] }}"
loop: "{{ samba_users }}"
no_log: true
- name: the directory exists
file:
path: "{{ shared_dir }}"
owner: root
group: "{{ allowed_group }}"
mode: '2775'
state: directory
setype: samba_share_t
- name: the directory is shared
template:
src: templates/smb.conf.j2
dest: /etc/samba/smb.conf
owner: root
group: root
mode: '0644'
setype: samba_etc_t
notify: reload smb
- name: the smb service is started and enabled
service:
name: smb
state: started
enabled: yes
- name: the samba firewall service is opened
firewalld:
service: samba
state: enabled
immediate: yes
permanent: yes
handlers:
- name: reload smb
service:
name: smb
state: reloaded
[student@workstation filestorage-automation]$
cifs-utils
软件包:使用yum模块确保目标主机上安装了cifs-utils软件包。[student@workstation filestorage-automation]$ cat smb_client.yml
---
- name: Access an SMB share
hosts: servera.lab.example.com
become: true
vars_files:
- smb_vars.yml
tasks:
- name: the cifs-utils package is installed
yum:
name: cifs-utils
state: present
- name: the credential file exists
copy:
content: "username={{ samba_usermount }}\n\
password={{ samba_passmount }}\n"
dest: /etc/samba/creds.txt
owner: root
group: root
mode: '0600'
no_log: true
- name: the SMB share is mounted
mount:
path: "{{ mount_point }}"
src: "//serverd.lab.example.com/{{ share_name }}"
opts: "credentials=/etc/samba/creds.txt,multiuser,seal"
state: mounted
fstype: cifs
- name: the Linux users exist
user:
name: "{{ item.name }}"
shell: /bin/bash
password: "{{ item.password | \
password_hash('sha512', 'redhatsalt') }}"
loop: "{{ samba_users }}"
no_log: true
[student@workstation filestorage-automation]$
© 文中涉及参考链接内容版权归原作者所有,如有侵权请告知,这是一个开源项目,如果你认可它,不要吝啬星星哦
红帽服务管理与自动化(RH358)
授课笔记
© 2018-2023 [email protected], All rights reserved. 保持署名-非商用-相同方式共享(CC BY-NC-SA 4.0)