Run Ansible playbook progragmatically?

295 views
Skip to first unread message

Richard O'Hara

unread,
May 25, 2018, 12:56:06 PM5/25/18
to Ansible Project
I have a python application that calls the code below with the plan to run an Ansible playbook programmatically via the Ansible API versus using something like subprocess.

The code below runs but nothing actually seems to get executed. Grabbing the output of results just gives me a dictionary that looks like:

[{'plays': [localhost], 'playbook': 'playbooks/asg_elb_example.yml'}]



I am not sure where I am going wrong or what I am missing. Here is the code I am running.

import os
import sys
SCRIPT_DIR
= os.path.dirname(os.path.abspath(__file__))
ROOT_DIR
= os.path.dirname(SCRIPT_DIR)


import json
from collections import namedtuple
from ansible.parsing.dataloader import DataLoader
from ansible.vars.manager import VariableManager
from ansible.inventory.manager import InventoryManager
from ansible.playbook.play import Play
from ansible.executor.playbook_executor import PlaybookExecutor


def ansible_part():
    playbook_path
= "playbooks/asg_elb_example.yml"
    inventory_path
= "hosts"


   
Options = namedtuple('Options', ['connection', 'module_path', 'forks', 'become', 'become_method', 'become_user', 'check', 'diff', 'listhosts', 'listtasks', 'listtags', 'syntax'])
    loader
= DataLoader()
    options
= Options(connection='local', module_path='%s/' % (ROOT_DIR), forks=100, become=None, become_method=None, become_user=None, check=False,
                    diff
=False, listhosts=True, listtasks=False, listtags=False, syntax=False)
    passwords
= dict(vault_pass='secret')


    inventory
= InventoryManager(loader=loader, sources=[inventory_path])
    variable_manager
= VariableManager(loader=loader, inventory=inventory)
    executor
= PlaybookExecutor(  
                playbooks
=[playbook_path], inventory=inventory, variable_manager=variable_manager, loader=loader,  
                options
=options, passwords=passwords)  
    results
= executor.run()  
   
print results


def main():
    ansible_part
()
sys.exit(main())

Matt Martz

unread,
May 25, 2018, 1:57:51 PM5/25/18
to ansible...@googlegroups.com
We do not recommend interfacing directly with the ansible playbook python API.  It is not a public interface and is not guaranteed to be stable.

Instead, I'd recommend looking at the ansible-runner project, which provides a python API for executing playbooks: https://github.com/ansible/ansible-runner

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/809aebf4-79ae-4554-812e-2148d08aa806%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Matt Martz
@sivel
sivel.net

Richard O'Hara

unread,
May 26, 2018, 2:34:45 AM5/26/18
to Ansible Project
I have installed ansible and ansible-runn

I tried the following, just something simple

import ansible_runner
r
=ansible_runner.run(playbook="asg_elb_example.yml",private_data_dir="demo")

However ansible-runner is complaining about:
pexpect.exceptions.ExceptionPexpect: The command was not found or was not executable: ansible-playbook.

I am not sure what the issue is since I can run ansible-playbook from the command line with no issues.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.

Brian Coca

unread,
May 26, 2018, 8:06:31 AM5/26/18
to Ansible Project
listhosts is true, any list option bypasses execution.
Reply all
Reply to author
Forward
0 new messages