Re: [ansible-project] Example for running a playbook using Python API

7,325 views
Skip to first unread message

Michael DeHaan

unread,
Feb 23, 2013, 6:37:52 PM2/23/13
to ansible...@googlegroups.com
Probably the best example of running a playbook via Python code is
actually /usr/bin/ansible-playbook itself, though it uses nearly all
of the flags.

https://github.com/ansible/ansible/blob/devel/bin/ansible-playbook

Anything referenced there either comes from command line flags or is
constructed further up in that file.

Hope that helps!



On Sat, Feb 23, 2013 at 6:24 PM, Kyle Heath <hea...@gmail.com> wrote:
> I'm new to Ansible... I would like to create a library of playbooks and then
> use ansible's python api to run the playbooks and get the result from a
> larger python script.
>
> I see there is an example of how to use the ansible.runner.Runner() from
> python, but that seems to be useful for running modules. I would like to
> run a playbook from a yml file (specifying the host, and private_key
> parameters directly from python). I found the playbooks.Playbook() python
> class, but the constructor requires several parameters which I am not sure
> how to construct... callbacks... runner callbacks... etc
>
> Any pointers or any examples of how to use the python API?
>
> Cheers,
> Kyle
>
> --
> 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.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
Message has been deleted

Jazzed

unread,
Nov 3, 2014, 1:36:35 PM11/3/14
to ansible...@googlegroups.com
import ansible.runner
import ansible.playbook
from ansible import callbacks
from ansible import utils

stats = callbacks.AggregateStats()
playbook_cb = callbacks.PlaybookCallbacks(verbose=utils.VERBOSITY)
runner_cb = callbacks.PlaybookRunnerCallbacks(stats, verbose=utils.VERBOSITY)


pb = ansible.playbook.PlayBook(
    playbook="nseries.yml",
    stats=stats,
    callbacks=playbook_cb,
    runner_callbacks=runner_cb,
    check=True
)
for (play_ds, play_basedir) in zip(pb.playbook, pb.play_basedirs):
    import ipdb
    ipdb.set_trace()
    # Can play around here to see what's going on.
   

pb.run()  # This runs the playbook

verv...@gmail.com

unread,
Feb 23, 2015, 9:11:55 AM2/23/15
to ansible...@googlegroups.com, brian....@cyaninc.com
Hi Jazzed,

I used your example as a base for triggering ansible through a python script, however while the output suggest that the connection to the node is made and the playbook is running, individual steps on the various roles fail consistently in contrast to running the playbook using the ansible-playbook command line tool passing the same playbook.

The output suggests no clear reasons for this behavior.

Any ideas on that?

Thanks,
Dimitris

Lavish Aggarwal

unread,
Mar 4, 2015, 12:31:19 AM3/4/15
to ansible...@googlegroups.com, brian....@cyaninc.com
Thanks Jazzed,

Its working fine only if I have my host listed in /etc/ansible/hosts, but I want to pass my custom host file while running a playbook. Can you tell me how to do so?

Mike Putnam

unread,
Mar 16, 2015, 9:05:15 AM3/16/15
to ansible...@googlegroups.com, brian....@cyaninc.com
Lavish,

You can override the default inventory file ( /etc/ansible/hosts ) with the shell environment variable ANSIBLE_HOSTS.


$ ANSIBLE_HOSTS=/path/to/ec2.py python myscript.py

张利高

unread,
Feb 22, 2017, 3:47:54 AM2/22/17
to Ansible Project, brian....@cyaninc.com
How to run ansible-playbook on ansible2.0 or later version?
The ansible.playbook has been removed.


在 2014年11月4日星期二 UTC+8上午2:36:35,Jazzed写道:

Dat Dao

unread,
Apr 25, 2017, 1:27:58 PM4/25/17
to Ansible Project
Hi all,
   I have a Playbook which is used to push template configuration to the router, It work fine if I run command ansible-playbook playbook_name.yml
But now I don't want call Playbook manually, I want to creat a file (such as pb.run) and then use it to call playbook.
How I can do it?
Please help me, thanks!

Vào 06:37:52 UTC+7 Chủ Nhật, ngày 24 tháng 2 năm 2013, Michael DeHaan đã viết:

balubhai555

unread,
Apr 25, 2017, 3:24:27 PM4/25/17
to Ansible Project
Hello Dao,

I did the same in ansible. 


  • I created one pb.sh within the file i wrote below   
              ansible-playbook playbook1.yml
  
and saved.
  • then i change the permission chmod +x pb.sh   and run those as   
           [root@localhost]# ./pb.sh 

i think it is useful to you also.
 


 

mantis quito

unread,
Feb 5, 2020, 3:33:03 AM2/5/20
to Ansible Project
Reply all
Reply to author
Forward
0 new messages