Skip to content

Commit

Permalink
Merge pull request #28 from dellis23/steenzout-ansible20
Browse files Browse the repository at this point in the history
added support for Ansible 2.0
  • Loading branch information
steenzout committed Apr 13, 2016
2 parents 1e8a423 + a9187a9 commit 75b306d
Show file tree
Hide file tree
Showing 11 changed files with 112 additions and 9 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ matrix:
include:
- python: 2.7
env: TOXENV=py27-ansible194
allow_failures:
- python: 2.7
env: TOXENV=py27-ansible2010

Expand Down
5 changes: 5 additions & 0 deletions ansible_toolkit/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# -*- coding: utf-8 -*-

from .dao import create_dao

DaoImpl = create_dao()
79 changes: 79 additions & 0 deletions ansible_toolkit/dao.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# -*- coding: utf-8 -*-

import ansible


class AnsibleDao(object):
"""
Interface for Ansible Data Access Object implementation.
"""

def __init__(self):
self.version = ansible.__version__

def get_vault_lib(self):
"""
Returns the Ansible VaultLib class.
"""
raise NotImplementedError

def read_vault_file(vault_password_file):
"""
Read a vault password from a file or if executable,
execute the script and
retrieve password from STDOUT
"""
raise NotImplementedError


class Ansible2(AnsibleDao):
"""
Ansible 2.x implementation.
"""

def __init__(self):
if not ansible.__version__.startswith('2'):
raise NotImplementedError(
'Cannot use Ansible 2.x implementation with Ansible 1.x!')

def get_vault_lib(self):
from ansible.parsing.vault import VaultLib
return VaultLib

def read_vault_file(self, vault_password_file):
from ansible.cli import CLI
from ansible.parsing.dataloader import DataLoader

return CLI.read_vault_password_file(vault_password_file, DataLoader())


class Ansible1(AnsibleDao):
"""
Ansible 1.x implementation.
"""

def __init__(self):
if not ansible.__version__.startswith('1'):
raise NotImplementedError(
'Cannot use Ansible 1.x implementation with Ansible 2.x!')

def get_vault_lib(self):
from ansible.utils.vault import VaultLib
return VaultLib

def read_vault_file(self, vault_password_file):
from ansible.utils import read_vault_file
return read_vault_file(vault_password_file)


def create_dao():
"""
Creates an Ansible data access object implementation that
implements the dao.AnsibleDao interface.
:return: Ansible data access object.
"""

if ansible.__version__.startswith('2'):
return Ansible2()
return Ansible1()
3 changes: 3 additions & 0 deletions ansible_toolkit/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
# -*- coding: utf-8 -*-


class MalformedGitDiff(Exception):
pass
10 changes: 7 additions & 3 deletions ansible_toolkit/git_diff.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
# -*- coding: utf-8 -*-

import difflib
from itertools import islice
import re
import subprocess

from ansible.utils.vault import VaultLib

from exceptions import MalformedGitDiff
from itertools import islice
from utils import get_vault_password, green, red, cyan, intense

from . import DaoImpl

VaultLib = DaoImpl.get_vault_lib()


def get_parts(git_diff_output):
r = re.compile(r"^diff --git", re.MULTILINE)
Expand Down
2 changes: 2 additions & 0 deletions ansible_toolkit/show_template.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# -*- coding: utf-8 -*-

from ansible.runner import Runner
from ansible.utils.template import template_from_file

Expand Down
2 changes: 2 additions & 0 deletions ansible_toolkit/show_vars.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# -*- coding: utf-8 -*-

from ansible.runner import Runner
from ansible.utils import combine_vars, template

Expand Down
6 changes: 4 additions & 2 deletions ansible_toolkit/utils.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# -*- coding: utf-8 -*-

import ConfigParser
import errno
import os

from ansible.inventory import Inventory
from ansible.utils import read_vault_file

from . import DaoImpl

config = ConfigParser.ConfigParser()

Expand Down Expand Up @@ -49,7 +51,7 @@ def get_vault_password(password_file=None):
password_file = config.get('vault', 'password_file')
except ConfigParser.NoSectionError:
return None
return read_vault_file(password_file)
return DaoImpl.read_vault_file(password_file)


# Inventory
Expand Down
4 changes: 3 additions & 1 deletion ansible_toolkit/utils_ansible.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# -*- coding: utf-8 -*-

import tempfile

import ansible.callbacks
from ansible.playbook import PlayBook
import ansible.constants as C
from utils import get_inventory, green, yellow
from utils import get_inventory, yellow


SETUP_PLAYBOOK = """
Expand Down
7 changes: 5 additions & 2 deletions ansible_toolkit/vault.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
# -*- coding: utf-8 -*-

import hashlib
import os

from ansible.utils.vault import VaultLib

from utils import get_vault_password, mkdir_p, split_path, get_files

from . import DaoImpl

ATK_VAULT = '.atk-vault'

VaultLib = DaoImpl.get_vault_lib()


def backup(path, password_file=None):
"""
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# -*- coding: utf-8 -*-

import tempfile
import unittest

Expand Down

0 comments on commit 75b306d

Please sign in to comment.