我想使用 Ansible 從模板建立 Linux 虛擬機,然後運行 Ansible 來配置該盒子。該模板只是一個基本安裝,沒有任何其他內容。建置 VM 後,下一個任務應該是配置新建立的 VM。
我的問題是關於樓主的。虛擬機尚未創建,那麼我應該在哪台主機上執行此腳本?如果我使用vcenter作為主機,它將無法配置虛擬機器。但是,如果我使用主機的 IP,Ansible 將找不到它,因為它尚未建立。
最好的方法是什麼?
- hosts: ???????
tasks:
- name: Clone a virtual machine from Linux template and customize
vmware_guest:
hostname: "{{ vcenter_host }}"
username: "{{ vcenter_user }}"
password: "{{ vcenter_pass }}"
validate_certs: no
datacenter: "{{ datacenter }}"
state: present
#folder: /DC1/vm
template: "{{ template }}"
name: "{{ vm_name }}"
cluster: "{{ cluster }}"
networks:
- name: VM Network
ip: 192.168.10.11
netmask: 255.255.255.0
wait_for_ip_address: True
customization:
domain: "{{ guest_domain }}"
dns_servers:
- 8.9.9.9
- 7.8.8.9
delegate_to: localhost
- name: configure VM
答案1
您可以在播放期間將新虛擬機器新增至清單中,並使用 wait_for 來確保虛擬機器已準備就緒:
---
- hosts: localhost
vars:
vm: ubuntutest
tasks:
- name: Clone a virtual machine from Linux template and customize
vmware_guest:
... etc
- add_host:
name: "{{ vm }}"
groups: examplegroup
somecustomvar: wowamazing
- wait_for:
host: "{{ vm }}"
port: 22
delay: 3
timeout: 320
state: started
- hosts: examplegroup
tasks:
- debug:
msg: "it works!!"
看https://docs.ansible.com/ansible/latest/modules/add_host_module.html了解詳情。