variable undefined?

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

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

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

- hosts: staging
   tasks:
   - name: Generate a temporary random password for template/os customization
     set_fact:
       randopass: ...
     run_once: true

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

HTH,

  -vlado