Ansible - Problem with using Hostvars to use variables across different hosts. Error: Undefined Variable
Ansible - Problem with using Hostvars to use variables across different hosts. Error: Undefined Variable
我遇到的问题很简单。我有一个 Windows Server 我正在连接的主机,以根据一些数据创建一个新的 AD 用户。目前它是静态的,但将来会是动态的。它需要一些变量加上随机化以获得唯一性并将它们放入一些变量中。
下一场比赛需要使用这些相同的变量来部署 Windows 10 台虚拟机。我使用“{{ hostvars[[=28=]]['variablename'] }}”将它们带到下一个游戏。但无论我尝试什么,我总是得到 未定义的变量错误。
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: ... 而不是将它们放在 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}}"
有人可以帮助我吗?
我相信如果你将你的库存设置如下,我相信它会起作用。
[your_group_name]
windowsDA ansible_host=188.88.88.88 etc.
我遇到的问题很简单。我有一个 Windows Server 我正在连接的主机,以根据一些数据创建一个新的 AD 用户。目前它是静态的,但将来会是动态的。它需要一些变量加上随机化以获得唯一性并将它们放入一些变量中。
下一场比赛需要使用这些相同的变量来部署 Windows 10 台虚拟机。我使用“{{ hostvars[[=28=]]['variablename'] }}”将它们带到下一个游戏。但无论我尝试什么,我总是得到 未定义的变量错误。
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: ... 而不是将它们放在 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}}"
有人可以帮助我吗?
我相信如果你将你的库存设置如下,我相信它会起作用。
[your_group_name]
windowsDA ansible_host=188.88.88.88 etc.