# variable undefined?

**URL:** <https://forum.ansible.com/t/variable-undefined/32357>\
**Category:** Ansible Project\
**Created:** [February 29, 2020, 8:48am UTC](https://forum.ansible.com/t/variable-undefined/32357 "2020-02-29T08:48:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dave\_York](https://avatars.discourse-cdn.com/v4/letter/d/77aa72/32.png) [@Dave\_York](https://forum.ansible.com/u/Dave_York)\
**Post date:** [February 29, 2020, 8:48am UTC](https://forum.ansible.com/t/variable-undefined/32357/1 "2020-02-29T08:48:45Z")

</div>

I’m confused why I’m getting a ‘variable undefined’ error with this playbook, anyone have any idea?

- hosts: localhost  
connection: local  
gather\_facts: yes

tasks:

- name: Generate a temporary random password for template/os customization  
set\_fact:  
randopass: “{{ lookup(‘password’, ‘/dev/null length=24 chars=ascii\_letters’) }}”

… I use ‘{{ randopass }}’ somewhere else in this play and its fine… but then I get to this part:

- hosts: staging  
vars:  
ansible\_user: Administrator  
ansible\_password: ‘{{ randopass }}’  
gather\_facts: yes

ERROR:  
The field ‘password’ has an invalid value, which includes an undefined variable. The error was: ‘randopass’ is undefined

---

<div class="post-metadata">

**Author:** ![jean-yves](https://avatars.discourse-cdn.com/v4/letter/j/a587f6/32.png) [@jean-yves](https://forum.ansible.com/u/jean-yves)\
**Post date:** [February 29, 2020, 8:55am UTC](https://forum.ansible.com/t/variable-undefined/32357/2 "2020-02-29T08:55:19Z")

</div>

Hi,

Variable are for a host… You use “localhost” and “staging”

unless the group staging contains only localhost, yes you will have some undefined variable for the different hosts in your staging group

Regards,

JYL

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [February 29, 2020, 9:28am UTC](https://forum.ansible.com/t/variable-undefined/32357/3 "2020-02-29T09:28:10Z")

</div>

Instead of "hosts: localhost" run the play with "hosts: staging" and limit  
the fork to "run\_once: true". For example

- hosts: staging  
&nbsp;&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;- name: Generate a temporary random password for template/os customization  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;randopass: ...  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;run\_once: true

This way the variable "randopass" will be defined in the "hostvars" of all  
hosts in the group staging.

HTH,

&nbsp;&nbsp;-vlado
