Skip to content

Instantly share code, notes, and snippets.

@skamithi
Last active August 29, 2015 14:18
Show Gist options
  • Save skamithi/f9bca2e5cad0e69a8866 to your computer and use it in GitHub Desktop.
Save skamithi/f9bca2e5cad0e69a8866 to your computer and use it in GitHub Desktop.
host key checking before running ansible playbook
# ansible constants are set when module is imported
# set host key checking before ansible imports
import os
os.environ['ANSIBLE_HOST_KEY_CHECKING'] = 'false'
from ansible import utils
from ansible import callbacks
from ansible import inventory
import ansible.runner
import ansible.playbook
from ansible.callbacks import display
class runplaybook():
def __init__(self):
return
@staticmethod
def playbook(host, private_key_file, playbookfile):
playbook_cb = callbacks.PlaybookCallbacks(verbose=utils.VERBOSITY)
stats = callbacks.AggregateStats()
runner_cb = callbacks.PlaybookRunnerCallbacks(stats, verbose=utils.VERBOSITY)
inv = inventory.Inventory([host])
playbook = ansible.playbook.PlayBook(
remote_user="root",
playbook=playbookfile,
callbacks=playbook_cb,
runner_callbacks=runner_cb,
stats=stats,
inventory=inv,
private_key_file=private_key_file)
results = playbook.run()
return results
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment