Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- Exempt shell pipelines with truthy task-level `ignore_errors` from
`risky-shell-pipe`, preserving independent failure-handling checks.
- Consolidate dependency license notices, retain upstream attribution under docs,
and exclude compiled libraries accidentally matched as license files.
- Resolve role-relative task includes with Ansible role and playbook fallbacks,
Expand Down
14 changes: 13 additions & 1 deletion src/extra_rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,19 @@ pub fn task<'a>(task: &'a Node, action: &Action<'a>, emit: &mut impl FnMut(&str,
let powershell = action
.argument("executable")
.is_some_and(|value| value.contains("pwsh") || value.contains("powershell"));
if !powershell && has_pipeline(&command) && !has_pipefail {
// The pinned rule tests task-level Python truthiness, including
// nonempty template strings; it does not evaluate the condition.
let ignores_errors = task
.get("ignore_errors")
.is_some_and(|node| match node.to_json() {
serde_json::Value::Null => false,
serde_json::Value::Bool(value) => value,
serde_json::Value::String(value) => !value.is_empty(),
serde_json::Value::Number(value) => value.as_f64() != Some(0.0),
serde_json::Value::Array(value) => !value.is_empty(),
serde_json::Value::Object(value) => !value.is_empty(),
});
if !powershell && !ignores_errors && has_pipeline(&command) && !has_pipefail {
emit(
"risky-shell-pipe",
"Set pipefail so an earlier pipeline failure is not hidden",
Expand Down
38 changes: 38 additions & 0 deletions tests/conditional_checks.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// SPDX-License-Identifier: GPL-3.0-or-later
use serde_json::Value;
use std::{fs, process::Command};

#[test]
fn ignored_shell_pipelines_and_implicit_conditionals_keep_scoped_checks() {
let cases: Value =
serde_json::from_str(include_str!("fixtures/conditional_checks.json")).unwrap();
for case in cases.as_array().unwrap() {
let directory = tempfile::tempdir().unwrap();
let file = case["file"].as_str().unwrap();
let path = directory.path().join(file);
fs::create_dir_all(path.parent().unwrap()).unwrap();
fs::write(path, case["source"].as_str().unwrap()).unwrap();
fs::write(
directory.path().join(".ansible-lint"),
case["config"].as_str().unwrap(),
)
.unwrap();
let output = Command::new(env!("CARGO_BIN_EXE_oxible"))
.current_dir(directory.path())
.env("PATH", "")
.args([file, "--offline", "--no-cache", "-f", "json"])
.output()
.unwrap();
let issues: Value = serde_json::from_slice(&output.stdout).unwrap();
// Retain implicit task-file spacing: pinned ansible-lint checks the
// singular kind "task" instead of its actual "tasks" classification.
// Playbook checks confirm these are useful, valid formatting findings.
assert_eq!(issues, case["expected"]["issues"], "{}", case["case"]);
assert_eq!(
output.status.code().map(i64::from),
case["expected"]["exit"].as_i64(),
"{}",
case["case"]
);
}
}
291 changes: 291 additions & 0 deletions tests/fixtures/conditional_checks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,291 @@
[
{
"case": "pipe-true",
"file": "tasks/main.yml",
"source": "---\n- name: Check for a matching entry\n ansible.builtin.shell: printf '%s\\n' example | grep example\n ignore_errors: true\n changed_when: false\n",
"config": "offline: true\n",
"expected": {
"exit": 2,
"issues": [
{
"type": "issue",
"check_name": "ignore-errors",
"categories": [
"unpredictability"
],
"url": "https://docs.ansible.com/projects/lint/rules/ignore-errors/",
"severity": "major",
"description": "Use failed_when and specify error conditions instead of using ignore_errors.",
"fingerprint": "404f3e389d3edc8dc3b8c5fa40e1cafd9b6b79fa1006e8e2a8154970fc1f14f1",
"location": {
"path": "tasks/main.yml",
"lines": {
"begin": 2
}
},
"content": {
"body": "Task/Handler: Check for a matching entry"
}
}
]
}
},
{
"case": "pipe-false",
"file": "tasks/main.yml",
"source": "---\n- name: Check for a matching entry\n ansible.builtin.shell: printf '%s\\n' example | grep example\n ignore_errors: false\n changed_when: false\n",
"config": "offline: true\n",
"expected": {
"exit": 2,
"issues": [
{
"type": "issue",
"check_name": "risky-shell-pipe",
"categories": [
"command-shell"
],
"url": "https://docs.ansible.com/projects/lint/rules/risky-shell-pipe/",
"severity": "major",
"description": "Shells that use pipes should set the pipefail option.",
"fingerprint": "ee7cba85ddf560b3ee5ac65fa53e264a18350f22ea071bc96fb472f0866683ce",
"location": {
"path": "tasks/main.yml",
"lines": {
"begin": 2
}
},
"content": {
"body": "Task/Handler: Check for a matching entry"
}
}
]
}
},
{
"case": "pipe-omitted",
"file": "tasks/main.yml",
"source": "---\n- name: Check for a matching entry\n ansible.builtin.shell: printf '%s\\n' example | grep example\n changed_when: false\n",
"config": "offline: true\n",
"expected": {
"exit": 2,
"issues": [
{
"type": "issue",
"check_name": "risky-shell-pipe",
"categories": [
"command-shell"
],
"url": "https://docs.ansible.com/projects/lint/rules/risky-shell-pipe/",
"severity": "major",
"description": "Shells that use pipes should set the pipefail option.",
"fingerprint": "ee7cba85ddf560b3ee5ac65fa53e264a18350f22ea071bc96fb472f0866683ce",
"location": {
"path": "tasks/main.yml",
"lines": {
"begin": 2
}
},
"content": {
"body": "Task/Handler: Check for a matching entry"
}
}
]
}
},
{
"case": "pipe-check-mode",
"file": "tasks/main.yml",
"source": "---\n- name: Check for a matching entry\n ansible.builtin.shell: printf '%s\\n' example | grep example\n ignore_errors: \"{{ ansible_check_mode }}\"\n changed_when: false\n",
"config": "offline: true\n",
"expected": {
"exit": 0,
"issues": []
}
},
{
"case": "pipe-registered",
"file": "tasks/main.yml",
"source": "---\n- name: Check for a matching entry\n ansible.builtin.shell: printf '%s\\n' example | grep example\n ignore_errors: true\n register: result\n changed_when: false\n",
"config": "offline: true\n",
"expected": {
"exit": 0,
"issues": []
}
},
{
"case": "implicit-tasks-gt-False",
"file": "tasks/main.yml",
"source": "---\n- name: Show a message conditionally\n ansible.builtin.debug:\n msg: hello\n when: ansible_distribution_major_version|int > 13\n",
"config": "offline: true\n",
"expected": {
"exit": 0,
"issues": [
{
"categories": [
"formatting"
],
"check_name": "jinja[spacing]",
"content": {
"body": "Jinja2 template rewrite recommendation: `ansible_distribution_major_version | int > 13`."
},
"description": "Jinja2 spacing could be improved: ansible_distribution_major_version|int > 13 -> ansible_distribution_major_version | int > 13",
"fingerprint": "e6e716ebd95046c182ff363a1496e84dbe6c26d38265218407c49fdd51ee6e4e",
"location": {
"path": "tasks/main.yml",
"positions": {
"begin": {
"column": 9,
"line": 5
}
}
},
"severity": "minor",
"type": "issue",
"url": "https://docs.ansible.com/projects/lint/rules/jinja/"
}
]
}
},
{
"case": "implicit-tasks-gt-True",
"file": "tasks/main.yml",
"source": "---\n- name: Show a message conditionally\n ansible.builtin.debug:\n msg: hello\n when: ansible_distribution_major_version | int > 13\n",
"config": "offline: true\n",
"expected": {
"exit": 0,
"issues": []
}
},
{
"case": "implicit-playbook-gt-False",
"file": "site.yml",
"source": "---\n- name: Test conditionals\n hosts: localhost\n gather_facts: false\n tasks:\n - name: Show a message conditionally\n ansible.builtin.debug:\n msg: hello\n when: ansible_distribution_major_version|int > 13\n",
"config": "offline: true\n",
"expected": {
"exit": 0,
"issues": [
{
"type": "issue",
"check_name": "jinja[spacing]",
"categories": [
"formatting"
],
"url": "https://docs.ansible.com/projects/lint/rules/jinja/",
"severity": "minor",
"description": "Jinja2 spacing could be improved: ansible_distribution_major_version|int > 13 -> ansible_distribution_major_version | int > 13",
"fingerprint": "7cdf934691bce1eb1228c8cbe9fe03c72538f5e2d1cf5a226d345840562b3d2c",
"location": {
"path": "site.yml",
"positions": {
"begin": {
"line": 9,
"column": 13
}
}
},
"content": {
"body": "Jinja2 template rewrite recommendation: `ansible_distribution_major_version | int > 13`."
}
}
]
}
},
{
"case": "implicit-playbook-gt-True",
"file": "site.yml",
"source": "---\n- name: Test conditionals\n hosts: localhost\n gather_facts: false\n tasks:\n - name: Show a message conditionally\n ansible.builtin.debug:\n msg: hello\n when: ansible_distribution_major_version | int > 13\n",
"config": "offline: true\n",
"expected": {
"exit": 0,
"issues": []
}
},
{
"case": "implicit-tasks-ge-False",
"file": "tasks/main.yml",
"source": "---\n- name: Show a message conditionally\n ansible.builtin.debug:\n msg: hello\n when: ansible_distribution_major_version|int >= 13\n",
"config": "offline: true\n",
"expected": {
"exit": 0,
"issues": [
{
"categories": [
"formatting"
],
"check_name": "jinja[spacing]",
"content": {
"body": "Jinja2 template rewrite recommendation: `ansible_distribution_major_version | int >= 13`."
},
"description": "Jinja2 spacing could be improved: ansible_distribution_major_version|int >= 13 -> ansible_distribution_major_version | int >= 13",
"fingerprint": "c6b957d9f56d62fd7193c925219acb1f3f5ebb3d4745862ebc3fb6a61522e4c5",
"location": {
"path": "tasks/main.yml",
"positions": {
"begin": {
"column": 9,
"line": 5
}
}
},
"severity": "minor",
"type": "issue",
"url": "https://docs.ansible.com/projects/lint/rules/jinja/"
}
]
}
},
{
"case": "implicit-tasks-ge-True",
"file": "tasks/main.yml",
"source": "---\n- name: Show a message conditionally\n ansible.builtin.debug:\n msg: hello\n when: ansible_distribution_major_version | int >= 13\n",
"config": "offline: true\n",
"expected": {
"exit": 0,
"issues": []
}
},
{
"case": "implicit-playbook-ge-False",
"file": "site.yml",
"source": "---\n- name: Test conditionals\n hosts: localhost\n gather_facts: false\n tasks:\n - name: Show a message conditionally\n ansible.builtin.debug:\n msg: hello\n when: ansible_distribution_major_version|int >= 13\n",
"config": "offline: true\n",
"expected": {
"exit": 0,
"issues": [
{
"type": "issue",
"check_name": "jinja[spacing]",
"categories": [
"formatting"
],
"url": "https://docs.ansible.com/projects/lint/rules/jinja/",
"severity": "minor",
"description": "Jinja2 spacing could be improved: ansible_distribution_major_version|int >= 13 -> ansible_distribution_major_version | int >= 13",
"fingerprint": "644cb48fc09ed8ba0310093bb5e0545054855e2905f00a1cae4e7c83ff7a232f",
"location": {
"path": "site.yml",
"positions": {
"begin": {
"line": 9,
"column": 13
}
}
},
"content": {
"body": "Jinja2 template rewrite recommendation: `ansible_distribution_major_version | int >= 13`."
}
}
]
}
},
{
"case": "implicit-playbook-ge-True",
"file": "site.yml",
"source": "---\n- name: Test conditionals\n hosts: localhost\n gather_facts: false\n tasks:\n - name: Show a message conditionally\n ansible.builtin.debug:\n msg: hello\n when: ansible_distribution_major_version | int >= 13\n",
"config": "offline: true\n",
"expected": {
"exit": 0,
"issues": []
}
}
]
Loading