0

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?

1
  • Can I take a look at your inventory file? Commented Sep 15, 2021 at 19:34

1 Answer 1

1

I believe it would work if you set your inventory as the following.

[your_group_name]
windowsDA ansible_host=188.88.88.88 etc.
Sign up to request clarification or add additional context in comments.

4 Comments

My hosts file used to be: [windowsDA] with only the IP and then [WindowsDA:vars] with the usual winrm stuff. I changed it to your suggestion with a random group name and now it works! Could you explain why this way would work, I really don't get it.
When I define it as the group and not the individual host then it stops working again. Could you explain?
I am not comfortable about group variables but after all you are trying to access the variables of a certain host instead of groups of hosts.
You're right. It probably has to do with something like that. Thanks for the help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.