Skip to content

Instantly share code, notes, and snippets.

@colynn
Created February 17, 2017 03:40
Show Gist options
  • Save colynn/c033e9ab98e27c8e9f8c38dd1709ce08 to your computer and use it in GitHub Desktop.
Save colynn/c033e9ab98e27c8e9f8c38dd1709ce08 to your computer and use it in GitHub Desktop.
for ansible 2.1 api the bug of paramiko_ssh example
import os
import sys
from collections import namedtuple
from ansible.parsing.dataloader import DataLoader
from ansible.vars import VariableManager
from ansible.inventory import Inventory
from ansible.executor.playbook_executor import PlaybookExecutor
def playbook_executor_instance(ex_vars):
"""
Args:
re_user: string, remote_user
ex_vars: dict
"""
variable_manager = VariableManager()
loader = DataLoader()
bin_path = sys.path[0]
inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list='hosts')
playbook_path = bin_path + '/site.yml'
if not os.path.exists(playbook_path):
print '[INFO] The playbook does not exist'
sys.exit()
Options = namedtuple('Options', ['listtags', 'listtasks', 'listhosts', 'syntax',
'connection', 'module_path', 'forks', 'remote_user', 'private_key_file',
'ssh_common_args', 'ssh_extra_args', 'sftp_extra_args', 'scp_extra_args',
'become', 'become_method', 'become_user', 'verbosity', 'check']
)
options = Options(listtags=False, listtasks=False, listhosts=False, syntax=False, connection='paramiko',
module_path=None, forks=100, remote_user=None, private_key_file=None,
ssh_common_args=None, ssh_extra_args=None, sftp_extra_args=None, scp_extra_args=None,
become=True, become_method='sudo', become_user='root', verbosity=None, check=False
)
variable_manager.extra_vars = ex_vars
passwords = {}
pbex = PlaybookExecutor(playbooks=[playbook_path], inventory=inventory, variable_manager=variable_manager,
loader=loader, options=options, passwords=passwords)
return pbex
if __name__ == '__main__':
ex_vars = {'key1': "value1", 'key2': False}
pbex = playbook_executor_instance(ex_vars)
results = pbex.run()
#
action-test001 ansible_host=x.x.x.x ansible_port=22 ansible_user=example-user
---
- hosts: all
serial: 1
gather_facts: false
tasks:
# Opsstack initialize
- command: echo key1
- file: path=/tmp/hello.txt state=touch
when: key2|bool
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment