|
| 1 | +# AGENTS.md |
| 2 | + |
| 3 | +This file provides guidance for AI coding agents working on the ansible-modules-hashivault project. |
| 4 | + |
| 5 | +## Project Overview |
| 6 | + |
| 7 | +This project provides Ansible modules for interacting with HashiCorp Vault. It includes over 90 modules covering all major Vault operations including: |
| 8 | + |
| 9 | +- Secret management (read, write, list, delete) |
| 10 | +- Authentication methods (token, userpass, LDAP, approle, AWS, Azure, K8s, JWT/OIDC) |
| 11 | +- Secret engines (KV, PKI, Database, Consul, SSH, Azure) |
| 12 | +- Administrative operations (init, seal/unseal, rekey, generate root) |
| 13 | +- Policy and ACL management |
| 14 | +- Identity management (entities, groups, aliases) |
| 15 | +- Audit backend configuration |
| 16 | + |
| 17 | +The modules are designed to run on localhost and communicate with a remote Vault server. |
| 18 | + |
| 19 | +## Code Structure |
| 20 | + |
| 21 | +- `ansible/modules/hashivault/` - All Ansible modules (90+ modules) |
| 22 | +- `ansible/module_utils/hashivault.py` - Shared utility functions and client initialization |
| 23 | +- `ansible/plugins/lookup/` - Lookup plugins for Vault integration |
| 24 | +- `ansible/plugins/action/` - Action plugins for file operations |
| 25 | +- `functional/` - Functional test suite |
| 26 | +- `example/` - Example playbooks |
| 27 | + |
| 28 | +## Build and Test |
| 29 | + |
| 30 | +### Running Tests |
| 31 | + |
| 32 | +Tests are orchestrated via tox: |
| 33 | + |
| 34 | +```bash |
| 35 | +# Run all tests (lint + functional tests + docs) |
| 36 | +tox |
| 37 | + |
| 38 | +# Run only linting |
| 39 | +tox -e pep8 |
| 40 | + |
| 41 | +# Run only functional tests |
| 42 | +tox -e py39 |
| 43 | + |
| 44 | +# Run only documentation generation |
| 45 | +tox -e docs |
| 46 | +``` |
| 47 | + |
| 48 | +Functional tests require a running Vault instance and are executed via: |
| 49 | + |
| 50 | +```bash |
| 51 | +bash -ex functional/run.sh |
| 52 | +``` |
| 53 | + |
| 54 | +### Building Documentation |
| 55 | + |
| 56 | +```bash |
| 57 | +./makedocs.sh |
| 58 | +``` |
| 59 | + |
| 60 | +Documentation is generated from module docstrings and published to GitHub Pages. |
| 61 | + |
| 62 | +## Code Style and Conventions |
| 63 | + |
| 64 | +### Python Style |
| 65 | + |
| 66 | +- **PEP8 compliant** with max line length of **120 characters** |
| 67 | +- **Indentation**: 4 spaces (no tabs) |
| 68 | +- Linting via `pycodestyle --max-line-length=120 ansible` |
| 69 | +- Always add a final newline to files |
| 70 | +- Use UTF-8 encoding |
| 71 | +- Trim trailing whitespace |
| 72 | + |
| 73 | +### YAML Style |
| 74 | + |
| 75 | +- **Indentation**: 2 spaces |
| 76 | +- Use lowercase `true`/`false` for booleans |
| 77 | + |
| 78 | +### Module Structure |
| 79 | + |
| 80 | +All modules should follow this pattern: |
| 81 | + |
| 82 | +1. Import required libraries (hvac, ansible.module_utils) |
| 83 | +2. Define ANSIBLE_METADATA with status and supported_by |
| 84 | +3. Define DOCUMENTATION string with module description, options, and examples |
| 85 | +4. Define RETURN string documenting return values |
| 86 | +5. Implement main logic using hashivault utilities |
| 87 | +6. Use `hashivault_argspec()` for common Vault connection parameters |
| 88 | +7. Use `hashivault_init()` to create the AnsibleModule instance |
| 89 | +8. Use `hashivault_client()` to create the Vault client |
| 90 | + |
| 91 | +### Security Considerations |
| 92 | + |
| 93 | +**CRITICAL**: Never log sensitive data |
| 94 | + |
| 95 | +- Always mark sensitive parameters with `no_log=True` (tokens, passwords, role_ids, secret_ids) |
| 96 | +- The shared module utils automatically excludes common false positives (0, 1, True, False, 'ttl') from no_log |
| 97 | +- Use `module.no_log_values.discard()` for legitimate non-sensitive values that trigger false positives |
| 98 | + |
| 99 | +**Vault Authentication**: Modules support multiple auth types via environment variables: |
| 100 | +- VAULT_ADDR, VAULT_TOKEN, VAULT_AUTHTYPE, VAULT_USER, VAULT_PASSWORD |
| 101 | +- VAULT_ROLE_ID, VAULT_SECRET_ID (for approle) |
| 102 | +- VAULT_CACERT, VAULT_CLIENT_CERT, VAULT_CLIENT_KEY (for TLS) |
| 103 | +- VAULT_NAMESPACE (for Enterprise namespaces) |
| 104 | + |
| 105 | +## Testing Guidelines |
| 106 | + |
| 107 | +- Each module should have corresponding functional tests in `functional/` |
| 108 | +- Tests use a local Vault container for integration testing |
| 109 | +- Test files are named `test_hashivault_<module_name>.sh` |
| 110 | +- All tests must pass before merging |
| 111 | +- Support for `check_mode` should be added where applicable |
| 112 | + |
| 113 | +## Commit Message Format |
| 114 | + |
| 115 | +Use conventional commit format: |
| 116 | + |
| 117 | +- `fix:` - Bug fixes |
| 118 | +- `feat:` - New features or modules |
| 119 | +- `chore:` - Maintenance tasks, dependency updates |
| 120 | +- `docs:` - Documentation updates |
| 121 | +- `test:` - Test additions or fixes |
| 122 | + |
| 123 | +Examples: |
| 124 | +``` |
| 125 | +fix: audit test broken with vault change |
| 126 | +feat: add disable_local_ca_jwt param in k8s auth config |
| 127 | +chore: update readme |
| 128 | +``` |
| 129 | + |
| 130 | +All commits are GPG-signed by maintainers. |
| 131 | + |
| 132 | +## Pull Request Guidelines |
| 133 | + |
| 134 | +- Ensure all CI checks pass (lint, tests, docs) |
| 135 | +- Update documentation if adding new modules or parameters |
| 136 | +- Add functional tests for new functionality |
| 137 | +- Follow existing code patterns and conventions |
| 138 | +- PRs are merged to `main` branch |
| 139 | + |
| 140 | +## Development Setup |
| 141 | + |
| 142 | +Standard pip installation doesn't work due to Ansible's module structure limitations. Use one of: |
| 143 | + |
| 144 | +```bash |
| 145 | +# Option 1: Use the link script |
| 146 | +./link.sh |
| 147 | + |
| 148 | +# Option 2: Build and install from dist |
| 149 | +rm -rf dist |
| 150 | +python setup.py sdist |
| 151 | +pip install ./dist/ansible-modules-hashivault-*.tar.gz |
| 152 | +``` |
| 153 | + |
| 154 | +## Dependencies |
| 155 | + |
| 156 | +- **hvac** >= 2.1.0 - Primary Vault client library |
| 157 | +- **ansible** - Module framework |
| 158 | +- **requests** - HTTP client (used for EC2 IAM role authentication) |
| 159 | + |
| 160 | +## Common Patterns |
| 161 | + |
| 162 | +### Error Handling |
| 163 | + |
| 164 | +```python |
| 165 | +try: |
| 166 | + result = client.secrets.kv.read_secret_version(path=secret) |
| 167 | +except InvalidPath: |
| 168 | + return {'rc': 1, 'failed': True, 'msg': 'Secret not found'} |
| 169 | +``` |
| 170 | + |
| 171 | +### Version Support |
| 172 | + |
| 173 | +- KV secrets engine v1 and v2 are both supported |
| 174 | +- Use `version` parameter to specify KV version (default is v2) |
| 175 | +- Use `mount_point` parameter for non-default mount points (default is `/secret`) |
| 176 | + |
| 177 | +### Return Values |
| 178 | + |
| 179 | +All modules should return: |
| 180 | +- `changed` - Boolean indicating if state changed |
| 181 | +- `rc` - Return code (0 for success) |
| 182 | +- `failed` - Boolean indicating failure (optional, for failures) |
| 183 | +- `msg` - Error message (for failures) |
| 184 | +- Module-specific data fields |
| 185 | + |
| 186 | +## CI/CD |
| 187 | + |
| 188 | +GitHub Actions workflow (`.github/workflows/test.yml`): |
| 189 | +- **Lint job**: Runs pycodestyle on Python 3.9 |
| 190 | +- **Tests job**: Runs tox functional tests on Python 3.9 |
| 191 | +- **Docs job**: Validates documentation generation |
| 192 | + |
| 193 | +All jobs must pass for PR merge. |
0 commit comments