我正在尝试读取具有标签的 YAML 文件!vault
在里面。我收到错误:
无法确定标签“!vault”的构造函数
在阅读了几篇博客后,我了解到我需要指定一些构造函数来解决此问题,但我不清楚如何做到这一点。
import yaml
from yaml.loader import SafeLoader
with open('test.yml' ) as stream:
try:
inventory_info = yaml.safe_load(stream)
except yaml.YAMLError as exc:
print(exc)
User = inventory_info['all']['children']['linux']['vars']['user']
key = inventory_info['all']['children']['linux']['vars']['key_file']
我正在使用的 YAML 文件:
all:
children:
rhel:
hosts: 172.18.144.98
centos:
hosts: 172.18.144.98
linux:
children:
rhel:
centos:
vars:
user: "o9ansibleuser"
key_file: "test.pem"
ansible_password: !vault |
$ANSIBLE_VAULT;2.1;AES256
3234567899353936376166353
要么使用from_yaml
实用功能:
from ansible.parsing.utils.yaml import from_yaml
# inventory_info = yaml.safe_load(stream) # Change this
inventory_info = from_yaml(stream) # to this
或者将构造函数添加到yaml.SafeLoader
:
from ansible.parsing.yaml.objects import AnsibleVaultEncryptedUnicode
def construct_vault_encrypted_unicode(loader, node):
value = loader.construct_scalar(node)
return AnsibleVaultEncryptedUnicode(value)
yaml.SafeLoader.add_constructor(u'!vault', construct_vault_encrypted_unicode)
inventory_info = yaml.safe_load(stream)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)