From 99bc43bbe9d7df8e352f6ce6368cc80f070fdc99 Mon Sep 17 00:00:00 2001 From: Anders Ingemann Date: Thu, 19 Jul 2018 15:28:33 +0200 Subject: [PATCH] ansible: Add task that removes the $HOME/.ansible directory on guest If ansible_ssh_user is set through extra_vars, ansible will create a .ansible directory in the home dir of that user. However, it will be owned by root, which is not what ansible will be expecting when it is provision through SSH at a later date. --- bootstrapvz/plugins/ansible/__init__.py | 3 +++ bootstrapvz/plugins/ansible/tasks.py | 20 ++++++++++++++++++-- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/bootstrapvz/plugins/ansible/__init__.py b/bootstrapvz/plugins/ansible/__init__.py index 0dde5ed..ae6932f 100644 --- a/bootstrapvz/plugins/ansible/__init__.py +++ b/bootstrapvz/plugins/ansible/__init__.py @@ -12,3 +12,6 @@ def resolve_tasks(taskset, manifest): tasks.CheckPlaybookPath, tasks.RunAnsiblePlaybook, ]) + + if manifest.plugins['ansible'].get('extra_vars', {}).get('ansible_ssh_user', False): + taskset.add(tasks.RemoveAnsibleSSHUserDir) diff --git a/bootstrapvz/plugins/ansible/tasks.py b/bootstrapvz/plugins/ansible/tasks.py index 6fd644e..83fe8f8 100644 --- a/bootstrapvz/plugins/ansible/tasks.py +++ b/bootstrapvz/plugins/ansible/tasks.py @@ -2,6 +2,7 @@ from bootstrapvz.base import Task from bootstrapvz.common.tasks import host from bootstrapvz.common import phases from bootstrapvz.common.tools import rel_path +from bootstrapvz.common.tools import log_check_call import os import json @@ -47,8 +48,6 @@ class RunAnsiblePlaybook(Task): @classmethod def run(cls, info): - from bootstrapvz.common.tools import log_check_call - # Extract playbook and directory playbook = rel_path(info.manifest.path, info.manifest.plugins['ansible']['playbook']) @@ -81,3 +80,20 @@ class RunAnsiblePlaybook(Task): # Run and remove the inventory file log_check_call(cmd) os.remove(inventory) + + +class RemoveAnsibleSSHUserDir(Task): + description = 'Removing .ansible directory' + phase = phases.user_modification + predecessors = [RunAnsiblePlaybook] + + @classmethod + def run(cls, info): + ssh_user = info.manifest.plugins['ansible']['extra_vars']['ansible_ssh_user'] + # os.path.expanduser does not work in a chroot, + # so we use sh instead + [ssh_user_home] = log_check_call(['chroot', info.root, 'sh', '-c', 'echo ~' + ssh_user]) + from shutil import rmtree + # [1:] to remove the leading slash from e.g. /home/ansible + ansible_dir_path = os.path.join(info.root, ssh_user_home[1:], '.ansible') + rmtree(ansible_dir_path)