Ansible-17-存储管理实战
//开始练习
[student@workstation ~]$ lab system-storage start
Setting up workstation for Guided Exercise (system-storage):
· Verifying Ansible installation.............................. SUCCESS
· Creating working directory.................................. SUCCESS
· Deploying Ansible inventory................................. SUCCESS
· Deploying ansible.cfg....................................... SUCCESS
· Downloading storage.yml file................................ SUCCESS
· Downloading storage_vars.yml file........................... SUCCESS
[student@workstation system-storage]$ ls
ansible.cfg inventory storage_vars.yml storage.yml
[student@workstation system-storage]$ cat ansible.cfg
[defaults]
remote_user=devops
inventory=./inventory
[privilege_escalation]
become=yes
become_method=sudo
[student@workstation system-storage]$ cat inventory
[webservers]
servera.lab.example.com
[student@workstation system-storage]$ cat storage_vars.yml
---
partitions:
- number: 1
start: 1MiB
end: 257MiB
volume_groups:
- name: apache-vg
devices: /dev/vdb1
logical_volumes:
- name: content-lv
size: 64M
vgroup: apache-vg
mount_path: /var/www
- name: logs-lv
size: 128M
vgroup: apache-vg
mount_path: /var/log/httpd
[student@workstation system-storage]$ cat storage.yml
---
- name: Ensure Apache Storage Configuration
hosts: webservers
vars_files:
- storage_vars.yml
tasks:
- name: Correct partitions exist on /dev/vdb
debug:
msg: TODO
loop: "{{ partitions }}"
- name: Ensure Volume Groups Exist
debug:
msg: TODO
loop: "{{ volume_groups }}"
- name: Create each Logical Volume (LV) if needed
debug:
msg: TODO
loop: "{{ logical_volumes }}"
when: true
- name: Ensure XFS Filesystem exists on each LV
debug:
msg: TODO
loop: "{{ logical_volumes }}"
- name: Ensure the correct capacity for each LV
debug:
msg: TODO
loop: "{{ logical_volumes }}"
- name: Each Logical Volume is mounted
debug:
msg: TODO
loop: "{{ logical_volumes }}"
//playbook编写
ansible.cfg inventory storage_vars.yml storage.yml
[student@workstation system-storage]$ cat storage.yml
---
- name: Ensure Apache Storage Configuration
hosts: webservers
vars_files:
- storage_vars.yml
tasks:
- name: Correct partitions exist on /dev/vdb
parted:
device: /dev/vdb
state: present
number: "{{ item.number }}"
part_start: "{{ item.start }}"
part_end: "{{ item.end }}"
loop: "{{ partitions }}"
- name: Ensure Volume Groups Exist
lvg:
vg: "{{ item.name }}"
pvs: "{{ item.devices }}"
loop: "{{ volume_groups }}"
- name: Create each Logical Volume (LV) if needed
lvol:
vg: "{{ item.vgroup }}"
lv: "{{ item.name }}"
size: "{{ item.size }}"
loop: "{{ logical_volumes }}"
when: item.name not in ansible_lvm["lvs"]
- name: Ensure XFS Filesystem exists on each LV
filesystem:
dev: "/dev/{{ item.vgroup }}/{{ item.name }}"
ftype: xfs
loop: "{{ logical_volumes }}"
- name: Ensure the correct capacity for each LV
lvol:
vg: "{{ item.vgroup }}"
lv: "{{ item.name }}"
size: "{{ item.size }}"
resizefs: yes
force: yes
loop: "{{ logical_volumes }}"
- name: Each Logical Volume is mounted
mount:
path: "{{ item.mount_path }}"
src: "/dev/{{ item.vgroup}}/{{ item.name }}"
fstype: xfs
opts: noatime
state: mounted
loop: "{{ logical_volumes }}"