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