请参阅下面的完整剧本示例。这个想法是把剧本分成两部分。在第一个块中获取磁盘信息并将其写入控制器的文件。在第二个块中创建分区。原因是为了让代码更安全、更健壮、更灵活。
- 第一部分是安全的。远程主机上没有任何变化。您可以在所有主机上运行一次并显示调试以检查详细信息。比如我们在4GB的闪存盘上测试
shell> parted -l
Model: Generic Flash Disk (scsi)
Disk /dev/sdb: 4089MB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
1 512B 34.1MB 34.1MB primary fat32 esp
设置变量
disk: sdb
part_size: 10
unit: MB
并运行剧本
shell> ansible-playbook pb.yml -e debug=true -e get_info=true
PLAY [localhost] *****************************************************************************
TASK [debug] *********************************************************************************
ok: [localhost] =>
msg: |-
disk: /dev/sdb
part_unit: MB
cache_update: False
cache_path: /tmp/localhost-sdb-part_info.json
TASK [get partition info] ********************************************************************
ok: [localhost]
TASK [debug] *********************************************************************************
ok: [localhost] =>
part_info:
changed: false
disk:
dev: /dev/sdb
logical_block: 512
model: Generic Flash Disk
physical_block: 512
size: 4089.0
table: msdos
unit: mb
failed: false
partitions:
- begin: 0.0
end: 34.1
flags:
- esp
fstype: fat32
name: ''
num: 1
size: 34.1
unit: mb
script: unit 'MB' print
TASK [debug] *********************************************************************************
ok: [localhost] =>
msg: |-
part_size: 10
last_number: 1
last_end: 34.1
next_number: 2
next_part_start: 34.1MB
next_part_end: 44.1MB
TASK [copy] **********************************************************************************
changed: [localhost]
TASK [set_fact] ******************************************************************************
skipping: [localhost]
TASK [debug] *********************************************************************************
skipping: [localhost]
TASK [debug] *********************************************************************************
skipping: [localhost]
TASK [create new partition] ******************************************************************
skipping: [localhost]
TASK [debug] *********************************************************************************
skipping: [localhost]
PLAY RECAP ***********************************************************************************
localhost: ok=5 changed=1 unreachable=0 failed=0 skipped=5 rescued=0 ignored=0
可以看到任务复制是变了.这意味着文件是写在控制器上的。看一看
shell> cat /tmp/localhost-sdb-part_info.json
{"changed": false, "disk": {"dev": "/dev/sdb", "size": 4089.0, "unit": "mb", "table": "msdos", "model": "Generic Flash Disk", "logical_block": 512, "physical_block": 512}, "partitions": [{"num": 1, "begin": 0.0, "end": 34.1, "size": 34.1, "fstype": "fat32", "name": "", "flags": ["esp"], "unit": "mb"}], "script": "unit 'MB' print", "failed": false}
这复制参数force设置为错误的.这意味着该文件不会被替换。仅当目标不存在时才会传输文件。这就是你想让游戏幂等的原因。创建新分区后,您不想用分区信息替换缓存。但是,如果您出于某种原因想要更新缓存集 cache_update=true 并运行剧本
shell> ansible-playbook pb.yml -e debug=true -e get_info=true -e cache_update=true
- 第二部分将创建新分区。先以
--check模式运行,查看调试输出
shell> ansible-playbook pb6.yml -e debug=true -e create_part=true -C
给出(删节)
TASK [debug] *********************************************************************************
ok: [localhost] =>
msg: |-
part_size: 10
last_number: 1
last_end: 34.1
unit: MB
next_number: 2
next_part_start: 34.1MB
next_part_end: 44.1MB
这是对的。我们要创建大小为 10MB 的分区号 2。让我们创造它
shell> ansible-playbook pb.yml -e debug=true -e create_part=true
PLAY [localhost] ********************************************************************************
TASK [debug] ************************************************************************************
skipping: [localhost]
TASK [get partition info] ***********************************************************************
skipping: [localhost]
TASK [debug] ************************************************************************************
skipping: [localhost]
TASK [debug] ************************************************************************************
skipping: [localhost]
TASK [copy] *************************************************************************************
skipping: [localhost]
TASK [set_fact] *********************************************************************************
ok: [localhost]
TASK [debug] ************************************************************************************
ok: [localhost] =>
part_info:
changed: false
disk:
dev: /dev/sdb
logical_block: 512
model: Generic Flash Disk
physical_block: 512
size: 4089.0
table: msdos
unit: mb
failed: false
partitions:
- begin: 0.0
end: 34.1
flags:
- esp
fstype: fat32
name: ''
num: 1
size: 34.1
unit: mb
script: unit 'MB' print
TASK [debug] ************************************************************************************
ok: [localhost] =>
msg: |-
part_size: 10
last_number: 1
last_end: 34.1
unit: MB
next_number: 2
next_part_start: 34.1MB
next_part_end: 44.1MB
TASK [create new partition] *********************************************************************
changed: [localhost]
TASK [debug] ************************************************************************************
ok: [localhost] =>
result:
changed: true
disk:
dev: /dev/sdb
logical_block: 512
model: Generic Flash Disk
physical_block: 512
size: 4089.0
table: msdos
unit: mb
failed: false
partitions:
- begin: 0.0
end: 34.1
flags:
- esp
fstype: fat32
name: ''
num: 1
size: 34.1
unit: mb
- begin: 34.1
end: 44.1
flags: []
fstype: ''
name: ''
num: 2
size: 10.0
unit: mb
script: unit MB mkpart primary 34.1MB 44.1MB
PLAY RECAP **************************************************************************************
localhost: ok=5 changed=1 unreachable=0 failed=0 skipped=5 rescued=0 ignored=0
您可以看到新分区已创建。该剧本是幂等的。您可以重复运行它以确保分区存在。一切正常时,不会报告任何更改。
shell> parted -l
Model: Generic Flash Disk (scsi)
Disk /dev/sdb: 4089MB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
1 512B 34.1MB 34.1MB primary fat32 esp
2 34.1MB 44.1MB 10.0MB primary
- 当您第一次在大量远程主机上运行第二部分时,将远程主机分成较小的组可能是个好主意。创建分区后,您可以将其全部运行一次以进行审计。
完整剧本的示例
---
# All rights reserved (c) 2022, Vladimir Botka <vbotka@gmail.com>
# Simplified BSD License, https://opensource.org/licenses/BSD-2-Clause
- hosts: localhost
gather_facts: false
become: true
vars:
disk: sda # change this
part_size: 3 # change this
unit: GiB # change this
cache_update: false
cache_path: "/tmp/{{ inventory_hostname }}-{{ disk }}-part_info.json"
# next variables are calculated from part_info
last_number: "{{ part_info.partitions|map(attribute='num')|max }}"
last_end: "{{ part_info.partitions|map(attribute='end')|max }}"
next_number: "{{ last_number|int + 1 }}"
next_part_start: "{{ last_end }}{{ unit }}"
next_part_end: "{{ last_end|float + part_size|float }}{{ unit }}"
tasks:
- name: Get info and write part_info to cache_path
block:
- debug:
msg: |-
disk: /dev/{{ disk }}
unit: {{ unit }}
cache_update: {{ cache_update }}
cache_path: {{ cache_path }}
when: debug|d(false)|bool
- name: get partition info
parted:
device: "/dev/{{ disk }}"
unit: "{{ unit }}"
register: part_info
- debug:
var: part_info
when: debug|d(false)|bool
- debug:
msg: |-
part_size: {{ part_size }}
last_number: {{ last_number }}
last_end: {{ last_end }}
next_number: {{ next_number }}
next_part_start: {{ next_part_start }}
next_part_end: {{ next_part_end }}
when: debug|d(false)|bool
- copy:
dest: "{{ cache_path }}"
force: "{{ cache_update|bool }}"
content: |
{{ part_info|to_json }}
delegate_to: localhost
when: get_info|d(false)|bool
- name: Read part_info from cache_path and create partition
block:
- set_fact:
part_info: "{{ lookup('file', cache_path)|from_yaml }}"
- debug:
var: part_info
when: debug|d(false)|bool
- debug:
msg: |-
part_size: {{ part_size }}
last_number: {{ last_number }}
last_end: {{ last_end }}
unit: {{ unit }}
next_number: {{ next_number }}
next_part_start: {{ next_part_start }}
next_part_end: {{ next_part_end }}
when: debug|d(false)|bool
- name: create new partition
parted:
device: "/dev/{{ disk }}"
number: "{{ next_number }}"
part_start: "{{ next_part_start }}"
part_end: "{{ next_part_end }}"
align: optimal
unit: "{{ unit }}"
state: present
register: result
- debug:
var: result
when: debug|d(false)|bool
when: create_part|d(false)|bool