The problem I'm having is fairly simple. I have a Windows Server host I'm connecting to, to make a new AD User based off some data. It's static for now, but will be dynamic in the future. It takes some variables plus randomization for uniqueness and puts them in some variables.
The next play needs to use those same variables for deploying a Windows 10 virtual machine. I used the "{{ hostvars['host']['variablename'] }}" to take them over to the next play. But no matter what I try, I always get the Undefined Variable error.
fatal: [localhost]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: \"hostvars['windowsDA']\" is undefined\n\nThe error appears to be in '/etc/ansibleplaybooks/Orchestration/onboarduser': line 67, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: giveinfo\n ^ here\n"}
I've tried gather_facts. I've tried putting the original vars in vars: ... than putting them under set_fact, I've tried multiple types of syntax. Yet it doesn't want to work. Here is a shortened version of my playbook. The rest Isn't needed to know. If the debug msg works, then it should work everywhere.
- name: Make AD User
hosts: windowsDA
tasks:
- name: Set variables as fact
set_fact:
var_random_string: "{{ lookup('community.general.random_string', length=3, special=false, base64=false) }}"
var_name: Pieter
var_lastname: Post
var_department: IT
var_underscore: _
- name: Create User
community.windows.win_domain_user:
enabled: yes
name: "{{var_name+var_lastname+var_underscore+var_random_string}}"
firstname: "{{ var_name }}"
surname: "{{ var_lastname }}"
company: Poliforma
password: P@ssw0rd
password_expired: yes
path: OU={{var_department}},DC=poliforma,DC=com
state: present
description: Werknemer van {{var_department}}Afdeling
upn: "{{var_name+var_lastname+var_underscore+var_random_string}}@poliforma.com"
user_cannot_change_password: no
attributes:
department: "{{var_department}}Afdeling"
- name: Clone the template and customize
hosts: localhost
vars:
test: "{{ hostvars['windowsDA']['var_name'] }}"
#hostvar_fact_var_name: #"{{hostvars['localhost']['fact_var_name']}}"
#hostvar_fact_var_lastname: #"{{hostvars['localhost']['fact_var_lastname']}}"
#hostvar_fact_var_underscore: #"{{hostvars['localhost']['fact_var_underscore']}}"
#hostvar_fact_var_random_string: #"{{hostvars['localhost']['fact_var_random_string']}}"
tasks:
- name: giveinfo
debug: msg="{{test}}"
Can someone help me?
I believe it would work if you set your inventory as the following.
[your_group_name]
windowsDA ansible_host=188.88.88.88 etc.