我正在尝试调整 /dev/sda2 分区的大小
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 50G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 22.5G 0 part
├─rhel-root 253:0 0 20.1G 0 lvm /
└─rhel-swap 253:1 0 2.4G 0 lvm [SWAP]
sr0 11:0 1 1024M 0 rom
所以它将占用 /dev/sda 磁盘的其余部分。手动扩展它时,一切都按预期工作,我尝试使用 ansible 自动化它,因为我将有超过 40 台完全相同的服务器,需要调整此分区的大小。
我尝试过使用 PARTED 模块,但没有找到调整正在使用的分区大小的方法。所以我想我可以使用 expect 模块来操作“fdisk /dev/sda”命令的输入。这是我的剧本:
- name: Resize current partition on /dev/sda
hosts: all
become: yes
tasks:
- name: Install pexpect
pip:
name: pexpect
# Delete and create new partition on /dev/sda
- name: create new partition
expect:
command: /sbin/fdisk /dev/sda
responses:
'Command.*$':
- 'd'
- 'n'
- 't'
- 'w'
'Partition number.*$': '\n' ## \n selects fdisk default choice ##
'Select.*$': '\n'
'First sector.*$': '\n'
'Last sector.*$': '\n'
'Do you want.*$': 'n' ## selects 'no' on removing LVM signature ##
'Hex code.*$': '08e' ## selects LVM partition type ##
echo: yes
timeout: 10
when: (ansible_os_family == "RedHat" and ansible_distribution_version|float >= 8)
我已经在我的 RHEL8 VM 上启动了它,使用本地主机作为清单,在收集事实并安装 pexpect playbook 冻结后没有输出。我尝试使用 -vvv 启动 ansible-playbook,但在确认权限升级后我没有收到任何输出,它冻结了:
<localhost> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o 'ControlPath="/home/ansible/.ansible/cp/8a5a4c6a60"' -tt localhost '/bin/sh -c '"'"'sudo -H -S -n -u root /bin/sh -c '"'"'"'"'"'"'"'"'echo BECOME-SUCCESS-btnzvwwmzfnhezvghgjycpvwrorpkkpg ; /usr/libexec/platform-python /home/ansible/.ansible/tmp/ansible-tmp-1679001756.3663957-6375-69716906410504/AnsiballZ_expect.py'"'"'"'"'"'"'"'"' && sleep 0'"'"''
Escalation succeeded
任何帮助将不胜感激
我设法创建了调整 /dev/sda 上最后一个分区大小的剧本,它满足了我的用例
- name: Resize last partition on /dev/sda
hosts: all
become: yes
vars:
disk_name: '/dev/sda'
tasks:
- name: Read device information (always use unit when probing)
parted: device={{ disk_name }} unit=MiB
register: disk_info
# - name: debug disk
# debug:
# msg: "Last partition is: {{sdb_info.partitions | length}}"
- name: Resize last partition
parted:
device: "{{ disk_name }}"
number: "{{disk_info.partitions | length}}"
part_end: "100%"
resize: true
state: present