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.
This commit is contained in:
Anders Ingemann 2018-07-19 15:28:33 +02:00
parent 92a3b5c079
commit 99bc43bbe9
No known key found for this signature in database
GPG key ID: 16A5864B259E59CD
2 changed files with 21 additions and 2 deletions

View file

@ -12,3 +12,6 @@ def resolve_tasks(taskset, manifest):
tasks.CheckPlaybookPath, tasks.CheckPlaybookPath,
tasks.RunAnsiblePlaybook, tasks.RunAnsiblePlaybook,
]) ])
if manifest.plugins['ansible'].get('extra_vars', {}).get('ansible_ssh_user', False):
taskset.add(tasks.RemoveAnsibleSSHUserDir)

View file

@ -2,6 +2,7 @@ from bootstrapvz.base import Task
from bootstrapvz.common.tasks import host from bootstrapvz.common.tasks import host
from bootstrapvz.common import phases from bootstrapvz.common import phases
from bootstrapvz.common.tools import rel_path from bootstrapvz.common.tools import rel_path
from bootstrapvz.common.tools import log_check_call
import os import os
import json import json
@ -47,8 +48,6 @@ class RunAnsiblePlaybook(Task):
@classmethod @classmethod
def run(cls, info): def run(cls, info):
from bootstrapvz.common.tools import log_check_call
# Extract playbook and directory # Extract playbook and directory
playbook = rel_path(info.manifest.path, info.manifest.plugins['ansible']['playbook']) playbook = rel_path(info.manifest.path, info.manifest.plugins['ansible']['playbook'])
@ -81,3 +80,20 @@ class RunAnsiblePlaybook(Task):
# Run and remove the inventory file # Run and remove the inventory file
log_check_call(cmd) log_check_call(cmd)
os.remove(inventory) 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)