Lint the whole Ansible directory (#2576)

This commit is contained in:
Marco Ferrari 2022-03-14 20:47:04 +01:00 committed by GitHub
parent e98f38ce93
commit 4b16b5b473
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
29 changed files with 53 additions and 174 deletions

View file

@ -4,7 +4,7 @@ This folder holds the test cases for **Ansible**.
## Additional Docs ## Additional Docs
The folder **ghe-initialize** is pulled from the **GitHub-Demo-Stack** and is a valid **Ansible** role. The folder **roles/ghe-initialize** is pulled from the **GitHub-Demo-Stack** and is a valid **Ansible** role.
## Good Test Cases ## Good Test Cases

View file

@ -1,39 +0,0 @@
---
###########################################
###########################################
#### GitHub Services-Engineering Stack ####
#### ####
#### GHE Primary HA backup-utils ONLY ####
###########################################
###########################################
###############################
# Description of the playbook #
###############################
# description: Builds GHE Primary, HA, and backup-utils.
# detailed_description: Builds GHE Primary, HA, and backup-utils.
######################
## Configure GitHub ##
######################
- hosts: Yo-Mama
vars:
github_host: "{{ hostvars['github_primary'].ansible_host }}"
probot_server_ip: "{{ hostvars['backup-utils'].ansible_host }}"
roles:
- role: ghe-initialize
##########################################
## Run ghe-config-apply for all changes ##
##########################################
# Due to us hot loading some data into GHE, the final
# run of ghe-config-apply hangs and leaves the system in an odd state
# The simplist option is to run the process
# 1 more time at the end to solve the issue
- hosts: github_primary
tasks:
- block:
include_role:
tasks_from: ghe-config-apply.yml
tags:
- github

View file

@ -1,42 +0,0 @@
---
###########################################
###########################################
#### GitHub Services-Engineering Stack ####
#### ####
#### GHE Primary HA backup-utils ONLY ####
###########################################
###########################################
###############################
# Description of the playbook #
###############################
# description: Builds GHE Primary, HA, and backup-utils.
# detailed_description: Builds GHE Primary, HA, and backup-utils.
######################
## Configure GitHub ##
######################
- hosts: github_primary
vars:
demo_github_initial_user: "{{ hostvars['localhost'].local_user }}"
github_host: "{{ hostvars['github_primary'].ansible_host }}"
probot_server_ip: "{{ hostvars['backup-utils'].ansible_host }}"
roles:
- role: ghe_initialize
##########################################
## Run ghe-config-apply for all changes ##
##########################################
# Due to us hot loading some data into GHE, the final
# run of ghe-config-apply hangs and leaves the system in an odd state
# The simplist option is to run the process
# 1 more time at the end to solve the issue
- hosts: github_primary
tasks:
- block:
- name: GHE-Config-Apply
include_role:
name: ghe_initialize
tasks_from: ghe-config-apply.yml
tags:
- github

View file

@ -0,0 +1,5 @@
---
all:
hosts:
test.example.com: null
...

View file

@ -0,0 +1,8 @@
---
- hosts: github_primary
vars:
demo_github_initial_user: "{{ hostvars['localhost'].local_user }}"
github_host: "{{ hostvars['github_primary'].ansible_host }}"
probot_server_ip: "{{ hostvars['backup-utils'].ansible_host }}"
roles:
- role: ghe_initialize

View file

@ -0,0 +1,4 @@
---
collections:
- ansible.posix
...

View file

@ -1,30 +1,9 @@
--- ---
##########################
##########################
## Ansible Linter rules ##
##########################
##########################
#############################
# Exclude paths from linter #
#############################
# exclude_paths:
######################## ########################
# Make output parsable # # Make output parsable #
######################## ########################
parseable: true parseable: true
#######################
# Set output to quiet #
#######################
quiet: true
#####################
# Path to rules dir #
#####################
# rulesdir:
################ ################
# Tags to skip # # Tags to skip #
################ ################
@ -36,11 +15,6 @@ skip_list:
- 'command-instead-of-shell' # Allow use of shell when you want - 'command-instead-of-shell' # Allow use of shell when you want
- 'no-handler' # Allow step to run like handler - 'no-handler' # Allow step to run like handler
##################
# Tags to follow #
##################
# tags:
############# #############
# Use rules # # Use rules #
############# #############
@ -49,4 +23,4 @@ use_default_rules: true
################# #################
# Set verbosity # # Set verbosity #
################# #################
verbosity: 1 verbosity: 2

View file

@ -278,6 +278,7 @@ But if you wish to select or exclude specific linters, we give you full control
| **ACTIONS_RUNNER_DEBUG** | `false` | Flag to enable additional information about the linter, versions, and additional output. | | **ACTIONS_RUNNER_DEBUG** | `false` | Flag to enable additional information about the linter, versions, and additional output. |
| **ANSIBLE_CONFIG_FILE** | `.ansible-lint.yml` | Filename for [Ansible-lint configuration](https://ansible-lint.readthedocs.io/en/latest/configuring.html#configuration-file) (ex: `.ansible-lint`, `.ansible-lint.yml`) | | **ANSIBLE_CONFIG_FILE** | `.ansible-lint.yml` | Filename for [Ansible-lint configuration](https://ansible-lint.readthedocs.io/en/latest/configuring.html#configuration-file) (ex: `.ansible-lint`, `.ansible-lint.yml`) |
| **ANSIBLE_DIRECTORY** | `/ansible` | Flag to set the root directory for Ansible file location(s), relative to `DEFAULT_WORKSPACE`. Set to `.` to use the top-level of the `DEFAULT_WORKSPACE`. | | **ANSIBLE_DIRECTORY** | `/ansible` | Flag to set the root directory for Ansible file location(s), relative to `DEFAULT_WORKSPACE`. Set to `.` to use the top-level of the `DEFAULT_WORKSPACE`. |
| **ANSIBLE_ROLES_PATH** | `${ANSIBLE_DIRECTORY}/roles` | Path to the Ansible roles directory. |
| **CSS_FILE_NAME** | `.stylelintrc.json` | Filename for [Stylelint configuration](https://github.com/stylelint/stylelint) (ex: `.stylelintrc.yml`, `.stylelintrc.yaml`) | | **CSS_FILE_NAME** | `.stylelintrc.json` | Filename for [Stylelint configuration](https://github.com/stylelint/stylelint) (ex: `.stylelintrc.yml`, `.stylelintrc.yaml`) |
| **DEFAULT_BRANCH** | `master` | The name of the repository default branch. | | **DEFAULT_BRANCH** | `master` | The name of the repository default branch. |
| **DEFAULT_WORKSPACE** | `/tmp/lint` | The location containing files to lint if you are running locally. | | **DEFAULT_WORKSPACE** | `/tmp/lint` | The location containing files to lint if you are running locally. |

View file

@ -243,6 +243,16 @@ function BuildFileList() {
done done
debug "---------------------------------------------" debug "---------------------------------------------"
#########################################
# Check if the Ansible directory exists #
#########################################
if [ -d "${ANSIBLE_DIRECTORY}" ]; then
debug "Adding ANSIBLE_DIRECTORY (${ANSIBLE_DIRECTORY}) to the list of files and directories to lint."
FILE_ARRAY_ANSIBLE+=("${ANSIBLE_DIRECTORY}")
else
debug "ANSIBLE_DIRECTORY (${ANSIBLE_DIRECTORY}) does NOT exist."
fi
################################################ ################################################
# Iterate through the array of all files found # # Iterate through the array of all files found #
################################################ ################################################
@ -498,15 +508,6 @@ function BuildFileList() {
################################ ################################
FILE_ARRAY_JSON+=("${FILE}") FILE_ARRAY_JSON+=("${FILE}")
############################
# Check if file is Ansible #
############################
if DetectAnsibleFile "${ANSIBLE_DIRECTORY}" "${FILE}"; then
################################
# Append the file to the array #
################################
FILE_ARRAY_ANSIBLE+=("${FILE}")
fi
############################ ############################
# Check if file is OpenAPI # # Check if file is OpenAPI #
############################ ############################
@ -793,19 +794,6 @@ function BuildFileList() {
################################ ################################
FILE_ARRAY_GITHUB_ACTIONS+=("${FILE}") FILE_ARRAY_GITHUB_ACTIONS+=("${FILE}")
fi fi
############################
# Check if file is Ansible #
############################
if [ -d "${ANSIBLE_DIRECTORY}" ]; then
if DetectAnsibleFile "${ANSIBLE_DIRECTORY}" "${FILE}"; then
################################
# Append the file to the array #
################################
FILE_ARRAY_ANSIBLE+=("${FILE}")
fi
else
debug "ANSIBLE_DIRECTORY (${ANSIBLE_DIRECTORY}) does NOT exist."
fi
##################################### #####################################
# Check if the file is CFN template # # Check if the file is CFN template #

View file

@ -7,27 +7,6 @@
################################################################################ ################################################################################
########################## FUNCTION CALLS BELOW ################################ ########################## FUNCTION CALLS BELOW ################################
################################################################################ ################################################################################
################################################################################
################################################################################
#### Function DetectAnsibleFile ################################################
DetectAnsibleFile() {
ANSIBLE_DIRECTORY="${1}"
FILE="${2}"
debug "Checking if ${FILE} is an Ansible file. Ansible directory: ${ANSIBLE_DIRECTORY}..."
if [[ ${FILE} == *"vault.yml" ]] || [[ ${FILE} == *"galaxy.yml" ]] || [[ ${FILE} == *"vault.yaml" ]] || [[ ${FILE} == *"galaxy.yaml" ]]; then
debug "${FILE} is a file that super-linter ignores. Ignoring it..."
return 1
elif [[ "$(dirname "${FILE}")" == *"${ANSIBLE_DIRECTORY}"* ]]; then
debug "${FILE} is an Ansible-related file."
return 0
else
debug "${FILE} is NOT an Ansible-related file."
return 1
fi
}
################################################################################
#### Function DetectActions #################################################### #### Function DetectActions ####################################################
DetectActions() { DetectActions() {
FILE="${1}" FILE="${1}"

View file

@ -155,10 +155,16 @@ function LintCodebase() {
fi fi
INDIVIDUAL_TEST_FOLDER="${FILE_TYPE,,}" # Folder for specific tests. By convention, it's the lowercased FILE_TYPE INDIVIDUAL_TEST_FOLDER="${FILE_TYPE,,}" # Folder for specific tests. By convention, it's the lowercased FILE_TYPE
TEST_CASE_DIRECTORY="${TEST_CASE_FOLDER}/${INDIVIDUAL_TEST_FOLDER}"
debug "File: ${FILE}, FILE_NAME: ${FILE_NAME}, DIR_NAME:${DIR_NAME}, FILE_STATUS: ${FILE_STATUS}, INDIVIDUAL_TEST_FOLDER: ${INDIVIDUAL_TEST_FOLDER}, TEST_CASE_DIRECTORY: ${TEST_CASE_DIRECTORY}"
debug "File: ${FILE}, FILE_NAME: ${FILE_NAME}, DIR_NAME:${DIR_NAME}, FILE_STATUS: ${FILE_STATUS}, INDIVIDUAL_TEST_FOLDER: ${INDIVIDUAL_TEST_FOLDER}" if [[ ${FILE_TYPE} != "ANSIBLE" ]]; then
# These linters expect files inside a directory, not a directory. So we add a trailing slash
TEST_CASE_DIRECTORY="${TEST_CASE_DIRECTORY}/"
debug "${FILE_TYPE} expects to lint individual files. Updated TEST_CASE_DIRECTORY to: ${TEST_CASE_DIRECTORY}"
fi
if [[ ${FILE} != *"${TEST_CASE_FOLDER}/${INDIVIDUAL_TEST_FOLDER}/"* ]] && [ "${TEST_CASE_RUN}" == "true" ]; then if [[ ${FILE} != *"${TEST_CASE_DIRECTORY}"* ]] && [ "${TEST_CASE_RUN}" == "true" ]; then
debug "Skipping ${FILE} because it's not in the test case directory for ${FILE_TYPE}..." debug "Skipping ${FILE} because it's not in the test case directory for ${FILE_TYPE}..."
continue continue
fi fi
@ -188,20 +194,10 @@ function LintCodebase() {
# Check for ansible # # Check for ansible #
##################### #####################
if [[ ${FILE_TYPE} == "ANSIBLE" ]]; then if [[ ${FILE_TYPE} == "ANSIBLE" ]]; then
#########################################
# Make sure we don't lint certain files #
#########################################
if [[ ${FILE} == *"vault.yml"* ]] || [[ ${FILE} == *"galaxy.yml"* ]]; then
# This is a file we don't look at
continue
fi
################################
# Lint the file with the rules #
################################
LINT_CMD=$( LINT_CMD=$(
cd "${ANSIBLE_DIRECTORY}" || exit debug "ANSIBLE_ROLES_PATH: ${ANSIBLE_ROLES_PATH}, LINTER_COMMAND:${LINTER_COMMAND}, FILE: ${FILE}"
${LINTER_COMMAND} "${FILE}" 2>&1 cd "${WORKSPACE_PATH}" || exit
ANSIBLE_ROLES_PATH=${ANSIBLE_ROLES_PATH} ${LINTER_COMMAND} "${FILE}" 2>&1
) )
#################################### ####################################
# Corner case for pwsh subshell # # Corner case for pwsh subshell #
@ -393,6 +389,6 @@ function LintCodebase() {
# We failed to find files and no tests were ran # # We failed to find files and no tests were ran #
################################################# #################################################
error "Failed to find any tests ran for the Linter:[${LINTER_NAME}]!" error "Failed to find any tests ran for the Linter:[${LINTER_NAME}]!"
fatal "Please validate logic or that tests exist!" fatal "Validate logic and that tests exist for linter: ${LINTER_NAME}"
fi fi
} }

View file

@ -847,6 +847,11 @@ export DEFAULT_TEST_CASE_ANSIBLE_DIRECTORY
############################ ############################
GetValidationInfo GetValidationInfo
# Now ANSIBLE_DIRECTORY is set
ANSIBLE_ROLES_PATH="${ANSIBLE_ROLES_PATH:-"${ANSIBLE_DIRECTORY}/roles"}"
debug "Setting ANSIBLE_ROLES_PATH to: ${ANSIBLE_ROLES_PATH}..."
export ANSIBLE_ROLES_PATH
################################# #################################
# Get the linter rules location # # Get the linter rules location #
################################# #################################
@ -868,7 +873,7 @@ GetStandardRules "typescript"
# Define linter commands # # Define linter commands #
########################## ##########################
declare -A LINTER_COMMANDS_ARRAY declare -A LINTER_COMMANDS_ARRAY
LINTER_COMMANDS_ARRAY['ANSIBLE']="ansible-lint -v -c ${ANSIBLE_LINTER_RULES}" LINTER_COMMANDS_ARRAY['ANSIBLE']="ansible-lint -vv -c ${ANSIBLE_LINTER_RULES}"
LINTER_COMMANDS_ARRAY['ARM']="Import-Module ${ARM_TTK_PSD1} ; \${config} = \$(Import-PowerShellDataFile -Path ${ARM_LINTER_RULES}) ; Test-AzTemplate @config -TemplatePath" LINTER_COMMANDS_ARRAY['ARM']="Import-Module ${ARM_TTK_PSD1} ; \${config} = \$(Import-PowerShellDataFile -Path ${ARM_LINTER_RULES}) ; Test-AzTemplate @config -TemplatePath"
LINTER_COMMANDS_ARRAY['BASH']="shellcheck --color --external-sources" LINTER_COMMANDS_ARRAY['BASH']="shellcheck --color --external-sources"
LINTER_COMMANDS_ARRAY['BASH_EXEC']="bash-exec" LINTER_COMMANDS_ARRAY['BASH_EXEC']="bash-exec"