1
1
#!/usr/bin/env python3
2
2
import subprocess
3
3
import pathlib
4
- import sys
5
4
import shutil
6
5
import tempfile
7
6
import 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
45
8
46
9
def run_playbook (args ):
47
10
tempdir = pathlib .Path (tempfile .mkdtemp ())
48
- create_workspace (tempdir , args .env , args .playbook )
11
+ ansible . create_workspace (tempdir , args .env , args .playbook )
49
12
try :
50
13
# Invoke the ansible binary installed in the virtualenv
51
14
ansible_args = [
52
- str (VENV_PATH / "bin" / "ansible-playbook" ),
15
+ str (ansible . VENV_PATH / "bin" / "ansible-playbook" ),
53
16
"-i" , str (tempdir / "env" / "hosts" ),
54
17
str (tempdir / "play" / "playbook.yml" ),
55
18
]
@@ -76,5 +39,5 @@ if __name__ == "__main__":
76
39
)
77
40
args = parser .parse_args ()
78
41
79
- install_ansible ()
42
+ ansible . install_ansible ()
80
43
run_playbook (args )
0 commit comments