为什么我得到'delegate_to'不是Ansible中播放错误的有效属性

时间:2019-06-16 02:42:18

标签: ansible

我从ansible文档站点here中提取了代码。

- name: Create a virtual machine on given ESXi hostname
  vmware_guest:
    hostname: "{{ vcenter_hostname }}"
    username: "{{ vcenter_username }}"
    password: "{{ vcenter_password }}"
    validate_certs: no
    folder: /DC1/vm/
    name: test_vm_0001
    state: poweredon
    guest_id: centos64Guest
    # This is hostname of particular ESXi server on which user wants VM to be deployed
    esxi_hostname: "{{ esxi_hostname }}"
    disk:
    - size_gb: 10
      type: thin
      datastore: datastore1
    hardware:
      memory_mb: 512
      num_cpus: 4
      scsi: paravirtual
    networks:
    - name: VM Network
      mac: aa:bb:dd:aa:00:14
      ip: 10.10.10.100
      netmask: 255.255.255.0
      device_type: vmxnet3
    wait_for_ip_address: yes
  delegate_to: localhost
  register: deploy_vm

当我将文本保存到文件并尝试运行它时,出现此错误:

enter image description here

2 个答案:

答案 0 :(得分:0)

我认为问题在于您将代码粘贴为剧本,但这是一项任务。

尝试将文件更改为以下内容。请仔细检查间距,因为yml是空格敏感的,而且我还没有亲自尝试过此脚本。您还需要提供一些要替换的变量,例如vcenter_hostname

---
- hosts: all
  tasks:
    - name: Create a virtual machine on given ESXi hostname
      vmware_guest:
        hostname: "{{ vcenter_hostname }}"
        username: "{{ vcenter_username }}"
        password: "{{ vcenter_password }}"
        validate_certs: no
        folder: /DC1/vm/
        name: test_vm_0001
        state: poweredon
        guest_id: centos64Guest
        # This is hostname of particular ESXi server on which user wants VM to be deployed
        esxi_hostname: "{{ esxi_hostname }}"
        disk:
        - size_gb: 10
        type: thin
        datastore: datastore1
        hardware:
        memory_mb: 512
        num_cpus: 4
        scsi: paravirtual
        networks:
        - name: VM Network
        mac: aa:bb:dd:aa:00:14
        ip: 10.10.10.100
        netmask: 255.255.255.0
        device_type: vmxnet3
        wait_for_ip_address: yes
      delegate_to: localhost
      register: deploy_vm

来自Ansible Docs

的代码

我不确定您是否需要为此创建inventory file

答案 1 :(得分:0)

在文件playbook / vm.yml中:

- hosts: all
  roles:
    - { role: vmdeletion, tags: ['vmdeletion'], when: remove_vm }
    - { role: vmcreation, tags: ['vmcreation'] }
    - { role: vmpower,    tags: ['vmpower'], power: "poweredon" }

在文件角色/vmcreation/tasks/main.yml中:

    - name: Create a virtual machine on given ESXi hostname
      vmware_guest:
        hostname: "{{ vcenter_hostname }}"
        username: "{{ vcenter_username }}"
        password: "{{ vcenter_password }}"
        validate_certs: no
        folder: /DC1/vm/
        name: test_vm_0001
        state: present
        guest_id: centos64Guest
        # This is hostname of particular ESXi server on which user wants VM to be deployed
        esxi_hostname: "{{ esxi_hostname }}"
        disk:
        - size_gb: 10
          type: thin
          datastore: datastore1
        hardware:
          memory_mb: 512
          num_cpus: 4
          scsi: paravirtual
        networks:
        - name: VM Network
          mac: aa:bb:dd:aa:00:14
          ip: 10.10.10.100
          netmask: 255.255.255.0
          device_type: vmxnet3
        wait_for_ip_address: yes
      delegate_to: localhost
      register: deploy_vm

然后运行:

ansible-playbook playbook/vm.yml -l ${my_groups_or_hosts_limit}

但是尝试将所有变量放入与主机/组关联的清单中,以使您的角色看起来像:

在文件角色/vmcreation/tasks/main.yml中:

- name: Create a virtual machine on given ESXi hostname
  vmware_guest:
    hostname: "{{ vcenter_hostname }}"
    username: "{{ vcenter_username }}"
    password: "{{ vcenter_password }}"
    validate_certs: no
    folder: "{{ vm_folder }}"
    name: "{{ inventory_hostname }}"
    state: present
    guest_id: centos64Guest
    # This is hostname of particular ESXi server on which user wants VM to be deployed
    esxi_hostname: "{{ vm_esxi_hostname }}"
    disk: "{{ vm_disks }}"
    hardware: "{{ vm_hardware }}"
    networks: "{{ vm_networks }}"
    wait_for_ip_address: yes
  delegate_to: localhost
  register: deploy_vm