mirror of
https://github.com/freeipa/ansible-freeipa.git
synced 2026-03-26 21:33:05 +00:00
utils/galaxyfy.py: Handle module_defaults, match roles and modules
The section module_defaults was not handled by utils/galaxyfy.py, also there was no verification that only roles and modules provided by ansible-freeipa are matched for prepending the collection prefix.
This commit is contained in:
@@ -4,7 +4,7 @@
|
|||||||
# Authors:
|
# Authors:
|
||||||
# Thomas Woerner <twoerner@redhat.com>
|
# Thomas Woerner <twoerner@redhat.com>
|
||||||
#
|
#
|
||||||
# Copyright (C) 2019,2020 Red Hat
|
# Copyright (C) 2019-2023 Red Hat
|
||||||
# see file 'COPYING' for use and warranty information
|
# see file 'COPYING' for use and warranty information
|
||||||
#
|
#
|
||||||
# This program is free software; you can redistribute it and/or modify
|
# This program is free software; you can redistribute it and/or modify
|
||||||
@@ -21,49 +21,95 @@
|
|||||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
import re
|
import re
|
||||||
|
from facts import ROLES, ALL_MODULES
|
||||||
|
|
||||||
|
|
||||||
|
def get_indent(txt):
|
||||||
|
return len(txt) - len(txt.lstrip())
|
||||||
|
|
||||||
|
|
||||||
def galaxyfy_playbook(project_prefix, collection_prefix, lines):
|
def galaxyfy_playbook(project_prefix, collection_prefix, lines):
|
||||||
po1 = re.compile('(%s.*:)$' % project_prefix)
|
po_module = re.compile('(%s.*):$' % project_prefix)
|
||||||
po2 = re.compile('(.*:) (%s.*)$' % project_prefix)
|
po_module_arg = re.compile('(%s.*): (.*)$' % project_prefix)
|
||||||
|
po_module_unnamed = re.compile('- (%s.*):$' % project_prefix)
|
||||||
|
po_role = re.compile('(.*:) (%s.*)$' % project_prefix)
|
||||||
|
|
||||||
|
pattern_module = r'%s.\1:' % collection_prefix
|
||||||
|
pattern_module_arg = r'%s.\1: \2' % collection_prefix
|
||||||
|
pattern_module_unnamed = r'- %s.\1:' % collection_prefix
|
||||||
|
pattern_role = r'\1 %s.\2' % collection_prefix
|
||||||
|
|
||||||
out_lines = []
|
out_lines = []
|
||||||
|
|
||||||
pattern1 = r'%s.\1' % collection_prefix
|
|
||||||
pattern2 = r'\1 %s.\2' % collection_prefix
|
|
||||||
|
|
||||||
changed = False
|
changed = False
|
||||||
changeable = False
|
changeable = False
|
||||||
include_role = False
|
include_role = False
|
||||||
|
module_defaults = False
|
||||||
|
module_defaults_indent = -1
|
||||||
for line in lines:
|
for line in lines:
|
||||||
stripped = line.strip()
|
stripped = line.strip()
|
||||||
if stripped.startswith("- name:") or \
|
if stripped.startswith("- name:") or \
|
||||||
stripped.startswith("- block:"):
|
stripped.startswith("- block:"):
|
||||||
changeable = True
|
changeable = True
|
||||||
|
module_defaults = False
|
||||||
|
module_defaults_indent = -1
|
||||||
elif stripped in ["set_fact:", "ansible.builtin.set_fact:", "vars:"]:
|
elif stripped in ["set_fact:", "ansible.builtin.set_fact:", "vars:"]:
|
||||||
changeable = False
|
changeable = False
|
||||||
include_role = False
|
include_role = False
|
||||||
|
module_defaults = False
|
||||||
|
module_defaults_indent = -1
|
||||||
elif stripped == "roles:":
|
elif stripped == "roles:":
|
||||||
changeable = True
|
changeable = True
|
||||||
include_role = False
|
include_role = False
|
||||||
|
module_defaults = False
|
||||||
|
module_defaults_indent = -1
|
||||||
elif (stripped.startswith("include_role:") or
|
elif (stripped.startswith("include_role:") or
|
||||||
stripped.startswith("ansible.builtin.include_role:")):
|
stripped.startswith("ansible.builtin.include_role:")):
|
||||||
include_role = True
|
include_role = True
|
||||||
|
module_defaults = False
|
||||||
|
module_defaults_indent = -1
|
||||||
elif include_role and stripped.startswith("name:"):
|
elif include_role and stripped.startswith("name:"):
|
||||||
line = po2.sub(pattern2, line)
|
match = po_role.search(line)
|
||||||
changed = True
|
if match and match.group(2) in ROLES:
|
||||||
|
line = po_role.sub(pattern_role, line)
|
||||||
|
changed = True
|
||||||
|
elif stripped == "module_defaults:":
|
||||||
|
changeable = True
|
||||||
|
include_role = False
|
||||||
|
module_defaults = True
|
||||||
|
module_defaults_indent = -1
|
||||||
|
elif module_defaults:
|
||||||
|
_indent = get_indent(line)
|
||||||
|
if module_defaults_indent == -1:
|
||||||
|
module_defaults_indent = _indent
|
||||||
|
if _indent == module_defaults_indent:
|
||||||
|
# only module, no YAML anchor or alias
|
||||||
|
match = po_module.search(line)
|
||||||
|
if match and match.group(1) in ALL_MODULES:
|
||||||
|
line = po_module.sub(pattern_module, line)
|
||||||
|
changed = True
|
||||||
|
# module with YAML anchor or alias
|
||||||
|
match = po_module_arg.search(line)
|
||||||
|
if match and match.group(1) in ALL_MODULES:
|
||||||
|
line = po_module_arg.sub(pattern_module_arg, line)
|
||||||
|
changed = True
|
||||||
elif changeable and stripped.startswith("- role:"):
|
elif changeable and stripped.startswith("- role:"):
|
||||||
line = po2.sub(pattern2, line)
|
match = po_role.search(line)
|
||||||
changed = True
|
if match and match.group(2) in ROLES:
|
||||||
|
line = po_role.sub(pattern_role, line)
|
||||||
|
changed = True
|
||||||
elif (changeable and stripped.startswith(project_prefix)
|
elif (changeable and stripped.startswith(project_prefix)
|
||||||
and not stripped.startswith(collection_prefix) # noqa
|
|
||||||
and stripped.endswith(":")): # noqa
|
and stripped.endswith(":")): # noqa
|
||||||
line = po1.sub(pattern1, line)
|
match = po_module.search(line)
|
||||||
changed = True
|
if match and match.group(1) in ALL_MODULES:
|
||||||
changeable = False # Only change first line in task
|
line = po_module.sub(pattern_module, line)
|
||||||
|
changed = True
|
||||||
|
changeable = False # Only change first line in task
|
||||||
elif (stripped.startswith("- %s" % project_prefix)
|
elif (stripped.startswith("- %s" % project_prefix)
|
||||||
and stripped.endswith(":")): # noqa
|
and stripped.endswith(":")): # noqa
|
||||||
line = po1.sub(pattern1, line)
|
match = po_module_unnamed.search(line)
|
||||||
changed = True
|
if match and match.group(1) in ALL_MODULES:
|
||||||
|
line = po_module_unnamed.sub(pattern_module_unnamed, line)
|
||||||
|
changed = True
|
||||||
|
|
||||||
out_lines.append(line)
|
out_lines.append(line)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user