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 for group_vars and host_vars #5

Merged
merged 4 commits into from
Jul 7, 2021
Merged

Support for group_vars and host_vars #5

merged 4 commits into from
Jul 7, 2021

Conversation

bemyak
Copy link
Contributor

@bemyak bemyak commented Jul 5, 2021

This PR adds support for loading external variables
from group_vars and host_vars directories. Also:

  • Refactor tests to use testify
  • Add few missing tests for public API functions
  • Fix warnings reported by the new Go

Fixes #3
Fixes #4

Sergei Gureev added 2 commits July 5, 2021 13:52
This PR adds support for loading external variables
from group_vars and host_vars directories. Also:
+ Refactor tests to use testify
+ Add few missing tests for public API functions
+ Fix warnings reported by the new Go
Copy link
Collaborator

@nemobis nemobis left a comment

Choose a reason for hiding this comment

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

Thanks! I've played with the tests a bit and the basic expectations seem to be met. The patch doesn't claim to support full-fledged variable precedence, which is fine, but maybe it's worth saying explicitly that it doesn't?

A couple of questions inline might or might not be worth answering in a comment before the walk function to briefly explain how it's supposed to work.

if err != nil {
return nil
}
if filepath.Dir(path) == root {
Copy link
Collaborator

Choose a reason for hiding this comment

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

So we ignore directories further down, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Depends on what you mean by "ignore" :)

We ignore their names, yes, because they have no semantical meaning. Only names of the first-level entities have it: they determine the name of a group or a host to which we should assign variables.

However, we do not ignore their contents: we read all files and directories recursively.

The WalkDir function traverses the file tree in the "depth-first" order. So, whenever we visit any first-level entity, we should remember its name so that all variables read further will go to the entity with the corresponding name.

vars.go Show resolved Hide resolved
Data from parsed files is now stored in private fields that are not
supposed to be modified after parsing.
This allows us:
+ make Reconsile function idempotent
+ implement proper multi-stage variable merging

Groups are now processed in lexical order, which matches how it is
implemented in Ansible
@bemyak
Copy link
Contributor Author

bemyak commented Jul 6, 2021

@nemobis About variable precedence: we do our best to support it, and if there is a mismatch, it should be considered a bug.

Let's go through that list. Role or playbook variables are completely out of the scope of this project. So, what's left is this part:

3. inventory file or script group vars
4. inventory group_vars/all
6. inventory group_vars/*
8. inventory file or script host vars
9. inventory host_vars/*

Points 4 and 6 are a bit misleading: in reality, group variables apply with hierarchy precedence, that's why the all group always has the least weight. Within one level groups and hosts are processed in lexical order: zgroup will have precedence over agroup.

This should match the variable processing logic introduced by the latest commit.

* Group loops no longer cause parsing to hang
* "all" group no longer contains self
* Add String() methods to group and hosts
* Fix wrong method call in populateInventoryVars
* Reconsile inventory method is now much more robust
@bemyak bemyak merged commit f74a08d into master Jul 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Feature Request] Parse inventory yaml files as well Parse folders host_vars and group_vars
3 participants