PyYAML error: Could not determine a constructor for the tag !vault

2024/11/18 10:58:37

I am trying to read a YAML file that has the tag !vault in it. I get the error:

could not determine a constructor for the tag '!vault'

Upon reading a couple of blogs, I understood that I need to specify some constructors to resolve this issue, but I'm not clear on how to do it.

import yaml
from yaml.loader import SafeLoaderwith 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']

The YAML file I am using:

all:children:rhel:hosts: 172.18.144.98centos:hosts: 172.18.144.98  linux:children:rhel:centos:vars:user: "o9ansibleuser"key_file: "test.pem"ansible_password: !vault |$ANSIBLE_VAULT;2.1;AES2563234567899353936376166353
Answer

Either use the from_yaml utility function:

from ansible.parsing.utils.yaml import from_yaml# inventory_info = yaml.safe_load(stream)  # Change this
inventory_info = from_yaml(stream)         # to this

Or add the constructor to yaml.SafeLoader:

from ansible.parsing.yaml.objects import AnsibleVaultEncryptedUnicodedef 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)
https://en.xdnf.cn/q/71071.html

Related Q&A

Accessing a XAMPP mysql via Python

Im attempting to use mysql after only having worked with sqlite in the past.Ive installed XAMPP on Linux (ubuntu) and have mysql up and running fine (seems like that with phpMyadmin at least). However,…

How to use deep learning models for time-series forecasting?

I have signals recorded from machines (m1, m2, so on) for 28 days. (Note: each signal in each day is 360 length long).machine_num, day1, day2, ..., day28 m1, [12, 10, 5, 6, ...], [78, 85, 32, 12, ...],…

Python - Create Shortcut with arguments

Using win32com.client, Im attempting to create a simple shortcut in a folder. The shortcut however I would like to have arguments, except I keep getting the following error.Traceback (most recent call …

How to replace a range of values with NaN in Pandas data-frame?

I have a huge data-frame. How should I replace a range of values (-200, -100) with NaN?

django 1.10 Exception while resolving variable is_popup in template admin/login.html

I create a new django project with python3.5 and django1.10.0,I keep getting an error in the admin whenever I want access localhost:8000/admin, He`res the error:[DEBUG]- Exception while resolving varia…

Programmatically extract data from an Excel spreadsheet

Is there a simple way, using some common Unix scripting language (Perl/Python/Ruby) or command line utility, to convert an Excel Spreadsheet file to CSV? Specifically, this one:http://www.econ.yale.e…

Is it possible to specify the driver dll directly in the ODBC connection string?

Im trying to use pyodbc to connect to a SQL Server (MS SQL Server through FreeTDS) in a portable application; since its supposed to be standalone, I would like to avoid having to explicitly install the…

Escape New line character in Spark CSV read

Im working on Spark 2.2.1 version and using the below python code, I can able to escape special characters like @ : I want to escape the special characters like newline(\n) and carriage return(\r). I r…

How can a class that inherits from a NumPy array change its own values?

I have a simple class that inherits from the NumPy n-dimensional array. I want to have two methods of the class that can change the array values of an instance of the class. One of the methods should s…

Python/SQL Alchemy Migrate - ValueError: too many values to unpack when migrating changes in db

I have several models in SQLAlchemy written and I just started getting an exception when running my migrate scripts: ValueError: too many values to unpackHere are my models:from app import dbROLE_USER …