# Run play books in remote server using python ansible runner

**URL:** <https://forum.ansible.com/t/run-play-books-in-remote-server-using-python-ansible-runner/18156>\
**Category:** Ansible Project\
**Created:** [May 4, 2015, 10:41pm UTC](https://forum.ansible.com/t/run-play-books-in-remote-server-using-python-ansible-runner/18156 "2015-05-04T22:41:36Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shasti](https://avatars.discourse-cdn.com/v4/letter/s/a587f6/32.png) [@Shasti](https://forum.ansible.com/u/Shasti)\
**Post date:** [May 4, 2015, 10:41pm UTC](https://forum.ansible.com/t/run-play-books-in-remote-server-using-python-ansible-runner/18156/1 "2015-05-04T22:41:36Z")

</div>

Hi,

I am trying to automate script execution in remote servers using ansible/ansible play books. To start with I wanted to do a test, by invoking ansible runner to read IPs during runtime and touch a file or display a message in the remote host. I followed an example that found in a blog.

Sample Python script.

**import** ansible.runner  
**import** ansible.playbook  
**import** ansible.inventory  
**from** ansible **import** callbacks  
**from** ansible **import** utils  
**import** json

**def**  **setup\_inventory** ():  
_“”"_  
_setting up the inventory_

_set up a host_  
**_:return_** _:_  
_“”"_

example\_host = ansible.inventory.Host(  
name='10._._.\*,  
port=22  
)

example\_group = ansible.inventory.Group(  
name=‘sample\_group\_name’  
)  
example\_group.add\_host(example\_host)  
example\_inventory = ansible.inventory.Inventory()  
example\_inventory.add\_group(example\_group)  
example\_inventory.subset(‘sample\_group\_name’)  
**return** example\_inventory

**def**  **run\_playbook** ():

# set up Inventory

example\_inventory = setup\_inventory()

# setup callbacks

stats = callbacks.AggregateStats()  
playbook\_cb = callbacks.PlaybookCallbacks(verbose=utils.VERBOSITY)  
runner\_cb = callbacks.PlaybookRunnerCallbacks(stats, verbose=utils.VERBOSITY)

pb = ansible.playbook.PlayBook(  
playbook=“test.yml”,  
stats=stats,  
callbacks=playbook\_cb,  
runner\_callbacks=runner\_cb,  
inventory=example\_inventory,  
check=True  
)

pr = pb.run()  
**print** json.dumps(pr, sort\_keys=True, indent=4, separators=(‘,’, ': '))

**if**  **name** == ‘ **main** ’:  
run\_playbook()

Sample test.yml playbook:

- hosts: sample\_group\_name

user: username

sudo: no

tasks:

# touch a file, using symbolic modes to set the permissions (equivalent to 0644)

- debug: msg=“Ansible is great"

When is execute the py script, I am getting the below error. It looks like ansible can SSH to the box just fine, but not executing anything after that. I can run the playbook with out any issues in CLI.

/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7 /app/ansible\_run.py

PLAY [sample\_group\_name] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

GATHERING FACTS \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

ok: [10._._.\*]

TASK: [debug msg=“Ansible is great"] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

FATAL: no hosts matched or all hosts have already failed – aborting

{

“10._._.\*: {

“changed”: 0,

“failures”: 0,

“ok”: 1,

“skipped”: 0,

“unreachable”: 0

}

}

Process finished with exit code 0

Could someone point out what am I doing wrong here?

Thanks for your support & help.

Shasti
