Skip to content

Commit

Permalink
Use yaml.load_all for multiple yaml document (ansible#3679)
Browse files Browse the repository at this point in the history
  • Loading branch information
shatakshiiii authored Aug 23, 2023
1 parent ee85ac9 commit c1b41f2
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tox.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ jobs:
WSLENV: FORCE_COLOR:PYTEST_REQPASS:TOXENV:GITHUB_STEP_SUMMARY
# Number of expected test passes, safety measure for accidental skip of
# tests. Update value if you add/remove tests.
PYTEST_REQPASS: 812
PYTEST_REQPASS: 813
steps:
- name: Activate WSL1
if: "contains(matrix.shell, 'wsl')"
Expand Down
23 changes: 23 additions & 0 deletions examples/playbooks/multi_yaml_doc.transformed.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
- name: First problematic play
hosts: localhost
tasks:
- name: Echo a message
ansible.builtin.shell: echo hello # <-- command-instead-of-shell
changed_when: false
---
- name: second problematic play # <-- name[casing]
hosts: localhost
tasks:
- name: Remove file (delete file)
ansible.builtin.file:
path: /etc/foo.txt
state: absent
---
- name: Third problematic play
hosts: localhost
tasks:
- name: Remove file (delete file)
file: # <-- fqcn[action-core]
path: /etc/foo.txt
state: absent
23 changes: 23 additions & 0 deletions examples/playbooks/multi_yaml_doc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
- name: First problematic play
hosts: localhost
tasks:
- name: Echo a message
ansible.builtin.shell: echo hello # <-- command-instead-of-shell
changed_when: false
---
- name: second problematic play # <-- name[casing]
hosts: localhost
tasks:
- name: Remove file (delete file)
ansible.builtin.file:
path: /etc/foo.txt
state: absent
---
- name: Third problematic play
hosts: localhost
tasks:
- name: Remove file (delete file)
file: # <-- fqcn[action-core]
path: /etc/foo.txt
state: absent
6 changes: 5 additions & 1 deletion src/ansiblelint/yaml_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import ruamel.yaml.events
from ruamel.yaml.comments import CommentedMap, CommentedSeq, Format
from ruamel.yaml.composer import ComposerError
from ruamel.yaml.constructor import RoundTripConstructor
from ruamel.yaml.emitter import Emitter, ScalarAnalysis

Expand Down Expand Up @@ -935,7 +936,10 @@ def loads(self, stream: str) -> Any:
# https://sourceforge.net/p/ruamel-yaml/tickets/460/

text, preamble_comment = self._pre_process_yaml(stream)
data = self.load(stream=text)
try:
data = self.load(stream=text)
except ComposerError:
data = self.load_all(stream=text)
if preamble_comment is not None and isinstance(
data,
(CommentedMap, CommentedSeq),
Expand Down
6 changes: 6 additions & 0 deletions test/test_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ def fixture_runner_result(
pytest.param("examples/playbooks/vars/empty.yml", 1, False, id="empty"),
pytest.param("examples/playbooks/name-case.yml", 1, True, id="name_case"),
pytest.param("examples/playbooks/fqcn.yml", 3, True, id="fqcn"),
pytest.param(
"examples/playbooks/multi_yaml_doc.yml",
1,
False,
id="multi_yaml_doc",
),
pytest.param(
"examples/playbooks/transform_command_instead_of_shell.yml",
3,
Expand Down

0 comments on commit c1b41f2

Please sign in to comment.