rougail-user-data-ansible/src/rougail/user_data_ansible/__init__.py

62 lines
2.1 KiB
Python
Raw Normal View History

2024-11-25 11:02:56 +01:00
"""
Silique (https://www.silique.fr)
Copyright (C) 2024
This program is free software: you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by the
Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
from rougail import RougailConfig
from rougail.user_data_file import RougailUserDataFile
from ansible.parsing.vault import VaultLib, PromptVaultSecret
from ansible.module_utils._text import to_bytes
from pathlib import Path
from .i18n import _
class RougailUserDataAnsible(RougailUserDataFile):
def __init__(
self,
config,
*,
rougailconfig=None,
) -> None:
if rougailconfig is None:
rougailconfig = RougailConfig
user_data = rougailconfig["step.user_data"]
if "ansible" not in user_data:
user_data.append("ansible")
rougailconfig["step.user_data"] = user_data
user_data = rougailconfig["step.user_data"]
if "ansible" not in user_data:
raise Exception(_("ansible is not set in step.user_data"))
self.rougailconfig = rougailconfig
self.filenames = self.rougailconfig["ansible.filename"]
self.secret = self.rougailconfig["ansible.secret"]
self.config = config
self.errors = []
self.warnings = []
def open(self, filename: str) -> dict:
prompt = PromptVaultSecret(to_bytes(self.secret), 'default', ["Vault password: "])
vault = VaultLib([('default', prompt)])
with Path(filename).open('rb') as fh:
return self.yaml.load(vault.decrypt(fh.read()))
RougailUserData = RougailUserDataAnsible
__all__ = ("RougailUserDataAnsible",)