Created
December 23, 2022 21:57
-
-
Save rnrbarbosa/52e447148a20861a45d52a09a7ee5cc5 to your computer and use it in GitHub Desktop.
Wrapper to run Ansible Playbooks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ansible_runner | |
class AnsiblePlaybookRunner: | |
def __init__(self, playbook_path, inventory_path=None, options=None, private_data_dir=None): | |
self.playbook_path = playbook_path | |
self.inventory_path = inventory_path | |
self.options = options or {} | |
self.private_data_dir = private_data_dir | |
def run(self): | |
# Create an Ansible Runner object | |
runner = ansible_runner.Runner( | |
playbook=self.playbook_path, | |
inventory=self.inventory_path, | |
private_data_dir=self.private_data_dir, | |
options=self.options | |
) | |
# Run the playbook | |
result = runner.run() | |
# Return the result | |
return result | |
# Set the path to your playbook and inventory file | |
playbook_path = '/path/to/your/playbook.yml' | |
inventory_path = '/path/to/your/inventory.ini' | |
private_data_dir = '/path/to/private/data/dir' | |
# Create an instance of AnsiblePlaybookRunner | |
runner = AnsiblePlaybookRunner(playbook_path, inventory_path, {'limit': 'group1'}, private_data_dir) | |
# Run the playbook | |
result = runner.run() | |
# Check the result | |
if result.status == 'successful': | |
print('Playbook run was successful') | |
else: | |
print('Playbook run failed') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment