Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/saint-hilaire/ansible-directory-helper
https://github.com/saint-hilaire/ansible-directory-helper
Last synced: 24 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/saint-hilaire/ansible-directory-helper
- Owner: saint-hilaire
- Created: 2024-10-10T15:07:57.000Z (about 1 month ago)
- Default Branch: master
- Last Pushed: 2024-10-16T18:06:42.000Z (about 1 month ago)
- Last Synced: 2024-10-18T11:03:09.444Z (about 1 month ago)
- Language: Python
- Size: 1.95 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
### About
This is a small helper library to make it easier to supply different variables - currently primarily Ansible inventories - to the Ansible Runner library,
specifically its _Input Directory Hierarchy_.I am building this primarily to help me with [Lampsible](https://github.com/saint-hilaire/lampsible)
and [Docksible](https://github.com/saint-hilaire/docksible), but anyone who is working with
Ansible Runner might find this useful.### Installing
Do something like this:
```
# pyproject.toml
...
dependencies = [
...
"ansible-directory-helper@git+https://github.com/saint-hilaire/ansible-directory-helper",
...
]
```### Usage
If you are using Ansible Runner to develop tools to automate server installations, you likely
have many dynamic variables, including but not limited to the system users and host addresses
of your remote servers. Ansible Runner expects these variables to be present in the so called
_Input Directory Hierarchy_, a predefined directory structure on your local file system.
To avoid the hassle of managing these files and directories yourself, you can use this
library, which essentially manages this directory structure for you.Example:
```python
# my_automation_tool.pyfrom ansible_runner import Runner, RunnerConfig
from ansible_directory_helper.inventory_file import InventoryFileprivate_data_dir = './.my-tool/tmp'
project_data_dir = './my-tool/src/project'inventory_file = InventoryFile(
os.path.join(
private_data_dir,
'inventory',
'hosts'
)
)inventory_file.add_groups([
'group1',
'group2',
])
inventory_file.add_host('host1.example.com', 'group1')
inventory_file.add_host('host2.example.com', 'group2')
inventory_file.set_ansible_user('host1.example.com', 'user1')
inventory_file.set_ansible_user('host2.example.com', 'user2')inventory_file.write()
# Now the inventory file is in the exact location and format in which
# Ansible Runner expects it to be (private_data_dir/inventory/), so you can easily
# configure Runner by simply passing the private_data_dir.rc = RunnerConfig(
private_data_dir=private_data_dir,
project_dir=project_dir,
playbook='my-playbook.yml'
)rc.prepare()
r = Runner(config=rc)
r.run()```
### Running unit tests
```
python -m unittest test/test_inventory_file.py
```