From b86685bcfa6198d044ff7e01e84160d944de3d77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20=C4=8Cern=C3=BD?= Date: Thu, 14 Sep 2023 16:45:44 +0200 Subject: [PATCH] Fix Ansible Tasks order In https://github.com/ComplianceAsCode/content/pull/11033, we have switched to a new script for generating profile oriented Ansible Playbooks. Unfortunately, when Python 2 is used the generated Ansible Playbooks don't preserve the order of Ansible Tasks in the order defined in the SCAP source data stream. The wrong order of Ansible Tasks in a Playbook might cause an unexpected conflict between them during the run, for example https://github.com/ComplianceAsCode/content/issues/11104. The root cause of the problem is that dictionaries in Python 2 don't preserve order of elements but starting from Python 3.6 the dictionaries preserve order of its elements. Fixes: #11104 --- build-scripts/generate_profile_remediations.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/build-scripts/generate_profile_remediations.py b/build-scripts/generate_profile_remediations.py index 91bf2cb9fdd..9182479ca80 100755 --- a/build-scripts/generate_profile_remediations.py +++ b/build-scripts/generate_profile_remediations.py @@ -1,6 +1,7 @@ #!/usr/bin/python3 import argparse +import collections import os import re import xml.etree.ElementTree as ET @@ -192,7 +193,7 @@ def get_benchmark_data(self): self.variables = get_all_variables(benchmark) def load_all_remediations(self, benchmark): - self.remediations = {} + self.remediations = collections.OrderedDict() rule_xpath = ".//{%s}Rule" % (XCCDF12_NS) for rule_el in benchmark.findall(rule_xpath): rule_id = rule_el.get("id") @@ -238,7 +239,7 @@ def create_output_ansible(self, profile_el): def collect_ansible_vars_and_tasks(self, profile_el): selected_rules = get_selected_rules(profile_el) refinements = get_value_refinenements(profile_el) - all_vars = {} + all_vars = collections.OrderedDict() all_tasks = [] for rule_id, fix_el in self.remediations.items(): if rule_id not in selected_rules: