mirror of
https://github.com/ansible/awx.git
synced 2026-03-19 01:47:31 -02:30
Fixes AC-346. Allow playbooks with invalid YAML syntax to show up in list of available playbooks.
This commit is contained in:
@@ -6,6 +6,7 @@ import hmac
|
|||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
|
import re
|
||||||
import shlex
|
import shlex
|
||||||
|
|
||||||
# PyYAML
|
# PyYAML
|
||||||
@@ -669,6 +670,7 @@ class Project(CommonModel):
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def playbooks(self):
|
def playbooks(self):
|
||||||
|
valid_re = re.compile(r'^\s*?-?\s*?(?:hosts|include):\s*?.*?$')
|
||||||
results = []
|
results = []
|
||||||
project_path = self.get_project_path()
|
project_path = self.get_project_path()
|
||||||
if project_path:
|
if project_path:
|
||||||
@@ -677,17 +679,17 @@ class Project(CommonModel):
|
|||||||
if os.path.splitext(filename)[-1] != '.yml':
|
if os.path.splitext(filename)[-1] != '.yml':
|
||||||
continue
|
continue
|
||||||
playbook = os.path.join(dirpath, filename)
|
playbook = os.path.join(dirpath, filename)
|
||||||
# Filter any invalid YAML files.
|
|
||||||
try:
|
|
||||||
data = yaml.safe_load(file(playbook).read())
|
|
||||||
except (IOError, yaml.YAMLError):
|
|
||||||
continue
|
|
||||||
# Filter files that do not have either hosts or top-level
|
# Filter files that do not have either hosts or top-level
|
||||||
# includes.
|
# includes. Use regex to allow files with invalid YAML to
|
||||||
|
# show up.
|
||||||
|
matched = False
|
||||||
try:
|
try:
|
||||||
if 'hosts' not in data[0] and 'include' not in data[0]:
|
for line in file(playbook):
|
||||||
continue
|
if valid_re.match(line):
|
||||||
except (TypeError, IndexError, KeyError):
|
matched = True
|
||||||
|
except IOError:
|
||||||
|
continue
|
||||||
|
if not matched:
|
||||||
continue
|
continue
|
||||||
playbook = os.path.relpath(playbook, project_path)
|
playbook = os.path.relpath(playbook, project_path)
|
||||||
# Filter files in a roles subdirectory.
|
# Filter files in a roles subdirectory.
|
||||||
|
|||||||
@@ -118,11 +118,11 @@ class ProjectsTest(BaseTest):
|
|||||||
self.assertEqual(len(project.playbooks), 1)
|
self.assertEqual(len(project.playbooks), 1)
|
||||||
write_test_file(project, 'blah.yml', '')
|
write_test_file(project, 'blah.yml', '')
|
||||||
self.assertEqual(len(project.playbooks), 1)
|
self.assertEqual(len(project.playbooks), 1)
|
||||||
# Invalid YAML
|
# Invalid YAML (now allowed to show)
|
||||||
project = self.projects[4]
|
project = self.projects[4]
|
||||||
self.assertEqual(len(project.playbooks), 1)
|
self.assertEqual(len(project.playbooks), 1)
|
||||||
write_test_file(project, 'blah.yml', TEST_PLAYBOOK + '----')
|
write_test_file(project, 'blah.yml', TEST_PLAYBOOK + '----')
|
||||||
self.assertEqual(len(project.playbooks), 1)
|
self.assertEqual(len(project.playbooks), 2)
|
||||||
# No hosts or includes
|
# No hosts or includes
|
||||||
project = self.projects[5]
|
project = self.projects[5]
|
||||||
self.assertEqual(len(project.playbooks), 1)
|
self.assertEqual(len(project.playbooks), 1)
|
||||||
|
|||||||
Reference in New Issue
Block a user