Skip to content

Commit c3cecc9

Browse files
authored
Merge pull request #3 from ansible-actions/docs
install requirements from file and explain vars
2 parents b7dd156 + 280535d commit c3cecc9

File tree

3 files changed

+71
-35
lines changed

3 files changed

+71
-35
lines changed

Diff for: README.md

+30-29
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,13 @@
11
Action Ansible Linting
2-
===============================
2+
========================
33

4-
Linting Ansible roles...
4+
Linting ansible roles using the ansible-lint package directly from pypi.
55

6-
Work in Progress...
6+
Optionally it is possible to install some requirements like ansible collections, roles or pip packages. For more details about it, have a look at the Variables
77

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-
python_dependency:
26-
description: |
27-
Install a Python Package using pip
28-
They will be installed using pip install <YourInput>.
29-
required: false
30-
```
8+
## Usage
319

32-
## Example Setup
10+
Example of ``.github/workflows/ansible-linting-check.yml``
3311
```yaml
3412
---
3513
name: Ansible Lint check
@@ -51,7 +29,30 @@ jobs:
5129
fetch-depth: 0
5230

5331
- name: Run ansible-lint
54-
uses: ansible-actions/[email protected]
32+
uses: ansible-actions/[email protected]
33+
with:
34+
target: "./"
35+
```
36+
37+
This will run the command ``ansible-lint ./``
38+
39+
You can install some reuqirements, example:
40+
```yml
41+
[...]
5542
with:
56-
target: "site.yml"
43+
target: "./"
44+
required_collections: 'community.general'
45+
python_dependency: 'jmespath'
5746
```
47+
*This will install the community.general collections as well as the jmespath pip package.*
48+
49+
## Variables
50+
51+
| name | required | description | example values |
52+
| --- | --- | --- | --- |
53+
| ``target`` | true | Target for ansible linter | ``./`` or ``site.yml`` or ``path/to/ansible/`` |
54+
| ``required_collections`` | - | define one ansible collection to install | ``community.general`` |
55+
| ``collections_yml`` | - | define path of yml file for installing multiple ansible collections | ``requirements.yml`` |
56+
| ``required_roles`` | - | define one ansible role to install | ``namespace.rolename`` |
57+
| ``python_dependency`` | - | define one pip package to install | ``jmespath`` |
58+
| ``python_dependency_file`` | - | define path of txt file for installing multiple python packages | ``requirements.txt`` |

Diff for: action.yml

+15-3
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,38 @@ inputs:
1212
required_collections:
1313
description: |
1414
You can define a required ansible collection here.
15-
They will be installed using ansible-galaxy collection install <YourInput>.
15+
They will be installed using ansible-galaxy collection install <YourInput> --upgrade.
16+
required: false
17+
collections_yml:
18+
description: |
19+
Install multiple Collections, defined from requirements.yml file
20+
They will be installed using ansible-galaxy collection install -r <YourInput> --upgrade.
1621
required: false
1722
required_roles:
1823
description: |
1924
You can define a required ansible role here.
20-
They will be installed using ansible-galaxy role install <YourInput>.
25+
They will be installed using ansible-galaxy role install <YourInput> --upgrade.
2126
required: false
2227
python_dependency:
2328
description: |
2429
Install a Python Package using pip
25-
They will be installed using pip install <YourInput>.
30+
They will be installed using pip install --upgrade <YourInput>.
31+
required: false
32+
python_dependency_file:
33+
description: |
34+
Install a Python Packages from file using pip
35+
They will be installed using pip install --upgrade -r <YourInput>.
2636
required: false
2737
runs:
2838
using: docker
2939
image: Dockerfile
3040
env:
3141
TARGET: ${{ inputs.target }}
3242
REQCOLLECTIONS: ${{ inputs.required_collections }}
43+
COLLECTIONSYML: ${{ inputs.collections_yml }}
3344
REQROLES: ${{ inputs.required_roles }}
3445
PIPPACKAGE: ${{ inputs.python_dependency }}
46+
PIPPACKAGETXT: ${{ inputs.python_dependency_file }}
3547
branding:
3648
icon: 'check-circle'
3749
color: 'blue'

Diff for: ansible_docker.py

+26-3
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ def run_command(self, command):
6060
ENV_REQUIRED_COLLECTION_NAME = "REQCOLLECTIONS"
6161
ENV_REQUIRED_ROLE_NAME = "REROLE"
6262
ENV_PIPPACKAGE_NAME = "PIPPACKAGE"
63+
ENV_PIPPACKAGE_TXT_NAME = "PIPPACKAGETXT"
64+
ENV_COLLECTIONS_YML_NAME = "COLLECTIONSYML"
6365

6466
# check for target variable
6567
env_target = EnvironmentManager(ENV_TARGET_NAME)
@@ -72,36 +74,57 @@ def run_command(self, command):
7274
env_collection = EnvironmentManager(ENV_REQUIRED_COLLECTION_NAME)
7375
reqired_collection = env_collection.check_optional_environment_variable()
7476

77+
# check for required collection variable
78+
env_collection_yml = EnvironmentManager(ENV_COLLECTIONS_YML_NAME)
79+
reqired_collection_yml = env_collection_yml.check_optional_environment_variable()
80+
7581
# check for required role variable
7682
env_role = EnvironmentManager(ENV_REQUIRED_ROLE_NAME)
7783
reqired_role = env_role.check_optional_environment_variable()
7884

79-
# check for required role variable
85+
# check for install pip packages variable
8086
env_pip = EnvironmentManager(ENV_PIPPACKAGE_NAME)
8187
pip_pkg = env_pip.check_optional_environment_variable()
8288

89+
# check for install packages.txt variable
90+
env_pip_txt = EnvironmentManager(ENV_PIPPACKAGE_TXT_NAME)
91+
pip_pkg_txt = env_pip_txt.check_optional_environment_variable()
92+
8393
# run ansible commands
8494
ansible_version_checker = AnsibleCommandExecution()
8595

86-
# Optionally install required ansible collections
96+
# Optionally install required ansible collections directly
8797
if bool(reqired_collection):
8898
ansible_command = ["ansible-galaxy", "collection", "install",
8999
f"{reqired_collection}", "--upgrade"]
90100
version_info = ansible_version_checker.run_command(ansible_command)
91101
print(f"COLLECTION INSTALL SUCCESSFUL\n{version_info}")
92102

103+
# Optionally install required ansible collections from yml file
104+
if bool(reqired_collection_yml):
105+
ansible_command = ["ansible-galaxy", "collection", "install", "-r",
106+
f"{reqired_collection_yml}", "--upgrade"]
107+
version_info = ansible_version_checker.run_command(ansible_command)
108+
print(f"COLLECTION.YML INSTALL SUCCESSFUL\n{version_info}")
109+
93110
# Optionally install required ansible roles
94111
if bool(reqired_role):
95112
ansible_command = ["ansible-galaxy", "role", "install", f"{reqired_role}", "--upgrade"]
96113
version_info = ansible_version_checker.run_command(ansible_command)
97114
print(f"ROLE INSTALL SUCCESSFUL\n{version_info}")
98115

99-
# Optionally install pip package
116+
# Optionally install pip package directly
100117
if bool(pip_pkg):
101118
ansible_command = ["pip", "install", "--upgrade", f"{pip_pkg}"]
102119
version_info = ansible_version_checker.run_command(ansible_command)
103120
print(f"PIP PACKAGE INSTALL SUCCESSFUL\n{version_info}")
104121

122+
# Optionally install pip package from file
123+
if bool(pip_pkg_txt):
124+
ansible_command = ["pip", "install", "--upgrade", "-r", f"{pip_pkg_txt}"]
125+
version_info = ansible_version_checker.run_command(ansible_command)
126+
print(f"PIP PACKAGE.TXT INSTALL SUCCESSFUL\n{version_info}")
127+
105128
# run ansible lint
106129
ansible_command = ["ansible-lint", f"{target}"]
107130
linter_run = ansible_version_checker.run_command(ansible_command)

0 commit comments

Comments
 (0)