A small helper library to manage local directories for Ansible Runner
Project description
About
This is a small helper library to make it easier to supply different variables to the Ansible Runner library, specifically its Input Directory Hierarchy.
I am building this primarily to help me with Lampsible and 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 (inventory, in Ansible's parlance), and numerous other variables that your playbooks might expect during runtime. 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:
# my_automation_tool.py
from ansible_runner import Runner, RunnerConfig
from ansible_directory_helper.private_data import PrivateData
private_data_dir = './.my-tool/tmp'
project_dir = './my-tool/src/project'
private_data_obj = PrivateData(private_data_dir)
private_data_obj.add_inventory_groups([
'group1',
'group2',
])
private_data_obj.add_inventory_host('host1.example.com', 'group1')
private_data_obj.add_inventory_host('host2.example.com', 'group2')
private_data_obj.set_inventory_ansible_user('host1.example.com', 'user1')
private_data_obj.set_inventory_ansible_user('host2.example.com', 'user2')
private_data_obj.write_inventory()
private_data_obj.set_extravar('some_string', 'Hello, World!')
private_data_obj.set_extravar('some_list', ['Hello', 'World'])
private_data_obj.set_extravar('some_dict', {'Hello': 'World'})
private_data_obj.set_extravar('some_none', None)
private_data_obj.write_env()
# Now the files for inventory and extravars are in the exact location and format in which
# Ansible Runner expects them to be, so you can easily
# configure Runner by simply passing the private_data_dir - all the required
# configuration will work.
rc = RunnerConfig(
private_data_dir=private_data_dir,
project_dir=project_dir,
playbook='my-playbook.yml'
)
rc.prepare()
r = Runner(config=rc)
r.run()
# Do this to delete the private_data_dir from your filesystem.
# This is important because it contains sensitive data.
private_data_obj.cleanup_dir()
Running unit tests
python -m unittest
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file ansible_directory_helper-0.2.0.tar.gz
.
File metadata
- Download URL: ansible_directory_helper-0.2.0.tar.gz
- Upload date:
- Size: 4.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 973f8c8e01f59014974a9986f21c1d0eb6d41d2221e6574928614fac51955c13 |
|
MD5 | 3a828570be01b6b116695e414ff202f3 |
|
BLAKE2b-256 | 38305697884eb6849bd395119a0a78f3c32a3791ce8d85e9f85d9d708bdfcdfe |
File details
Details for the file ansible_directory_helper-0.2.0-py3-none-any.whl
.
File metadata
- Download URL: ansible_directory_helper-0.2.0-py3-none-any.whl
- Upload date:
- Size: 4.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 82767b9f47e946dd2661ea41a95f74d32899fa848e8515b894bdb85748d6fde0 |
|
MD5 | effa01635905fed4958f3c101eec6694 |
|
BLAKE2b-256 | f7f04f203a9e1d5b37919924590587e73eb9fb6402494f16261e6790265997f9 |