如何调试Ansible和Vagrant中的"ERROR!Unexpected Exception:Non-hexadecimal digit"?

Vla*_*vic 2 virtualbox vagrant ansible vagrant-windows ansible-vault

我正试图用Ansible运行Vagrant时遇到问题.这是我在Windows 7上尝试运行vagrant upvagrant provision终端(git bash)时遇到的错误:

ERROR! Unexpected Exception: Non-hexadecimal digit found
to see the full traceback, use -vvv
Ansible failed to complete successfully. Any error output should be
visible above. Please fix these errors and try again.
Run Code Online (Sandbox Code Playgroud)

这是因为Ansible-vault文件.vault_pass不是十六进制(它是一个字符串,不能更改).在我的同事的电脑上,它正常工作,但在我的电脑上却没有.

.vault_pass文件中有问题的字符串看起来像DBAKWeG3KOr3jKjBDbAz.

我想问题是Python的问题,但我不确定,我不知道如何解决它.

当我尝试添加时,-vvv这是重新开始:

ERROR! Unexpected Exception: Non-hexadecimal digit found
the full traceback was:

Traceback (most recent call last):
  File "/usr/bin/ansible-playbook", line 92, in 
    exit_code = cli.run()
  File "/usr/lib/python2.7/dist-packages/ansible/cli/playbook.py", line 132, in run
    inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list=self.options.inventory)
  File "/usr/lib/python2.7/dist-packages/ansible/inventory/__init__.py", line 85, in __init__
    self.parse_inventory(host_list)
  File "/usr/lib/python2.7/dist-packages/ansible/inventory/__init__.py", line 144, in parse_inventory
    group.vars = combine_vars(group.vars, self.get_group_variables(group.name))
  File "/usr/lib/python2.7/dist-packages/ansible/inventory/__init__.py", line 509, in get_group_variables
    self._vars_per_group[groupname] = self._get_group_variables(groupname, vault_password=vault_password)
  File "/usr/lib/python2.7/dist-packages/ansible/inventory/__init__.py", line 527, in _get_group_variables
    vars = combine_vars(vars, self.get_group_vars(group))
  File "/usr/lib/python2.7/dist-packages/ansible/inventory/__init__.py", line 707, in get_group_vars
    return self._get_hostgroup_vars(host=None, group=group, new_pb_basedir=new_pb_basedir)
  File "/usr/lib/python2.7/dist-packages/ansible/inventory/__init__.py", line 746, in _get_hostgroup_vars
    results = combine_vars(results, self._variable_manager.add_group_vars_file(base_path, self._loader))
  File "/usr/lib/python2.7/dist-packages/ansible/vars/__init__.py", line 578, in add_group_vars_file
    (name, data) = self._load_inventory_file(path, loader)
  File "/usr/lib/python2.7/dist-packages/ansible/vars/__init__.py", line 535, in _load_inventory_file
    _found, results = self._load_inventory_file(path=p, loader=loader)
  File "/usr/lib/python2.7/dist-packages/ansible/vars/__init__.py", line 550, in _load_inventory_file
    data = loader.load_from_file(path)
  File "/usr/lib/python2.7/dist-packages/ansible/parsing/dataloader.py", line 113, in load_from_file
    (file_data, show_content) = self._get_file_contents(file_name)
  File "/usr/lib/python2.7/dist-packages/ansible/parsing/dataloader.py", line 172, in _get_file_contents
    data = self._vault.decrypt(data)
  File "/usr/lib/python2.7/dist-packages/ansible/parsing/vault/__init__.py", line 169, in decrypt
    b_data = this_cipher.decrypt(b_data, self.b_password)
  File "/usr/lib/python2.7/dist-packages/ansible/parsing/vault/__init__.py", line 674, in decrypt
    data = unhexlify(data)
TypeError: Non-hexadecimal digit found
Ansible failed to complete successfully. Any error output should be
visible above. Please fix these errors and try again.

Jav*_*lba 11

以防万一有人遇到同样的问题,我在尝试手动解密直接从 YAML 文件(或从 的输出ansible-vault encrypt_string)复制的变量时遇到了这个问题

例如:

加密字符串:

echo -n 'all that is gold does not glitter' | ansible-vault encrypt_string
Reading plaintext input from stdin. (ctrl-d to end input)
!vault |
          $ANSIBLE_VAULT;1.1;AES256
          61626566613637386434386364376236636636646263386561336463386132626335386335356463
          3930303065646433346431383463653663356332306564310a653464313035383335633065666462
          61303866343966613164623533323936383165623539623734316161373561383532326231623862
          6439306562306433360a633038626233376262373561333630356662386462343566346565636364
          64643961613064313964376266336330366566616435663130666135383739323962646563326336
          3062636437636664363039383436306535303939323535353163
Run Code Online (Sandbox Code Playgroud)

尝试解密:

echo '$ANSIBLE_VAULT;1.1;AES256
          61626566613637386434386364376236636636646263386561336463386132626335386335356463
          3930303065646433346431383463653663356332306564310a653464313035383335633065666462
          61303866343966613164623533323936383165623539623734316161373561383532326231623862
          6439306562306433360a633038626233376262373561333630356662386462343566346565636364
          64643961613064313964376266336330366566616435663130666135383739323962646563326336
          3062636437636664363039383436306535303939323535353163' | ansible-vault decrypt --vault-password-file ./vault-env
[WARNING]: There was a vault format error in -: Vault format unhexlify error:
Non-hexadecimal digit found
ERROR! Vault format unhexlify error: Non-hexadecimal digit found for -
Run Code Online (Sandbox Code Playgroud)

要修复它,只需删除开头的 YAML 缩进空格即可:

echo '$ANSIBLE_VAULT;1.1;AES256
61626566613637386434386364376236636636646263386561336463386132626335386335356463 
3930303065646433346431383463653663356332306564310a653464313035383335633065666462 
61303866343966613164623533323936383165623539623734316161373561383532326231623862 
6439306562306433360a633038626233376262373561333630356662386462343566346565636364 
64643961613064313964376266336330366566616435663130666135383739323962646563326336 
3062636437636664363039383436306535303939323535353163' | ansible-vault decrypt --vault-password-file ./vault-env 
Decryption successful
all that is gold does not glitter%
Run Code Online (Sandbox Code Playgroud)


nul*_*ull 5

编码文件包含回车符(\ r)符号时,可能会发生此问题.

如果你通过git获得文件,你应该正确配置git - 设置core.eol = lf.gitconfig


Nik*_*cic 5

它与加密文件末尾的Windows行结尾有关.只需将最后一行切换到Unix并保存文件即可.