Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support extra_vars in syntax check rule #1342

Merged
merged 5 commits into from Feb 15, 2021
Merged

support extra_vars in syntax check rule #1342

merged 5 commits into from Feb 15, 2021

Conversation

skarzi
Copy link
Contributor

@skarzi skarzi commented Feb 12, 2021

Adds ability to define a set of extra vars to be passed to ansible when we call --syntax-check, so we can avoid failures caused by them.

Closes: #1299

@pytest.fixture
def config_options():
"""Return configuration options that will be restored after testrun."""
global options # pylint: disable=global-statement
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not happy with this fixture, probably the better idea will be to create some ConfigWrapper like for instance pytest-django do in the settings fixture (reference).
Do you have any other ideas for a more elegant solution?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that django approach is considerably better than what we have but if you want to do this, please do it as a separated pull-request after we sort the rest.

.ansible-lint Outdated

# Define required Ansible's variables to satisfy syntax check
extra_vars:
- foo: bar
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not how vars are defined in ansible, you mush use a dictionary, not a list of dictionaries for variables, somethng like

extra_vars:
  foo: bar
  zoo: moo

cmd = ['ansible-playbook', '--syntax-check', str(lintable.path)]
extra_vars_cmd = []
if options.extra_vars:
extra_vars_cmd = ['--extra-vars', json.dumps(options.extra_vars)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will likely fail to work well for more complex vars, like one containing shell escapable vars, multilines and so.

IMHO, I would instead dump these vars inside .cache/extra_vars.yml and pass @.cache/extra_vars.yml to ansible, so it will load them from there.

Be sure to add some problematic var example to the list and see if it works. I really do not want to get bug reports about linter failing to load var files, when this is the job of ansible itself.

Maybe we should only support mentioning the extra_vars_filename inside config and force users that want this feature to add another file with them, so we can rely on ansible to process the file.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see why this should fail for more complex variables. The shell is not involved in interpreting this (see shell=False below), this is passed directly as-is to ansible-playbook.

Copy link
Member

@ssbarnea ssbarnea Feb 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will be glad to see it as working as is, lets put something like this into the example, which also works as test-file:

extra_vars:
  # sample values used for testing
  foo: |
    line1
    line2
  bar: ":{;\t$()"

@ssbarnea ssbarnea merged commit 109669c into ansible:master Feb 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

AnsibleSyntaxCheckRule doesn't support extra vars
3 participants