A dynamic Ansible inventory plugin that retrieves GitHub repositories and creates an inventory based on repository metadata. This plugin allows you to manage and organize your GitHub repositories as Ansible hosts, making it easy to automate operations across multiple repositories.
- 🔍 Dynamic Repository Discovery: Automatically discovers repositories from a GitHub organization
- 🏷️ Flexible Grouping: Group repositories by:
- Team assignments (via repository topics)
- Programming languages
- Custom regex patterns
- Repository metadata
- 🚀 Caching Support: Built-in caching to improve performance and reduce API calls
- 🔐 Secure Authentication: Uses GitHub Personal Access Tokens for authentication
- 📝 Rich Metadata: Exposes all repository metadata as Ansible variables
- 🔧 Configurable Filtering: Filter repositories using search patterns and regex
- Ansible >= 2.15.4
- Python >= 3.8
- PyGithub library
pip install -r requirements.txtansible-galaxy collection install saltyblu.githubCreate a YAML file (e.g., github_repositories.yml) with your configuration:
---
plugin: github_repositories_inventory
url: "https://github.com"
access_token: "your_github_pat_token"
org: "your-organization"
repository_filter: "*-deployment" # Optional: filter repositories
regex_filter: "^project-(\w+)" # Optional: group by regex pattern
cache: true # Optional: enable caching
group_by_languages: true # Optional: group by programming languages
show_archived_repos: false # Optional: include archived repositories| Option | Description | Required | Default | Environment Variable |
|---|---|---|---|---|
url |
GitHub URL | No | https://github.com/ |
GITHUB_INVENTORY_URL |
access_token |
GitHub Personal Access Token | Yes | - | GITHUB_INVENTORY_ACCESS_TOKEN |
org |
GitHub organization name | Yes | - | GITHUB_INVENTORY_ORG |
repository_filter |
Repository search filter | No | "" |
GITHUB_INVENTORY_SEARCH_FILTER |
regex_filter |
Regex pattern for grouping | No | "" |
GITHUB_INVENTORY_REGEX_GROUP_FILTER |
cache |
Enable inventory caching | No | false |
- |
group_by_languages |
Group by programming languages | No | false |
GITHUB_INVENTORY_GROUP_BY_LANGUAGES |
show_archived_repos |
Include archived repositories | No | false |
GITHUB_INVENTORY_ARCHIVED |
Create a GitHub Personal Access Token with the following permissions:
repo(if working with private repositories)public_repo(for public repositories)
# Test the inventory
ansible-inventory -i github_repositories.yml --list
# Use with ansible-playbook
ansible-playbook -i github_repositories.yml your-playbook.yml{
"_meta": {
"hostvars": {
"my-awesome-repo": {
"ansible_host": "localhost",
"ansible_connection": "local",
"name": "my-awesome-repo",
"full_name": "myorg/my-awesome-repo",
"clone_url": "https://github.com/myorg/my-awesome-repo.git",
"ssh_url": "git@github.com:myorg/my-awesome-repo.git",
"topics": ["team-backend", "python", "api"],
"team": "team-backend",
"default_branch": "main",
"languages": {"Python": 95.2, "Dockerfile": 4.8}
}
}
},
"all": {
"children": ["team_backend", "python", "unassigned"]
},
"team_backend": {
"hosts": ["my-awesome-repo"]
},
"python": {
"hosts": ["my-awesome-repo"]
}
}Repositories are automatically grouped by team if they have topics starting with team-:
# Repository topics: ["team-backend", "python", "api"]
# Result: Repository assigned to group "team_backend"Enable group_by_languages: true to group repositories by their primary programming languages:
group_by_languages: true
# Result: Groups like "python", "javascript", "go", etc.Use regex patterns to create custom groups:
regex_filter: "^(project|service)-(\w+)"
# Repository: "project-auth-service"
# Result: Groups "main-project", "project", "auth"---
plugin: github_repositories_inventory
access_token: "{{ github_token }}"
org: "myorg"
repository_filter: "*-deployment OR *-infra"
cache: true---
plugin: github_repositories_inventory
access_token: "{{ github_token }}"
org: "myorg"
regex_filter: "^(\w+)-(dev|staging|prod)$"
group_by_languages: false
cache: true---
plugin: github_repositories_inventory
access_token: "{{ github_token }}"
org: "myorg"
show_archived_repos: true
group_by_languages: true
cache: trueThe plugin supports caching to improve performance:
cache: trueCache files are stored in the default Ansible cache directory. To force cache refresh:
ansible-inventory -i github_repositories.yml --list --flush-cacheYou can use environment variables instead of hardcoding values:
export GITHUB_INVENTORY_ACCESS_TOKEN="your_token"
export GITHUB_INVENTORY_ORG="your-org"
export GITHUB_INVENTORY_GROUP_BY_LANGUAGES="true"
ansible-inventory -i github_repositories.yml --listThe plugin logs to dynamic_inventory.log in the current directory. To see debug information, check this file or increase Ansible verbosity:
ansible-inventory -i github_repositories.yml --list -vvv- Authentication Errors: Verify your GitHub token has the correct permissions
- No Repositories Found: Check your organization name and repository filter
- Rate Limiting: Enable caching to reduce API calls
- Empty Groups: Ensure your regex patterns are correct
See the complete example in inventory.sample/my.github_repositories.yml.
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
This project is licensed under the GPL-3.0-or-later license. See the source code for full license text.
- Volker Schmitz (saltyblu)
- Martin Soentgenrath (merin80)
See CHANGELOG.md for version history and changes.