-
-
Save geek182/97f9b19c897adc75226a44ee83ac2f96 to your computer and use it in GitHub Desktop.
Ansible Playbook hello_world using Ansible 2.0 API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from ansible.cli import CLI | |
from ansible.executor import playbook_executor | |
from ansible import inventory | |
from ansible.parsing import dataloader | |
from ansible import vars | |
HOSTS = ['127.0.0.1'] | |
PLAYBOOK = "/tmp/hello.yml" | |
REMOTE_USER = "user" | |
SSH_KEY = "/home/user/.ssh/id_rsa" | |
loader = dataloader.DataLoader() | |
var_manager = vars.VariableManager() | |
inv = inventory.Inventory( | |
loader=loader, | |
variable_manager=var_manager, | |
host_list=HOSTS | |
) | |
# Need to generate some random huge options structure? Not sure how else to do this | |
parser = CLI.base_parser( | |
connect_opts=True, | |
meta_opts=True, | |
runas_opts=True, | |
subset_opts=True, | |
check_opts=True, | |
inventory_opts=True, | |
runtask_opts=True, | |
vault_opts=True, | |
fork_opts=True, | |
module_opts=True, | |
) | |
# This is all copied from playbook.py and slightly simplified | |
parser.add_option('--list-tasks', dest='listtasks', action='store_true') | |
parser.add_option('--list-tags', dest='listtags', action='store_true') | |
parser.add_option('--step', dest='step', action='store_true') | |
parser.add_option('--start-at-task', dest='start_at_task') | |
options, _ = parser.parse_args([]) # We literally just need a blank default | |
options.inventory = inv | |
# Set remote user and ssh key | |
options.remote_user = REMOTE_USER | |
options.private_key_file = SSH_KEY | |
pbex = playbook_executor.PlaybookExecutor( | |
playbooks=[PLAYBOOK], | |
inventory=inv, | |
variable_manager=var_manager, | |
loader=loader, | |
options=options, | |
passwords=None | |
) | |
pbex.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment