unix Ansible:部署虚拟机的行动手册(Linux)

bmp9r5qi  于 2022-11-04  发布在  Unix
关注(0)|答案(1)|浏览(186)

我创建此行动手册是为了从模板部署新的虚拟机。现在,如果我尝试部署新的Linux虚拟机,它可以正常工作,但主机名无法自定义。它会保留模板中的相同主机名,我不知道为什么?
下面是我的剧本(这只是一个测试):

- name: Clone multiple VMs
  hosts: localhost 
  gather_facts: false
  vars_files: 
    multiple_vms.yml
  tasks: 
  - name: Clone multiple Ubuntu VMS from Template
    local_action:
      module: vmware_guest
      hostname: "{{ vcenter_hostname }}"
      username: "{{ vcenter_username }}"
      password: "{{ vcenter_password }}"
      validate_certs: no      
      folder: "{{ folder }}"
      template: "{{ vmtemplate }}"
      name: "{{ item }}"
      cluster: "{{ vcenter_cluster }}"
      datacenter: "{{ vcenter_datacenter }}"
      state: poweredon
      networks:
      - name: "{{ network }}"
        type: "{{ network_dhcp_or_static }}"
      hardware:
        memory_mb: {{ number_memory }}
        num_cpus: {{ number_cpu }}
      customization:
        hostname: "{{ servers }}"
        domain : "mydomaine.test"
      #customization_spec: "{{ customization_spec }}"
    with_items: "{{ servers }}"

其中vars multiple_vms.yml:

servers:
  - ubuntu_test01
  - ubuntu_test02
....

所以我需要我的新VMS从服务器列表(ubuntu_test01.mydomaine.test ..等)中获取主机名。
谢谢你的帮助
使用vmware_guest和自定义选项

lpwwtiir

lpwwtiir1#

首先,尝试hostname: "{{ item }}"
第二,不要做循环的事情,做一个适当的清单,然后:

- name: Clone multiple VMs
  hosts: all
  gather_facts: false
  vars_files: 
    multiple_vms.yml
  tasks: 
  - name: Clone multiple Ubuntu VMS from Template
    vmware_guest:
      hostname: "{{ vcenter_hostname }}"
      username: "{{ vcenter_username }}"
      password: "{{ vcenter_password }}"
      validate_certs: no      
      folder: "{{ folder }}"
      template: "{{ vmtemplate }}"
      name: "{{ ansible_hostname }}"
      cluster: "{{ vcenter_cluster }}"
      datacenter: "{{ vcenter_datacenter }}"
      state: poweredon
      networks:
      - name: "{{ network }}"
        type: "{{ network_dhcp_or_static }}"
      hardware:
        memory_mb: {{ number_memory }}
        num_cpus: {{ number_cpu }}
      customization:
        hostname: "{{ ansible_hostname }}"
        domain : "mydomaine.test"
    delegate_to: localhost

注意:local_action已过时。请使用delegate_to: localhost

相关问题