LoginSignup
0
4

More than 3 years have passed since last update.

ansible-lint導入

Last updated at Posted at 2020-06-18

Ansible-e1500023954352.png

背景

ansible/ansible-lint

Ansible-playbookが多くなってきて管理が面倒になってきたのでメモ書き程度に記事を書いた。

インストール

pipで簡単に導入可能です

$ pip install ansible-lint

オプションに関しては--helpで見れます。

Usage: ansible-lint [options] [playbook.yml [playbook2 ...]]|roledirectory

Options:
  --version             show program's version number and exit
  -h, --help            show this help message and exit
  -L                    list all the rules
  -q                    quieter, although not silent output
  -p                    parseable output in the format of pep8
  --parseable-severity  parseable output including severity of rule
  -r RULESDIR           specify one or more rules directories using one or
                        more -r arguments. Any -r flags override the default
                        rules in ['/path/to/ansible-
                        lint/lib/ansiblelint/rules'], unless -R is also used.
  -R                    Use default rules ['/path/to/ansible-
                        lint/lib/ansiblelint/rules'] in addition to any extra
                        rules directories specified with -r. There is no need
                        to specify this if no -r flags are used
  -t TAGS               only check rules whose id/tags match these values
  -T                    list all the tags
  -v                    Increase verbosity level
  -x SKIP_LIST          only check rules whose id/tags do not match these
                        values
  --nocolor             disable colored output
  --force-color         Try force colored output (relying on ansible's code)
  --exclude=EXCLUDE_PATHS
                        path to directories or files to skip. This option is
                        repeatable.
  -c /path/to/file      Specify configuration file to use.  Defaults to
                          ".ansible-lint"

実行

$ ansible-lint 

細かい設定などは実行ディレクトリへ「.ansible-lint」を作成しそちらに記載していきます。

.ansible-lint
exclude_paths:
  - ./common_script/
  - ./ansible/bin/
  - ./ansible/module/
  - ./ansible/apps/
parseable: true
quiet: true
skip_list:
  - skip_this_tag # skipするタグを指定
  - skip_this_id # skipするidを指定
use_default_rules: true
verbosity: 1

特定のルールのみを実行したい場合は-tをつけることで実行が可能。

ルール

ansible-lint用のルールを自作することができます。
READMEにも作成の仕方が記載されていますがそのうち試してみようと思います。
以下はデフォルトのルールのほぼ直訳です。あまり見ないやつは省いています。(追記していく予定)

ANSIBLE0002: Trailing whitespace

行の末尾に空白がある

ANSIBLE0004: Git checkouts must contain explicit version

gitモジュールでversionを指定しなかったり、HEADに指定していると出る

ANSIBLE0005: Mercurial checkouts must contain explicit revision

ANSIBLE0004のhg版。revisionを指定しなかったり、defaultに指定したりすると警告が出る。

ANSIBLE000a6: Using command rather than module

git, curl, serviceその他、Ansibleモジュールが使えるところでcommand, shellを直接叩いてると警告が出る。

ANSIBLE0007: Using command rather than an argument to e.g. file

chmod, mkdir, rmその他、fileモジュールの引数でできることでcommand, shellを直接叩いていると警告が出る。

ANSIBLE0008: Deprecated sudo

become, become_userでなくsudo, 使っていると警告が出。

ANSIBLE0009: Octal file permissions must contain leading zero

fileモジュールなどでmodeの指定時に0666のような表記ではなく666のような表記をしていると警告が出る。

ANSIBLE0011: All tasks should be named

タスクに名前をつけないと警告

ANSIBLE0012: Commands should not change things if nothing needs doing

command, shellなどがwhen, createsなどで条件つき実行になっていない場合警告。

ANSIBLE0013: Use shell only when shell functionality is required

shell モジュールがらみのエラー 代替モジュールがあるばあいはそっちを使えとのこと

ANSIBLE0014: Environment variables don't work as part of command

command, shellのコマンド内で環境変数を使うと警告

ANSIBLE0015: Using bare variables is deprecated

{{ var }}形式以外の変数を使った場合に警告される。

ANSIBLE0016: Tasks that run when changed should likely be handlers

when: result.changedみたいなことやるなら普通にhandlers使えば変更あった場合のタスク実行できるからそうしよう、みたいなルール。

ANSIBLE0017: become_user requires become to work as expected

becomeなしでbecome_userしてると警告される。

ANSIBLE0018: Deprecated always_run

always_runを使っていると警告される。check_modeが代替とのこと。

GitHub Actionsとの連携

CIとの連携も簡単で以下のように定義しておくことでAnsibleのplaybook修正のたびにlinterが走る仕組みになる。

# https://github.com/ansible/ansible-lint |
- name: Lint Ansible
  shell: bash
  run: |
    find . -maxdepth 1 -name '*.yml' | xargs ansible-lint -v linux_init.yml
0
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
4