@@ -15,17 +15,28 @@ def __init__(self, env_var_name):
15
15
self .env_var_name = env_var_name
16
16
self .env_var_value = os .getenv (env_var_name )
17
17
18
- def check_environment_variable (self ):
18
+ def check_required_environment_variable (self ):
19
19
"""
20
- Check if Variable is defined.
20
+ Check if required Variable is defined.
21
21
exit if undefined
22
22
"""
23
23
if self .env_var_value is not None :
24
24
print (f"The value of { self .env_var_name } is: { self .env_var_value } " )
25
25
return f"{ self .env_var_value } "
26
- print (f"The environment variable { self .env_var_name } is not set.\n FAILED" )
26
+ print (f"The variable { self .env_var_name } is not set but needs to be defined .\n FAILED" )
27
27
sys .exit (1 )
28
28
29
+ def check_optional_environment_variable (self ):
30
+ """
31
+ Check if optional Variable is defined.
32
+ exit if undefined
33
+ """
34
+ if self .env_var_value is not None :
35
+ print (f"The value of { self .env_var_name } is: { self .env_var_value } " )
36
+ return f"{ self .env_var_value } "
37
+ return False
38
+
39
+ # pylint: disable=R0903
29
40
class AnsibleCommandExecution :
30
41
"""
31
42
running ansible commands
@@ -36,19 +47,46 @@ def run_command(self, command):
36
47
Printing error on fail and exit
37
48
"""
38
49
try :
39
- result = subprocess .run (command , stdout = subprocess .PIPE , stderr = subprocess .PIPE , text = True , check = True )
50
+ result = subprocess .run (command , stdout = subprocess .PIPE ,
51
+ stderr = subprocess .PIPE , text = True , check = True )
40
52
return result .stdout
41
53
except subprocess .CalledProcessError as error :
42
54
print (f"Error running Ansible command: { error } \n \n { error .stdout } \n { error .stderr } " )
43
55
sys .exit (1 )
44
56
45
57
if __name__ == "__main__" :
58
+ # define known enviroment vars
46
59
ENV_TARGET_NAME = "TARGET"
60
+ ENV_REQUIRED_COLLECTION_NAME = "REQCOLLECTIONS"
61
+ ENV_REQUIRED_ROLE_NAME = "REROLE"
62
+
63
+ # check for target variable
47
64
env_target = EnvironmentManager (ENV_TARGET_NAME )
48
- target = env_target .check_environment_variable ()
49
- print (target )
65
+ target = env_target .check_required_environment_variable ()
50
66
67
+ # check for required collection variable
68
+ env_collection = EnvironmentManager (ENV_REQUIRED_COLLECTION_NAME )
69
+ reqired_collection = env_collection .check_optional_environment_variable ()
51
70
52
- ansible_command = ["ansible-lint" , f"{ target } " ]
71
+ # check for required role variable
72
+ env_role = EnvironmentManager (ENV_REQUIRED_ROLE_NAME )
73
+ reqired_role = env_role .check_optional_environment_variable ()
74
+
75
+ # run ansible commands
53
76
ansible_version_checker = AnsibleCommandExecution ()
77
+
78
+ # Optionally install required ansible collections
79
+ if bool (reqired_collection ):
80
+ ansible_command = ["ansible-galaxy" , "collection" , "install" , f"{ reqired_collection } " , "--upgrade" ]
81
+ version_info = ansible_version_checker .run_command (ansible_command )
82
+ print (f"COLLECTION INSTALL SUCCESSFUL\n { version_info } " )
83
+
84
+ # Optionally install required ansible roles
85
+ if bool (reqired_role ):
86
+ ansible_command = ["ansible-galaxy" , "role" , "install" , f"{ reqired_role } " , "--upgrade" ]
87
+ version_info = ansible_version_checker .run_command (ansible_command )
88
+ print (f"ROLE INSTALL SUCCESSFUL\n { version_info } " )
89
+
90
+ # run ansible lint
91
+ ansible_command = ["ansible-lint" , f"{ target } " ]
54
92
version_info = ansible_version_checker .run_command (ansible_command )
0 commit comments