fix pep8 lint

This commit is contained in:
Mathieu Bultel 2020-03-04 09:15:23 +01:00
parent 83ee5c1439
commit ca65791d1a
2 changed files with 27 additions and 34 deletions

View File

@ -15,8 +15,10 @@
import ansible_runner import ansible_runner
import logging import logging
import pwd
import os import os
import six import six
import sys
import tempfile import tempfile
import yaml import yaml
@ -29,12 +31,10 @@ LOG = logging.getLogger(__name__ + ".ansible")
class Ansible(object): class Ansible(object):
def __init__(self): def __init__(self):
self.log = logging.getLogger(__name__ + ".Ansible") self.log = logging.getLogger(__name__ + ".Ansible")
def _playbook_check(self, play, playbook_dir=None):
def _playbook_check(self, play):
"""Check if playbook exist""" """Check if playbook exist"""
if not os.path.exists(play): if not os.path.exists(play):
play = os.path.join(playbook_dir, play) play = os.path.join(playbook_dir, play)
@ -43,8 +43,7 @@ class Ansible(object):
self.log.debug('Ansible playbook {} found'.format(play)) self.log.debug('Ansible playbook {} found'.format(play))
return play return play
def _inventory(self, inventory, ansible_artifact_path):
def _inventory(self, inventory):
"""Handle inventory for Ansible""" """Handle inventory for Ansible"""
if inventory: if inventory:
if isinstance(inventory, six.string_types): if isinstance(inventory, six.string_types):
@ -62,7 +61,6 @@ class Ansible(object):
'hosts' 'hosts'
) )
def _creates_ansible_fact_dir(self, def _creates_ansible_fact_dir(self,
temp_suffix='validagions-libs-ansible'): temp_suffix='validagions-libs-ansible'):
"""Creates ansible fact dir""" """Creates ansible fact dir"""
@ -85,7 +83,6 @@ class Ansible(object):
) )
) )
def _get_extra_vars(self, extra_vars): def _get_extra_vars(self, extra_vars):
"""Manage extra_vars into a dict""" """Manage extra_vars into a dict"""
extravars = dict() extravars = dict()
@ -97,20 +94,19 @@ class Ansible(object):
extravars.update(yaml.safe_load(f.read())) extravars.update(yaml.safe_load(f.read()))
return extravars return extravars
def _callback_whitelist(self, callback_whitelist, output_callback):
def _callback_whitelist(self, callback_whitelist):
"""Set callback whitelist""" """Set callback whitelist"""
if callback_whitelist: if callback_whitelist:
callback_whitelist = ','.join([callback_whitelist, output_callback]) callback_whitelist = ','.join([callback_whitelist,
output_callback])
else: else:
callback_whitelist = output_callback callback_whitelist = output_callback
return ','.join([callback_whitelist, 'profile_tasks']) return ','.join([callback_whitelist, 'profile_tasks'])
def _ansible_env_var(self, output_callback, ssh_user, workdir, connection, def _ansible_env_var(self, output_callback, ssh_user, workdir, connection,
gathering_policy, module_path, key, gathering_policy, module_path, key,
extra_env_variables): extra_env_variables, ansible_timeout,
callback_whitelist):
"""Handle Ansible env var for Ansible config execution""" """Handle Ansible env var for Ansible config execution"""
cwd = os.getcwd() cwd = os.getcwd()
env = os.environ.copy() env = os.environ.copy()
@ -244,7 +240,6 @@ class Ansible(object):
return env return env
def _encode_envvars(self, env): def _encode_envvars(self, env):
"""Encode a hash of values. """Encode a hash of values.
@ -256,23 +251,21 @@ class Ansible(object):
else: else:
return env return env
def run(self, playbook, inventory, workdir, playbook_dir=None, def run(self, playbook, inventory, workdir, playbook_dir=None,
connection='smart', output_callback='yaml', connection='smart', output_callback='yaml',
ssh_user='root', key=None, module_path=None, ssh_user='root', key=None, module_path=None,
limit_hosts=None, tags=None, skip_tags=None, limit_hosts=None, tags=None, skip_tags=None,
verbosity=0, quiet=False, extra_vars=None, verbosity=0, quiet=False, extra_vars=None,
gathering_policy='smart', gathering_policy='smart',
extra_env_variables=None, parallel_run=False, extra_env_variables=None, parallel_run=False,
callback_whitelist=None, ansible_cfg=None, callback_whitelist=None, ansible_cfg=None,
ansible_timeout=30, reproduce_command=False, ansible_timeout=30, reproduce_command=False,
fail_on_rc=True): fail_on_rc=True):
if not playbook_dir: if not playbook_dir:
playbook_dir = workdir playbook_dir = workdir
playbook = self._playbook_check(play=playbook) playbook = self._playbook_check(playbook, playbook_dir)
self.log.info( self.log.info(
'Running Ansible playbook: {},' 'Running Ansible playbook: {},'
' Working directory: {},' ' Working directory: {},'
@ -286,12 +279,14 @@ class Ansible(object):
ansible_fact_path = self._creates_ansible_fact_dir() ansible_fact_path = self._creates_ansible_fact_dir()
extravars = self._get_extra_vars(extra_vars) extravars = self._get_extra_vars(extra_vars)
callback_whitelist = self._callback_whitelist(callback_whitelist) callback_whitelist = self._callback_whitelist(callback_whitelist,
output_callback)
# Set ansible environment variables # Set ansible environment variables
env = _ansible_env_var(output_callback, ssh_user, workdir, connection, env = self._ansible_env_var(output_callback, ssh_user, workdir,
gathering_policy, module_path, key, connection, gathering_policy, module_path,
extra_env_variables) key, extra_env_variables, ansible_timeout,
callback_whitelist)
command_path = None command_path = None
@ -310,7 +305,7 @@ class Ansible(object):
r_opts = { r_opts = {
'private_data_dir': workdir, 'private_data_dir': workdir,
'project_dir': playbook_dir, 'project_dir': playbook_dir,
'inventory': self._inventory(inventory), 'inventory': self._inventory(inventory, ansible_artifact_path),
'envvars': self._encode_envvars(env=env), 'envvars': self._encode_envvars(env=env),
'playbook': playbook, 'playbook': playbook,
'verbosity': verbosity, 'verbosity': verbosity,
@ -320,7 +315,7 @@ class Ansible(object):
'fact_cache_type': 'jsonfile', 'fact_cache_type': 'jsonfile',
'artifact_dir': ansible_artifact_path, 'artifact_dir': ansible_artifact_path,
'rotate_artifacts': 256 'rotate_artifacts': 256
} }
if skip_tags: if skip_tags:
r_opts['skip_tags'] = skip_tags r_opts['skip_tags'] = skip_tags
@ -348,5 +343,3 @@ class Ansible(object):
status, rc = runner.run() status, rc = runner.run()
return playbook, rc, status return playbook, rc, status

View File

@ -309,7 +309,7 @@ def get_results(results):
if len(new_log_files) > len(results): if len(new_log_files) > len(results):
LOG.warn('Looks like we have more log files than ' LOG.warn('Looks like we have more log files than '
'executed validations') 'executed validations')
for i in new_log_files: for i in new_log_files:
os.rename( os.rename(