在ansible中设置变量并将它们用作参数

时间:2016-09-02 11:23:31

标签: yaml ansible ansible-playbook

我想设置变量并稍后将它们用作参数。这是我的代码

---
- hosts: localhost
  connection: local

  vars:
      vcenter_hostname: hellothere.hey.com

      user: root

      pass: ohheytherehowareyou


  vars_prompt:

    - name: 'vm_task_action'
      prompt: 'Enter vm action to execute (none, delete)'
      default: 'none'
      private: no


    - name: 'guest_vm'
      prompt: "Enter the guest vm you want to delete"
      default: 'none'
      private: no
      when: vm_task_action == "delete"

  tasks:
    - name: deleting VMs
      vsphere_guest:
        vcenter_hostname: vcenter_hostname
        username: user
        password: pass 
        guest: "{{ guest_vm }}"
        state: absent
        force: yes
        when: vm_task_action == 'delete'

此代码有什么问题,我该如何解决?任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

错误本身就说明了:

  

'pass'不是有效的变量名

pass是一个特殊关键字,因此不能用作变量 例如,使用mypass: ohheytherehowareyou

P.S。另请注意,when:部分无法使用vars_prompt