YAML:“ansible 模块不支持的参数”

YAML: “Unsupported parameters for ansible module"

尝试如下操作时出现以下错误:

"msg": "Unsupported parameters for (vmware_vm_vm_drs_rule) module: delegate_to Supported parameters include: affinity_rule, cluster_name, drs_rule_name, enabled, hostname, mandatory, password, port, proxy_host, proxy_port, state, username, validate_certs, vms".

我不希望我的模块在 final_list 为空或少于一个对象时执行。仅当 final_list 中的对象超过 2 个时,我才希望它执行。有人能帮我解决一下吗

    "ansible_facts": {"final_list": [ "NPSY7ADFS01"]}
    "ansible_facts": {"final_list": []}

    - name: Create DRS Anti Affinity Rule for VM-VM
       vmware_vm_vm_drs_rule:
         hostname: "{{ vcenter_hostname }}"
         username: "{{ vcenter_username }}"
         password: "{{ vcenter_password }}"
         cluster_name: "{{ datacenter_name }}-{{ cluster_name }}"
         validate_certs: no
         vms: "{{ final_list }}"
         drs_rule_name: "{{ rule_name }}"
         enabled: True
         mandatory: True
         affinity_rule: "{{ action_type }}" 
         delegate_to: localhost
       register: rule_creation
       when: final_list is defined

Q: "Unsupported parameters for (vmware_vm_vm_drs_rule) module: delegate_to ... "

答:错误信息很明确:"delegate_to is not a parameter of vmware_vm_vm_drs_rule"delegate_to 的缩进是错误的。这就是为什么 delegate_to 被认为是模块的参数的原因。修复 delegate_to

的缩进
     - name: Create DRS Anti Affinity Rule for VM-VM
       vmware_vm_vm_drs_rule:
         hostname: "{{ vcenter_hostname }}"
         username: "{{ vcenter_username }}"
         password: "{{ vcenter_password }}"
         cluster_name: "{{ datacenter_name }}-{{ cluster_name }}"
         validate_certs: no
         vms: "{{ final_list }}"
         drs_rule_name: "{{ rule_name }}"
         enabled: True
         mandatory: True
         affinity_rule: "{{ action_type }}" 
       delegate_to: localhost
       register: rule_creation
       when: final_list is defined

FWIW。使用 ansible-lint 验证语法。


Q: "I don't want the module to execute when the final_list is empty."

答:试试这个

       when: final_list|default([])|length > 0