首页 > 解决方案 > 在库存文件中设置变量时,ansible-playbook 命令引发未定义变量错误

问题描述

我正面临一个神秘的ansible行为。

我已经在我的清单文件中设置了组、主机和组变量,但是该ansible-playbook命令正在Undefined Variable针对我的所有组变量进行提升,同时使用模块打印它们可以ansible debug很好地显示变量。

这是我的 inventory.ini 文件:

 [windows]
ad_server ansible_host=mrspw00550.mydomain.com
[windows:vars]
ansible_connection=winrm
ansible_winrm_transport=ntlm
ansible_port=5985
ansible_become_method=runas
dns_zone=mydomain.com
ptr_zone=10.in-addr.arpa
dns_server=10.0.100.100

[linux]
web_server ansible_host=10.0.101.129
[linux:vars]
ansible_connection=ssh
ansible_become=yes
ansible_become_method=sudo
dns_zone=mydomain.com
ptr_zone=10.in-addr.arpa
dns_server=10.0.100.100
linux_home_dir=/home/ldaphome

剧本文件(set-hostname-and-dns.yml):

- name: Debug vars
  debug: var=hostvars[inventory_hostname]['dns_zone']
  delegate_to: ad_server

- name: Set  hostname and make DNS registrations
  block:
    - name: Set  hostname
      hostname:
        name: "web.{{ dns_zone }}"
      delegate_to: web_server
    - name: Create a DNS registration of type A 
      win_dns_record:
        name: "web"
        type: "A"
        value: "{{ inventory_hostname }}"
        zone: "{{ dns_zone }}"
        computer_name: "{{ dns_server }}"
      delegate_to: "{{ ad_server }}"
    - name: Create a DNS registration of type PTR 
      win_dns_record:
        name: "{{ inventory_hostname }}"
        type: "PTR"
        value: "web"
        zone: "{{ ptr_zone }}"
        computer_name: "{{ dns_server }}"
      delegate_to: "{{ ad_server }}"

运行ansible-playbook命令给出:

$ ansible-playbook playbook.yml  -i inventory-files/inventory.ini
PLAY [localhost] *********************************************************************************************************************************************************************************************
TASK [Gathering Facts] ***************************************************************************************************************************************************************************************
ok: [localhost]
TASK [create-linux-vm : Debug vars] **************************************************************************************************************************************************************************
ok: [localhost -> mrspw00550.mydomain.com] => {
    "hostvars[inventory_hostname]['dns_zone']": "VARIABLE IS NOT DEFINED!"
}
TASK [create-linux-vm : Set web_server hostname] **************************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'dns_zone' is undefined\n\nThe error appears to be in 'set-hostname-and-dns.yml': line 14, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n  block:\n    - name: Set {{ vm_name }} hostname\n      ^ here\nWe could be wrong, but this one looks like it might be an issue with\nmissing quotes. Always quote template expression brackets when they\nstart a value. For instance:\n\n    with_items:\n      - {{ foo }}\n\nShould be written as:\n\n    with_items:\n      - \"{{ foo }}\"\n"}
PLAY RECAP ***************************************************************************************************************************************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0

运行debug模块:

$ ansible -m debug -i inventory-files/inventory.ini -a "var=hostvars[inventory_hostname]['dns_zone']" all
ad_server | SUCCESS => {
    "hostvars[inventory_hostname]['dns_zone']": "mydomain.com"
}
web_server | SUCCESS => {
    "hostvars[inventory_hostname]['dns_zone']": "mydomain.com"
}
$

如您所见,ansible-playbook命令无法检索我的主机变量,而ansible debug模块可以。

即使我在inventory-files/group_vars/linux.ymlor中定义了这些变量,结果也是一样的inventory-files/group_vars/windows.yml,该变量仍然是未定义的ansible。如果尝试访问库存运行中的特殊变量以外的任何其他变量,问题是相同的ansible-playbook

那里可能有什么问题?

标签: ansibleansible-2.x

解决方案


我尝试delegate_facts设置为true,它没有用。

然后local在清单中定义一个组,其中包含localhost并定义我的本地组变量,如下所示:

[local]
localhost

[local:vars]
ansible_connection=local
dns_zone=mydomain.com
ptr_zone=10.in-addr.arpa
dns_server=10.0.100.100
linux_home_dir=/home/ldaphome

并从远程主机事实中删除这些 var,因为我真的没有那些变量。我的所有任务都在本地主机上执行,并根据操作委托给远程主机。

我还想明确指出,{{ hostvars['ad_server'].ptr_zone }}在他的评论中也特别要求像上面提到的@Zeitounator () 这样的 var/fact。


推荐阅读