fix: support user data ansible with output ansible

This commit is contained in:
egarette@silique.fr 2024-11-27 16:22:34 +01:00
parent e7157de7b5
commit 71e0fcd8cf
5 changed files with 55 additions and 16 deletions

View file

@ -5,8 +5,8 @@
msgid "" msgid ""
msgstr "" msgstr ""
"Project-Id-Version: \n" "Project-Id-Version: \n"
"POT-Creation-Date: 2024-11-25 10:49+0100\n" "POT-Creation-Date: 2024-11-27 16:19+0100\n"
"PO-Revision-Date: 2024-11-25 10:51+0100\n" "PO-Revision-Date: 2024-11-27 16:21+0100\n"
"Last-Translator: \n" "Last-Translator: \n"
"Language-Team: \n" "Language-Team: \n"
"Language: fr\n" "Language: fr\n"
@ -16,6 +16,6 @@ msgstr ""
"Generated-By: pygettext.py 1.5\n" "Generated-By: pygettext.py 1.5\n"
"X-Generator: Poedit 3.5\n" "X-Generator: Poedit 3.5\n"
#: src/rougail/user_data_ansible/__init__.py:43 #: src/rougail/user_data_ansible/__init__.py:46
msgid "ansible is not set in step.user_data" msgid "ansible is not set in step.user_data"
msgstr "ansible n'est pas définit dans step.user_data" msgstr "ansible n'est pas définit dans step.user_data"

View file

@ -5,7 +5,7 @@
msgid "" msgid ""
msgstr "" msgstr ""
"Project-Id-Version: PACKAGE VERSION\n" "Project-Id-Version: PACKAGE VERSION\n"
"POT-Creation-Date: 2024-11-25 10:53+0100\n" "POT-Creation-Date: 2024-11-27 16:21+0100\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n" "Language-Team: LANGUAGE <LL@li.org>\n"
@ -15,7 +15,7 @@ msgstr ""
"Generated-By: pygettext.py 1.5\n" "Generated-By: pygettext.py 1.5\n"
#: src/rougail/user_data_ansible/__init__.py:43 #: src/rougail/user_data_ansible/__init__.py:46
msgid "ansible is not set in step.user_data" msgid "ansible is not set in step.user_data"
msgstr "" msgstr ""

View file

@ -16,16 +16,19 @@ 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/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
""" """
from rougail import RougailConfig from pathlib import Path
from rougail.user_data_file import RougailUserDataFile
from ansible.parsing.vault import VaultLib, PromptVaultSecret from ansible.parsing.vault import VaultLib, PromptVaultSecret
from ansible.module_utils._text import to_bytes from ansible.module_utils._text import to_bytes
from pathlib import Path
from rougail import RougailConfig
from rougail.user_data_file import RougailUserDataFile
from .i18n import _ from .i18n import _
class RougailUserDataAnsible(RougailUserDataFile): class RougailUserDataAnsible(RougailUserDataFile):
"""Load Ansible data from encrypted file
"""
def __init__( def __init__(
self, self,
config, config,
@ -49,9 +52,13 @@ class RougailUserDataAnsible(RougailUserDataFile):
self.warnings = [] self.warnings = []
def open(self, filename: str) -> dict: def open(self, filename: str) -> dict:
prompt = PromptVaultSecret(to_bytes(self.secret), 'default', ["Vault password: "]) """Open file
vault = VaultLib([('default', prompt)]) """
with Path(filename).open('rb') as fh: 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())) return self.yaml.load(vault.decrypt(fh.read()))

View file

@ -19,17 +19,35 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
def get_rougail_config( def get_rougail_config(
*, *,
backward_compatibility=True, backward_compatibility: bool = True, # pylint: disable=unused-argument
) -> dict: ) -> dict:
options = """ """get rougail config for ansible"""
# redefine ansible family if already exists (for output)
# or create a new only only for user data
options = [
"""
ansible: ansible:
description: Configuration rougail-user-data-ansible exists: true
redefine: true
description: Configuration of user data or output Ansible
disabled:
type: jinja
jinja: |
{% if step.output != 'ansible' and 'ansible' not in step.user_data %}
disabled
{% endif %}
""",
"""
ansible:
exists: false
description: Configuration of user data Ansible
disabled: disabled:
type: jinja type: jinja
jinja: | jinja: |
{% if 'ansible' not in step.user_data %} {% if 'ansible' not in step.user_data %}
disabled disabled
{% endif %} {% endif %}
filename: filename:
description: Ansible filename inventory description: Ansible filename inventory
type: unix_filename type: unix_filename
@ -39,10 +57,24 @@ ansible:
test_existence: True test_existence: True
types: types:
- file - file
disabled:
type: jinja
jinja: |
{% if 'ansible' not in step.user_data %}
disabled
{% endif %}
secret: secret:
description: Secret to decrypt file description: Secret to decrypt file
type: secret type: secret
""" disabled:
type: jinja
jinja: |
{% if 'ansible' not in step.user_data %}
disabled
{% endif %}
""",
]
return { return {
"name": "ansible", "name": "ansible",
"process": "user data", "process": "user data",
@ -51,4 +83,4 @@ ansible:
} }
__all__ = "get_rougail_config" __all__ = ("get_rougail_config",)