运行 ansible playbook 时出现错误 AnsibleUndefinedVariable: 'facter_ipaddress_eth1'

运行 ansible playbook 时出现错误 AnsibleUndefinedVariable: 'facter_ipaddress_eth1'

收到一个大错误堆栈跟踪,未找到 ansible 变量异常

ansible.errors.AnsibleUndefinedVariable:'facter_ipaddress_eth1'

当我运行 ansible play book 时。

在这里和其他地方设置的值。

https://github.com/vfarcic/docker-swarm-networking/blob/master/ansible/group_vars/all

TASK [docker : Debian files are present] ********************************************************************************************************************************************************task path: /vagrant/ansible/roles/docker/tasks/main.yml:76
<10.100.192.200> ESTABLISH SSH CONNECTION FOR USER: vagrant
<10.100.192.200> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o 'IdentityFile="/vagrant/.vagrant/machines/swarm-master/virtualbox/private_key"' -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o 'User="vagrant"' -o ConnectTimeout=10 -o ControlPath=/home/vagrant/.ansible/cp/ceb9f96da5 10.100.192.200 '/bin/sh -c '"'"'echo ~vagrant && sleep 0'"'"''
<10.100.192.200> (0, b'/home/vagrant\n', b'')
<10.100.192.200> ESTABLISH SSH CONNECTION FOR USER: vagrant
<10.100.192.200> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o 'IdentityFile="/vagrant/.vagrant/machines/swarm-master/virtualbox/private_key"' -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o 'User="vagrant"' -o ConnectTimeout=10 -o ControlPath=/home/vagrant/.ansible/cp/ceb9f96da5 10.100.192.200 '/bin/sh -c '"'"'( umask 77 && mkdir -p "` echo /home/vagrant/.ansible/tmp `"&& mkdir "` echo /home/vagrant/.ansible/tmp/ansible-tmp-1640389849.3916476-93636-106822855105481 `" && echo ansible-tmp-1640389849.3916476-93636-106822855105481="` echo /home/vagrant/.ansible/tmp/ansible-tmp-1640389849.3916476-93636-106822855105481 `" ) && sleep 0'"'"''
<10.100.192.200> (0, b'ansible-tmp-1640389849.3916476-93636-106822855105481=/home/vagrant/.ansible/tmp/ansible-tmp-1640389849.3916476-93636-106822855105481\n', b'')
<10.100.192.200> ESTABLISH SSH CONNECTION FOR USER: vagrant
<10.100.192.200> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o 'IdentityFile="/vagrant/.vagrant/machines/swarm-master/virtualbox/private_key"' -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o 'User="vagrant"' -o ConnectTimeout=10 -o ControlPath=/home/vagrant/.ansible/cp/ceb9f96da5 10.100.192.200 '/bin/sh -c '"'"'rm -f -r /home/vagrant/.ansible/tmp/ansible-tmp-1640389849.3916476-93636-106822855105481/ > /dev/null 2>&1 && sleep 0'"'"''
<10.100.192.200> (0, b'', b'')
The full traceback is:
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/ansible/template/__init__.py", line 1139, in do_template
    res = j2_concat(rf)
  File "<template>", line 11, in root
  File "/usr/lib/python3/dist-packages/jinja2/runtime.py", line 639, in _fail_with_undefined_error
    raise self._undefined_exception(hint)
jinja2.exceptions.UndefinedError: 'facter_ipaddress_eth1' is undefined

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/ansible/template/vars.py", line 98, in __getitem__
    value = self._templar.template(variable)
  File "/usr/lib/python3/dist-packages/ansible/template/__init__.py", line 869, in template
    result = self.do_template(
  File "/usr/lib/python3/dist-packages/ansible/template/__init__.py", line 1176, in do_template
    raise AnsibleUndefinedVariable(e)
ansible.errors.AnsibleUndefinedVariable: 'facter_ipaddress_eth1' is undefined

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/ansible/template/__init__.py", line 1139, in do_template
    res = j2_concat(rf)
  File "<template>", line 9, in root
  File "/usr/lib/python3/dist-packages/ansible/template/__init__.py", line 404, in resolve_or_missing
    val = super(AnsibleContext, self).resolve_or_missing(key)
  File "/usr/lib/python3/dist-packages/jinja2/runtime.py", line 217, in resolve_or_missing
    return resolve_or_missing(self, key)
  File "/usr/lib/python3/dist-packages/jinja2/runtime.py", line 129, in resolve_or_missing
    return context.parent[key]
  File "/usr/lib/python3/dist-packages/ansible/template/vars.py", line 100, in __getitem__
    raise AnsibleUndefinedVariable("%s: %s" % (to_native(variable), e.message))
ansible.errors.AnsibleUndefinedVariable: {{ facter_ipaddress_eth1 }}: 'facter_ipaddress_eth1' is undefined

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/ansible/plugins/action/template.py", line 150, in run
    resultant = templar.do_template(template_data, preserve_trailing_newlines=True, escape_backslashes=False)
  File "/usr/lib/python3/dist-packages/ansible/template/__init__.py", line 1176, in do_template
    raise AnsibleUndefinedVariable(e)
ansible.errors.AnsibleUndefinedVariable: {{ facter_ipaddress_eth1 }}: 'facter_ipaddress_eth1' is undefined
fatal: [10.100.192.200]: FAILED! => {
    "changed": false,
    "msg": "AnsibleUndefinedVariable: {{ facter_ipaddress_eth1 }}: 'facter_ipaddress_eth1' is undefined"
}

PLAY RECAP **************************************************************************************************************************************************************************************10.100.192.200             : ok=14   changed=6    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0

按照@alexD 的建议后,我添加了安装 facter 的任务,但仍然收到相同的错误:

- name: Install facter
  raw: apt install -y facter
  become: true
  tags: [docker]

- name: Check facter
  command: "facter ipaddress_eth1"
  register: result
  tags: [docker]

答案1

要获取facter_变量,你需要facter目标主机上安装的程序。请参见笔记对于setup模块。

相关内容