Pass dynamic JSON/YAML inventory to ansible CLI

139 views
Skip to first unread message

W Dec

unread,
Feb 15, 2017, 9:27:31 AM2/15/17
to Ansible Project
Hi All,

while trying, and so far not very successfully, to work with the (overly verbose) Ansible 2 programmatic API, I came to think of another possible solution to my problem, but am looking for tips on how to put it into action.
The problem: I have a python REST server which accepts host registrations. From these I'm able to generate a JSON/YAML inventory blob, that I'd like to pass simply to the ansible-playbook command line command spun up in a seperate thread. I do not want to write the inventory to a local file.

In other words, invoke ansible CLI from my python script, but poassing a dynamically generated inventory blob.

Playing around with all kinds of "ansible-playbook -i ... <name of playbook>"  redirects was so far unsucessful. Any pointers/examples of something like this?

Cheers,
W.

Matt Martz

unread,
Feb 15, 2017, 12:03:00 PM2/15/17
to ansible...@googlegroups.com
You are going to need an actual file.  There really isn't a way to just pass an inventory blob into ansible, that doesn't exist as a file.

I've done it with the ansible python API, but it was a bit complex, and potentially very fragile as the API was not designed for this.

--
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/ab9f8682-ee55-4918-b225-0abddb2f1768%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Matt Martz
@sivel
sivel.net

Matt Martz

unread,
Feb 15, 2017, 12:04:08 PM2/15/17
to ansible...@googlegroups.com
I hit enter too early, my other recommendation was an inventory script, that wouldn't have to have anything hard coded, and could pull from the same database that your "REST server" utilizes.  For documentation on dynamic inventories see:

W Dec

unread,
Feb 16, 2017, 4:48:31 AM2/16/17
to Ansible Project
Ok thanks. Indeed I've resorted to writing the inventory and host parameters to a tmp file, off a jinja2 template, and using the ansible -i option to read from it. Still it would be a nice enhancement for Ansible to support such "piped" inventories, as the Ansible API is way too cumbersome to use (passing all those config params is a nightmare in 2.0+).

Rough working code is:

    inventory_template = jinja2.Template(inventory)
    rendered_inventory = inventory_template.render({
        'host': "centos2"
    })
   
    # Create a temporary file and write the template string to it
    hosts_inv = NamedTemporaryFile(delete=False, bufsize=0)
    hosts_inv.write(rendered_inventory)

    cmd = "ansible-playbook " + playbook + \
        " -i " + hosts_inv.name
        " -e " + extra_params
       
    if debug:
        cmd += " -vvv"
    print("Ansible command: %s" % cmd)
    with hosts_inv:
        try:
            ret = call(cmd, shell=True)
        except Exception as e:
            print "ERROR: [%s]: could not execute [%s]" % (cmd, e.errno)
        if not ret:
            print("Command '%s' executed successfully" % cmd)
        else:
            print "ERROR: [%s]: failed in execution not execute [%s]" % (cmd, ret)

 

On Wednesday, February 15, 2017 at 6:04:08 PM UTC+1, Matt Martz wrote:
I hit enter too early, my other recommendation was an inventory script, that wouldn't have to have anything hard coded, and could pull from the same database that your "REST server" utilizes.  For documentation on dynamic inventories see:

On Wed, Feb 15, 2017 at 11:02 AM, Matt Martz <ma...@sivel.net> wrote:
You are going to need an actual file.  There really isn't a way to just pass an inventory blob into ansible, that doesn't exist as a file.

I've done it with the ansible python API, but it was a bit complex, and potentially very fragile as the API was not designed for this.
On Wed, Feb 15, 2017 at 7:40 AM, W Dec <wdec...@gmail.com> wrote:
Hi All,

while trying, and so far not very successfully, to work with the (overly verbose) Ansible 2 programmatic API, I came to think of another possible solution to my problem, but am looking for tips on how to put it into action.
The problem: I have a python REST server which accepts host registrations. From these I'm able to generate a JSON/YAML inventory blob, that I'd like to pass simply to the ansible-playbook command line command spun up in a seperate thread. I do not want to write the inventory to a local file.

In other words, invoke ansible CLI from my python script, but poassing a dynamically generated inventory blob.

Playing around with all kinds of "ansible-playbook -i ... <name of playbook>"  redirects was so far unsucessful. Any pointers/examples of something like this?

Cheers,
W.

--
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-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
--
Matt Martz
@sivel
sivel.net
Reply all
Reply to author
Forward
0 new messages