user3796292 user3796292 - 3 months ago 16
YAML Question

Setting vars in ansible and using them as arguments

I want to set variables and use them as parameters later on. This is the code I have

---
- 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'


What is wrong with this code and how do I fix it. Any help would be greatly appreciated.

Answer

The Error says for itself:

'pass' is not a valid variable name

pass is a special keyword, so it can't be used as variable.
Use mypass: ohheytherehowareyou, for example.

P.S. also note that when: can't be used in vars_prompt section.