Ansible-lint: Difference between revisions

From DWIKI
Tony (talk | contribs)
Tag: wikieditor
Tony (talk | contribs)
Tag: wikieditor
 
(7 intermediate revisions by the same user not shown)
Line 1: Line 1:
=FAQ=
=FAQ=
==Don't compare to empty string==
* Use `when: var | length > 0` instead of `when: var != ""`.
* Use `when: var | length == 0` instead of `when: var == ""`.


==Don't compare to literal True/False==
==Don't compare to literal True/False==
Line 6: Line 9:
use
use
  when: not (foo == 1 and bar == 0)  
  when: not (foo == 1 and bar == 0)  
==Error messages==
===WARNING  Overriding detected file kind 'yaml' with 'playbook' for given positional argument===
Create '''~/.ansible-lint''' with
---
kinds:
  - playbook: "**/*.{yml,yaml}"
===The field 'hosts' has an invalid value, which includes an undefined variable.. 'target' is undefined===
Add default value:
- hosts: "{{ target | default('dummy') }}"
(some docs suggest default('localhost') but that looks like asking for trouble)
==Use shell only when shell functionality is required==
Instead of
shell:
try
command:


==Commands should not change things if nothing needs doing==
==Commands should not change things if nothing needs doing==
Line 17: Line 38:


==Shells that use pipes should set the pipefail option==
==Shells that use pipes should set the pipefail option==
  shell: set -o pipefail && ...
  shell: |
  set -o pipefail
  your command
  args:
  executable: /bin/bash
 
==Ansible and APT==
===Links===
*[https://docs.ansible.com/ansible/latest/collections/ansible/builtin/apt_module.html ansible.builtin.apt module – Manages apt-packages]
 
===HOWTO===
 
====Override needrestart====
-name: install something
  ansible.builtin.apt:
    ...
 
  environment:
    - NEEDRESTART_MODE: automatically
 
===FAQ===
====Package installs should not use latest====
??
 
 
[[Category:Ansible]]

Latest revision as of 12:49, 12 May 2026

FAQ

Don't compare to empty string

  • Use `when: var | length > 0` instead of `when: var != ""`.
  • Use `when: var | length == 0` instead of `when: var == ""`.

Don't compare to literal True/False

Instead of

when: (foo == 1 and bar == 0) == false

use

when: not (foo == 1 and bar == 0) 

Error messages

WARNING Overriding detected file kind 'yaml' with 'playbook' for given positional argument

Create ~/.ansible-lint with

---
kinds:
  - playbook: "**/*.{yml,yaml}"

The field 'hosts' has an invalid value, which includes an undefined variable.. 'target' is undefined

Add default value:

- hosts: "Template:Target"

(some docs suggest default('localhost') but that looks like asking for trouble)

Use shell only when shell functionality is required

Instead of

shell:

try

command:

Commands should not change things if nothing needs doing

Sometimes fixed by adding

changed_when: false


Lines should be no longer than 160 chars

ignore for now?


Shells that use pipes should set the pipefail option

shell: |
  set -o pipefail
  your command
args:
  executable: /bin/bash

Ansible and APT

HOWTO

Override needrestart

-name: install something
 ansible.builtin.apt:
   ...
 environment:
   - NEEDRESTART_MODE: automatically

FAQ

Package installs should not use latest

??