11#!/usr/bin/env python3
22import subprocess
33import pathlib
4- import sys
54import shutil
65import tempfile
76import argparse
8- import os
9-
10- BASE_PATH = pathlib .Path (__file__ ).resolve ().parent
11- VENV_PATH = BASE_PATH / ".venv"
12-
13- # Ansible changes a lot between releases and deprecates a lot of stuff each of
14- # them. Using a pinned ansible identical between all team members should
15- # reduce the churn.
16- def install_ansible (venv_path = VENV_PATH ):
17- requirements = BASE_PATH / "requirements.txt"
18- venv_requirements = venv_path / "installed-requirements.txt"
19-
20- # Avoid installing ansible in the virtualenv multiple times
21- if venv_requirements .exists () and \
22- venv_requirements .read_bytes () == requirements .read_bytes ():
23- return
24-
25- print ("creating a new virtual environment and install ansible in it..." )
26- shutil .rmtree (venv_path , ignore_errors = True )
27- subprocess .run ([sys .executable , "-m" , "venv" , str (venv_path )], check = True )
28- subprocess .run ([
29- str (venv_path / "bin" / "pip" ), "install" , "-r" , str (requirements ),
30- ], check = True )
31- shutil .copy (str (requirements ), str (venv_requirements ))
32-
33- def create_workspace (dir , env , playbook ):
34- env_dir = BASE_PATH / "envs" / env
35- # Create a temporary directory merging together the chosen
36- # environment, the chosen playbook and the shared files.
37- (dir / "play" ).mkdir ()
38- (dir / "play" / "roles" ).symlink_to (BASE_PATH / "roles" )
39- (dir / "play" / "group_vars" ).symlink_to (BASE_PATH / "group_vars" )
40- (dir / "play" / "playbook.yml" ).symlink_to (
41- BASE_PATH / "playbooks" / (playbook + ".yml" )
42- )
43- (dir / "env" ).symlink_to (env_dir )
44- (dir / "ansible.cfg" ).symlink_to (BASE_PATH / "ansible.cfg" )
7+ import ansible
458
469def run_playbook (args ):
4710 tempdir = pathlib .Path (tempfile .mkdtemp ())
48- create_workspace (tempdir , args .env , args .playbook )
11+ ansible . create_workspace (tempdir , args .env , args .playbook )
4912 try :
5013 # Invoke the ansible binary installed in the virtualenv
5114 ansible_args = [
52- str (VENV_PATH / "bin" / "ansible-playbook" ),
15+ str (ansible . VENV_PATH / "bin" / "ansible-playbook" ),
5316 "-i" , str (tempdir / "env" / "hosts" ),
5417 str (tempdir / "play" / "playbook.yml" ),
5518 ]
@@ -76,5 +39,5 @@ if __name__ == "__main__":
7639 )
7740 args = parser .parse_args ()
7841
79- install_ansible ()
42+ ansible . install_ansible ()
8043 run_playbook (args )
0 commit comments