【问题标题】:Ansible - Problem with using Hostvars to use variables across different hosts. Error: Undefined VariableAnsible - 使用 Hostvars 跨不同主机使用变量的问题。错误:未定义的变量
【发布时间】:2021-09-15 19:01:30
【问题描述】:

我遇到的问题相当简单。我有一个要连接的 Windows Server 主机,根据一些数据创建一个新的 AD 用户。它现在是静态的,但将来会是动态的。它需要一些变量加上随机化以获得唯一性并将它们放入一些变量中。

下一场比赛需要使用这些相同的变量来部署 Windows 10 虚拟机。我使用“{{ hostvars['host']['variablename'] }}”将它们带到下一场比赛中。但无论我尝试什么,我总是得到 Undefined Variable 错误

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"}

我试过 gather_facts。我尝试将原始 vars 放入 vars: ...而不是将它们放在 set_fact 下,我尝试了多种类型的语法。然而它不想工作。这是我的剧本的缩短版本。其余的不需要知道。如果调试消息有效,那么它应该在任何地方都有效。

- 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}}"

有人可以帮我吗?

【问题讨论】:

  • 我可以看看你的库存文件吗?

标签: ansible undefined-variable


【解决方案1】:

我相信如果您将库存设置为以下内容,它会起作用。

[your_group_name]
windowsDA ansible_host=188.88.88.88 etc.

【讨论】:

  • 我的 hosts 文件曾经是:[windowsDA] 只有 IP,然后是 [WindowsDA:vars] 和通常的 winrm 文件。我用随机组名将其更改为您的建议,现在它可以工作了!你能解释一下为什么这种方式会起作用吗,我真的不明白。
  • 当我将它定义为组而不是单个主机时,它会再次停止工作。你能解释一下吗?
  • 我对组变量感到不舒服,但毕竟您试图访问某个主机的变量而不是主机组。
  • 你是对的。它可能与类似的事情有关。感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 2019-08-27
  • 2017-04-20
  • 2020-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多