服务器数据盘初始化

 group_vars目录下的变量定义文件init文件

disk_partitions:
  - number: 1
    start: 0%
    end: 50%
    device: /dev/sdb
    mount: /opt
  - number: 2
    start: 50%
    end: 100%
    device: /dev/sdb
    mount: /data

 tasks目录下的main.yml

---
- name: disk | primary | partition
  parted:
    device: "{
    { item.device }}"
    number: "{
    { item.number }}"
    label: gpt
    part_start: "{
    { item.start }}"
    part_end: "{
    { item.end }}"
    part_type: primary
    state: present
  with_items:
    - "{
    { disk_partitions }}"
  tags:
    - disk_primary_partition


- name: disk | ext4 | filesystem
  filesystem:
    fstype: ext4
    dev: "{
    { item.device }}{
    { item.number }}"
    force: yes
  with_items:
   - "{
    { disk_partitions }}"
  tags:
    - disk_ext4_filesystem


- name: disk | mount | dir
  mount:
    fstype: ext4
    src: "{
    { item.device }}{
    { item.number }}"
    path: "{
    { item.mount }}"
    state: mounted
  with_items:
    - "{
    { disk_partitions }}"
  tags:
    - disk_mount_dir

执行命令,这里 -e 可以自定义参数。

ansible-playbook  -i env/init  init.yml  -e '{"disk_partitions":[{"number":"1","start":"0%","end":"50%","device":"/dev/sdb","mount":"/opt"},{"number":"2","start":"50%","end":"100%","device":"/dev/sdb","mount":"/data"}]}'

 

你可能感兴趣的:(服务器数据盘初始化)