From 16dec2e594b1afa5dce524f2d4a0ed196401c8dc Mon Sep 17 00:00:00 2001 From: Kate Case Date: Thu, 18 Apr 2024 10:41:44 -0400 Subject: [PATCH 1/2] This feels too easy to be a proper solution. `_get_path_to_task_in_nested_tasks_block()` seems to do the wrong thing under certain circumstances when lineno > next_task_ling_index. It seems like we should never want that anyway, so just skip the whole thing until there is not a later play that is earlier than lineno. --- src/ansiblelint/yaml_utils.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/ansiblelint/yaml_utils.py b/src/ansiblelint/yaml_utils.py index 913ce28134..aa001b89ea 100644 --- a/src/ansiblelint/yaml_utils.py +++ b/src/ansiblelint/yaml_utils.py @@ -305,6 +305,10 @@ def _get_path_to_task_in_playbook( else: next_play_line_index = None + # We clearly haven't found the right spot yet if a following play starts on an earlier line. + if next_play_line_index and lineno > next_play_line_index: + continue + play_keys = list(play.keys()) for tasks_keyword in PLAYBOOK_TASK_KEYWORDS: if not play.get(tasks_keyword): From a5e89d4a8dd6b535e3e192ae948146124a4fa292 Mon Sep 17 00:00:00 2001 From: Kate Case Date: Mon, 22 Apr 2024 10:29:53 -0400 Subject: [PATCH 2/2] Add key-order test for multiple plays --- .github/workflows/tox.yml | 2 +- .../transform-key-order-block.transformed.yml | 20 +++++++++++++++++++ .../playbooks/transform-key-order-block.yml | 20 +++++++++++++++++++ test/test_transformer.py | 7 +++++++ 4 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 examples/playbooks/transform-key-order-block.transformed.yml create mode 100644 examples/playbooks/transform-key-order-block.yml diff --git a/.github/workflows/tox.yml b/.github/workflows/tox.yml index 81f4c3d50a..6c2c302d9f 100644 --- a/.github/workflows/tox.yml +++ b/.github/workflows/tox.yml @@ -71,7 +71,7 @@ jobs: env: # Number of expected test passes, safety measure for accidental skip of # tests. Update value if you add/remove tests. - PYTEST_REQPASS: 853 + PYTEST_REQPASS: 854 steps: - uses: actions/checkout@v4 with: diff --git a/examples/playbooks/transform-key-order-block.transformed.yml b/examples/playbooks/transform-key-order-block.transformed.yml new file mode 100644 index 0000000000..0f1ca120ed --- /dev/null +++ b/examples/playbooks/transform-key-order-block.transformed.yml @@ -0,0 +1,20 @@ +--- +- name: Testing multiple plays in a playbook + hosts: localhost + tasks: + - name: First block + when: true + block: + - name: Display a message + ansible.builtin.debug: + msg: Hello world! + +- name: A second play + hosts: localhost + tasks: + - name: Second block + when: true # <-- name key should be the second one + block: + - name: Display a message + ansible.builtin.debug: + msg: Hello world! diff --git a/examples/playbooks/transform-key-order-block.yml b/examples/playbooks/transform-key-order-block.yml new file mode 100644 index 0000000000..12a171e9e8 --- /dev/null +++ b/examples/playbooks/transform-key-order-block.yml @@ -0,0 +1,20 @@ +--- +- name: Testing multiple plays in a playbook + hosts: localhost + tasks: + - name: First block + when: true + block: + - name: Display a message + ansible.builtin.debug: + msg: Hello world! + +- name: A second play + hosts: localhost + tasks: + - name: Second block + block: + - name: Display a message + ansible.builtin.debug: + msg: Hello world! + when: true # <-- name key should be the second one diff --git a/test/test_transformer.py b/test/test_transformer.py index 0fd9522e77..7fb5fee92f 100644 --- a/test/test_transformer.py +++ b/test/test_transformer.py @@ -156,6 +156,13 @@ def fixture_runner_result( True, id="key_order_play_transform", ), + pytest.param( + "examples/playbooks/transform-key-order-block.yml", + 1, + True, + True, + id="key_order_block_transform", + ), pytest.param( "examples/.github/workflows/sample.yml", 0,