0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Ansible Coreを使ってRHEL10がインストールされたKVM仮想マシンを作成する

0
Last updated at Posted at 2025-12-27

目的

KVMの仮想マシンをAnsibleを使って作成からOSインストールまでを自動化します。
安定性を重視して、シンプルな構成となるAnsible Coreと必要なコレクションだけ追加していきます。

環境やデフォルトと変えている事

  • ホストのOSはREHL10
  • 仮想マシンの配置先は "/data"
  • ISOファイルの配置先は "/data/iso"

必要なもの

  • Ansible Core
  • community.libvirtコレクション
  • Red Hat Enterprise Linux 10.1 Binary DVD のISOファイル

インストール手順

Ansible Coreをインストールします。

Shell
sudo dnf install ansible-core

必要となるコレクションを追加します。
今回は全ユーザでAnsibleを使うことを前提としています。

Shell
sudo ansible-galaxy collection install community.libvirt -p /usr/share/ansible/collections

ディレクトリ構成とファイルの配置先

ディレクトリ構成
.
├── inventories
│   └── hosts
└── playbooks
    ├── kickstart
    │   └── ks.cfg
    └── rhel10-autoinstall.yaml

kickstartファイルを作成する

ますはOSを自動インストトールするためのkickstartファイルを作成します。
ファイル名は"ks.cfg"で作成します。

ks.cfg
#version=RHEL10
lang ja_JP.UTF-8
keyboard jp
timezone Asia/Tokyo --utc

rootpw --plaintext redhat
user --name=admin --password=redhat --groups=wheel

network --bootproto=dhcp
firewall --enabled
selinux --enforcing

bootloader --location=mbr
clearpart --all --initlabel
autopart

# 最小構成を指定
%packages
@^minimal-environment
%end

reboot

仮想マシンを構築するPlaybookを作成する

次に仮想マシンを作成するPlaybookを作成します。
今回作成するのはRHEL10の仮想マシンです。
このplaybookでは次のような工程が実行されます。

  1. 必要なツール(xorriso)をインストールする
  2. Kickstart ファイルから「自動設定用 ISO」を作成する
  3. 仮想マシン用のディスクファイルを作成する
  4. virt-install を使って RHEL10 を自動インストールする
rhel10-autoinstall.yaml
- name: Fully automated RHEL10 install using local Kickstart
  hosts: kvm_host
  become: yes

  vars:
    vm_name: rhel10-auto01
    vm_memory: 4096
    vm_vcpus: 2
    vm_disk_size: 20
    vm_pool: "/data"
    rhel_iso: "{{vm_pool}}/iso/rhel-10.1-x86_64-dvd.iso"
    ks_cfg: "ks.cfg"
    ks_iso: "{{vm_pool}}/{{ vm_name }}-ks.iso"
    vm_disk: "{{vm_pool}}/{{ vm_name }}.qcow2"

  tasks:

    - name: Ensure xorriso is installed
      package:
        name: xorriso
        state: present

    - name: Create Kickstart ISO
      command: >
        xorriso -as mkisofs -o {{ ks_iso }}
        -volid OEMDRV
        -J -R
        {{ ks_cfg }}
      args:
        chdir: "{{ playbook_dir }}/kickstart"

    - name: Create VM disk
      command: >
        qemu-img create -f qcow2 {{ vm_disk }} {{ vm_disk_size }}G

    - name: Install RHEL10 using virt-install with local Kickstart
      command: >
        virt-install
        --name {{ vm_name }}
        --memory {{ vm_memory }}
        --vcpus {{ vm_vcpus }}
        --disk path={{ vm_disk }},format=qcow2
        --disk path={{ ks_iso }},device=cdrom
        --location {{ rhel_iso }}
        --extra-args "inst.ks=hd:LABEL=OEMDRV:/ks.cfg console=ttyS0"
        --network network=default
        --graphics none
        --noautoconsole
        --wait -1

Playbookを実行する

Shell
ansible-playbook -i inventories/hosts playbooks/rhel10-autoinstall.yaml --ask-become-pass

成功するとこんな感じにログが出ます。

ログ
$ ansible-playbook -i inventories/hosts playbooks/rhel10-autoinstall.yaml --ask-become-pass
BECOME password: 

PLAY [Fully automated RHEL10 install using local Kickstart] **********************************************

TASK [Gathering Facts] ***********************************************************************************
ok: [localhost]

TASK [Ensure xorriso is installed] ***********************************************************************
ok: [localhost]

TASK [Create Kickstart ISO] ******************************************************************************
changed: [localhost]

TASK [Create VM disk] ************************************************************************************
changed: [localhost]

TASK [Install RHEL10 using virt-install with local Kickstart] ********************************************
changed: [localhost]

PLAY RECAP ***********************************************************************************************
localhost                  : ok=5    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

$

次回は仮想マシン名をユニークにしたり、kickstartをさらに工夫したりと高度化を目指していきたいと思います。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?