2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Ansible vmware_guest で VM(CentOS7) のクローンを作ったときのメモ

Last updated at Posted at 2019-07-07

注意事項

  • VMware vCenter が必要です。
    • vmware_guest Module は VMware vCenter に接続を行い処理するため。
  • インターネット接続環境が必要です。

1. クローン元になる CentOS7 を作る

1) CentOS をインストール

IPアドレスはStatic(固定)で設定しておく。
既にインストールしてあれば飛ばしてOK

2) (minimal導入時)必要なパッケージをインストール

yum -y install perl net-tools

3) VMware Tools のイメージをアタッチ

4) VMware Tools のインストール

# mount -r /dev/cdrom /mnt
# cp -pr /mnt /root
# umount /mnt
# cd /root/mnt
# tar zxvf VMwareTools-8.3.17-784891.tar.gz
# cd vmware-tools-distrib/
# ./vmware-install.pl -d

5) VMをシャットダウン

# shutdown -h now

VMware ESX 上の CentOS 7 に VMwareTools をインストール
https://bacchi.me/linux/install-vmwaretools/

2. Ansible 環境を作る

1) クローン元とは別に CentOS7 をインストール

vCenterと通信する必要があるので、vCenterと疎通できるようにすること

2) ansibleなどの必要なパッケージをインストール

yum install unzip wget epel-release -y
yum install ansible python gcc python-devel -y
curl -L https://bootstrap.pypa.io/get-pip.py | python
pip install pyvmomi

pyvmomi を使って Python スクリプトで vSphere を操作する
http://sig9.hatenablog.com/entry/2017/11/01/000000

3) Ansibleのコードを書く

ディレクトリ構造

vm_clone_example/
├── inventory
└── main.yml

main.yml

---
- name: VMware Clone VM
  hosts: localhost
  vars_prompt:
    - name: "vcenter_host"
      prompt: "Please enter vCenter Hostname or IP address"
      private: no
    - name: "vcenter_password"
      prompt: "\nConnect vCenter ... \nPlease enter vCenter User Password"
      private: yes
    - name: "vm_name"
      prompt: "Clone VM Name"
      private: no
    - name: "network_ip"
      prompt: "Clone VM IP Address"
      private: no
  gather_facts: no
  tasks:
    - name: vm clone
      vmware_guest:
        hostname: "{{ vcenter_host }}"
        username: "{{ vcenter_username }}"
        password: "{{ vcenter_password }}"
        validate_certs: no
        datacenter: "{{ datacenter }}"
        folder: "{{ folder }}"
        name: "{{ vm_name }}"
        datastore: "{{ datastore }}"
        template: "{{ vm_template }}"
        state: poweredon
        networks:
          - name: "{{ network_name }}"
            ip: "{{ network_ip }}"
            netmask: "{{ network_netmask }}"
            gateway: "{{ network_gateway }}"
        customization:
          hostname: "{{ vm_name }}"
          dns_servers: "{{ dns_servers }}"
          domain: "{{ domain }}"

    - name: wait vmware tools started
      vmware_guest_tools_wait:
        hostname: "{{ vcenter_host }}"
        username: "{{ vcenter_username }}"
        password: "{{ vcenter_password }}"
        validate_certs: no
        name: "{{ vm_name }}"

inventory

[localhost]
127.0.0.1 ansible_connection=local

[clone_host]
192.168.123.211 ansible_user=root ansible_password=root

[localhost:vars]
vcenter_host = **** #ipアドレスorホスト名
vcenter_username = administrator@vsphere.loca
datacenter = Datacenter
folder = /vm
datastore = vsandatastore
vm_template = CentOS7u6_template #クローン元のVM名
network_name = VM Network 
network_netmask = 255.255.255.0
network_gateway = 192.168.123.254
dns_servers = ["192.168.123.254"]
domain = ****

3. Ansibleを叩いてクローンを作る!

Ansible 環境で実行

# ls -al
total 8
drwxrwxr-x  2 root root   39 Jul  8 00:08 .
dr-xr-x---. 6 root root  229 Jul  7 23:38 ..
-rw-rw-r--  1 root root  515 Jul  7 22:19 inventory
-rw-rw-r--  1 root root 1431 Jul  7 23:37 main.yml
#
# ansible-playbook main.yml -i inventory
Please enter vCenter Hostname or IP address: ksato-vcenter.2dabo.net

Connect vCenter ...
Please enter vCenter User Password:
Clone VM Name: centos01
Clone VM IP Address: 192.168.123.214

PLAY [VMware Clone VM] *********************************************************************************************
TASK [vm clone] *********************************************************************************************
changed: [127.0.0.1]

TASK [wait vmware tools started] *********************************************************************************************
changed: [127.0.0.1]

PLAY RECAP *********************************************************************************************
127.0.0.1                  : ok=2    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Ansibleを使ってVMwareのVMクローンとゲストOSにパッケージをインストールした時のメモ
https://qiita.com/sky_jokerxx/items/d8a42f27be97fb05dea6

2
4
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
2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?