Skip to content

Commit ca85f7b

Browse files
committed
final adjustments for first version of this action
1 parent 56f3193 commit ca85f7b

File tree

5 files changed

+81
-7
lines changed

5 files changed

+81
-7
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ansible/

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,22 @@
44
Linting Ansible roles...
55

66
Work in Progress...
7+
8+
```yml
9+
inputs:
10+
target:
11+
description: |
12+
Target for ansible linter
13+
For example './', 'roles/my_role/' or 'site.yml'
14+
required: true
15+
required_collections:
16+
description: |
17+
You can define a required ansible collection here.
18+
They will be installed using ansible-galaxy collection install <YourInput>.
19+
required: false
20+
required_roles:
21+
description: |
22+
You can define a required ansible role here.
23+
They will be installed using ansible-galaxy role install <YourInput>.
24+
required: false
25+
```

action.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,23 @@ inputs:
99
Target for ansible linter
1010
For example './', 'roles/my_role/' or 'site.yml'
1111
required: true
12+
required_collections:
13+
description: |
14+
You can define a required ansible collection here.
15+
They will be installed using ansible-galaxy collection install <YourInput>.
16+
required: false
17+
required_roles:
18+
description: |
19+
You can define a required ansible role here.
20+
They will be installed using ansible-galaxy role install <YourInput>.
21+
required: false
1222
runs:
1323
using: docker
1424
image: Dockerfile
1525
env:
1626
TARGET: ${{ inputs.target }}
27+
REQCOLLECTIONS: ${{ inputs.required_collections }}
28+
REQROLES: ${{ inputs.required_roles }}
1729
branding:
1830
icon: 'upload-cloud'
1931
color: 'black'

ansible_docker.py

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,28 @@ def __init__(self, env_var_name):
1515
self.env_var_name = env_var_name
1616
self.env_var_value = os.getenv(env_var_name)
1717

18-
def check_environment_variable(self):
18+
def check_required_environment_variable(self):
1919
"""
20-
Check if Variable is defined.
20+
Check if required Variable is defined.
2121
exit if undefined
2222
"""
2323
if self.env_var_value is not None:
2424
print(f"The value of {self.env_var_name} is: {self.env_var_value}")
2525
return f"{self.env_var_value}"
26-
print(f"The environment variable {self.env_var_name} is not set.\nFAILED")
26+
print(f"The variable {self.env_var_name} is not set but needs to be defined.\nFAILED")
2727
sys.exit(1)
2828

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
2940
class AnsibleCommandExecution:
3041
"""
3142
running ansible commands
@@ -36,19 +47,46 @@ def run_command(self, command):
3647
Printing error on fail and exit
3748
"""
3849
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)
4052
return result.stdout
4153
except subprocess.CalledProcessError as error:
4254
print(f"Error running Ansible command: {error}\n\n{error.stdout}\n{error.stderr}")
4355
sys.exit(1)
4456

4557
if __name__ == "__main__":
58+
# define known enviroment vars
4659
ENV_TARGET_NAME = "TARGET"
60+
ENV_REQUIRED_COLLECTION_NAME = "REQCOLLECTIONS"
61+
ENV_REQUIRED_ROLE_NAME = "REROLE"
62+
63+
# check for target variable
4764
env_target = EnvironmentManager(ENV_TARGET_NAME)
48-
target = env_target.check_environment_variable()
49-
print(target)
65+
target = env_target.check_required_environment_variable()
5066

67+
# check for required collection variable
68+
env_collection = EnvironmentManager(ENV_REQUIRED_COLLECTION_NAME)
69+
reqired_collection = env_collection.check_optional_environment_variable()
5170

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
5376
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}"]
5492
version_info = ansible_version_checker.run_command(ansible_command)

requirements.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
collections:
3+
- name: l3d.git
4+
version: ">=1.0.10"

0 commit comments

Comments
 (0)