てくなべ (tekunabe)

ansible / network automation / 学習メモ

[Ansible] ansible-lint でファイル名と無視したいルールを指定する .ansible-lint-ignore

はじめに

ansible-lint 6.13.0 で、無視するルールを定義するファイル が導入されました。

定義ファイル名は .ansible-lint-ignore または .config/ansible-lint-ignore.txt です(個人的には後者が好み)。ファイル名の先頭の . 有無や、拡張子の有無が異なります。以降この記事では、.ansible-lint-ignore を利用します。

フォーマットは、Playbookなどの対象ファイル名と、無視したいルール名を書くだけの簡単なものです。

例:

hello.yml name[play]
hello.yml fqcn[action-core]

少し試してみます。

  • 環境
    • ansible-lint 6.14.4

おためし

Playbook

ansible-lint を実行する対象の Playbook は以下のものにします。

---
- hosts: localhost
  gather_facts: false

  tasks:
    - name: Hello
      debug:
        msg: Hello Ansible Lint!

まずデフォルトのまま ansible-lint

まず、ansible-lint の設定ファイル(.ansible-lint など)も、今回の.ansible-lint-ignore もない状態で実行します。

$ ansible-lint hello.yml                 
WARNING  Listing 2 violation(s) that are fatal
name[play]: All plays should be named.
hello.yml:2

fqcn[action-core]: Use FQCN for builtin module actions (debug).
hello.yml:6 Use `ansible.builtin.debug` or `ansible.legacy.debug` instead.

Read documentation for instructions on how to ignore specific rule violations.

                 Rule Violation Summary                  
 count tag               profile    rule associated tags 
     1 name[play]        basic      idiom                
     1 fqcn[action-core] production formatting           

Failed after min profile: 2 failure(s), 0 warning(s) on 1 files.
$ echo $?   # 終了コードを確認
2

Play には名前(name)が必要でというルールと、FQCN で書いてねというルールを満たしてないことが検出されました。

今回はこの2つを無視する設定をすることにします。なお、わかりやすい例を取り上げただけであり、無視することを推奨する意図ではありません。

.ansible-lint-ignore を定義して再実行

ansible-lint を実行するパスと同じディレクトリの .ansible-lint-ignore に、以下の無視する定義をします。フォーマットは公式ドキュメントのページ)に従います。

# おためし(これはコメント)
hello.yml name[play]
hello.yml fqcn[action-core]

それでは再度 ansible-lint を実行します。

$ ansible-lint hello.yml         
WARNING  Listing 2 violation(s) marked as ignored, likely already known
name[play]: All plays should be named. (warning) # ignored
hello.yml:2

fqcn[action-core]: Use FQCN for builtin module actions (debug). (warning) # ignored
hello.yml:11 Use `ansible.builtin.debug` or `ansible.legacy.debug` instead.


Passed with production profile: 0 failure(s), 2 warning(s) on 1 files.
$ echo $?   # 終了コードを確認
0

思ったよりは色々表示されました。2件検出されたこと自体は変わりないですが、以下の点が異なります。

  • (warning) # ignored がついた
  • Passed になった
  • failure ではなく warning とてカウントされた
  • 終了コードが 0 になった

現状の Playbook から .ansible-lint-ignore を生成する

現状の Playbook に ansible-lint を実行して、検出したエラーを元にして .ansible-lint-ignore を生成する機能もあります。ansible-lint コマンドに --generate-ignore オプションを付けるだけのようです。

生成も試します。先ほど手動で作成した .ansible-lint-ignore は一旦削除して試します。

$ ansible-lint hello.yml --generate-ignore
WARNING  Listing 2 violation(s) that are fatal
name[play]: All plays should be named.
hello.yml:2

fqcn[action-core]: Use FQCN for builtin module actions (debug).
hello.yml:11 Use `ansible.builtin.debug` or `ansible.legacy.debug` instead.

Writing ignore file to .ansible-lint-ignore

                 Rule Violation Summary                  
 count tag               profile    rule associated tags 
     1 name[play]        basic      idiom                
     1 fqcn[action-core] production formatting           

Failed after min profile: 2 failure(s), 0 warning(s) on 1 files.

デフォルトで実行したので、例の2件が検出されました。加えて、以下の内容の .ansible-lint-ignore が生成されました。

# This file contains ignores rule violations for ansible-lint
hello.yml fqcn[action-core]
hello.yml name[play]

この状態で再度 ansible-lint を実行すると、無視する設定が反映されました。

また、もともと .ansible-lint-ignore がある状態で、生成させると上書きされました。

なお、今回は hello.yml という Playbook 1つを指定したので、生成される .ansible-lint-ignore も 1つの Playbook 分のみですが、Playbook を指定せず ansible-lint --generate-ignore のよう実行すると、複数 Playbook 分が生成されます。

おわりに

ファイルに関わらずルールを無視したい場合は .ansible-lint (または .config/ansible-lint.yml)内のskip_list` に指定すればよいですが、ファイルごとに無視したいルールを指定したいときは便利な機能だなと思います。

また、もし # noqa ルール で個別に無視する指定をしていて、 .ansible-lint-ignore でまとめられる内容であれば、まとめたほうがスッキリするかもしれません。