LoginSignup
1
3

More than 1 year has passed since last update.

ansible-lintを導入して構文の標準化を実施する

Posted at

はじめに

AnsibleAutomationPlatform2.2が登場しましたが、今回のリリースの中でansible-lintがtechPreviewで導入されています(ansible-navigatorのサブコマンドとして)。ansibleのplaybookはスクリプト言語よろしく自由度高く記述できるため、ある程度の人数で作り始めると微妙な構文の違いで散らかってしまい後々取りまとめて管理する頃にはなんじゃこりゃ!となりがちだと思います。

ansibleで使えるlinterにはyamllintとansible-lintが選択肢としてあります。今回AAP2.2に導入されたことでansible-lintはもっと需要が増えると思うので今回はansible-lintを触ってみます

What's new in Red Hat Ansible Automation Platform 2.2
AnsibleLintDocumentation
yamllint documentation

目次

  1. インストール
  2. チェックの実施
  3. ルールの変更
  4. 参考文献

インストール

pipから入れるのが簡単です

pip install ansible-lint

macの場合、brewからでも入れれます(自分はこれ)

brew install ansible-lint

インストール後にバージョンを確認します

@MacBook-Pro Downloads % ansible-lint --version
WARNING: PATH altered to include /opt/homebrew/Cellar/ansible-lint/6.3.0/libexec/bin
ansible-lint 6.3.0 using ansible 2.13.0

チェックの実施

お試しのために適当なplaybookを用意して、

sample.yml
---
- hosts: all
  gather_facts: no
  become: true
  tasks:
    - name: Copy Local
      copy:
        src={{ playbook_dir }}/tmp/test.txt
        dest=/tmp
        owner=root
        group=root
        mode=0700
      tags: upload

    - name: Apache Install
      yum: 
        name: httpd
        state: latest

早速ansible-lintを実行します。

MacBook-Pro Downloads % ansible-lint sample.yml

色々反応してメッセージが出ています

WARNING: PATH altered to include /opt/homebrew/Cellar/ansible-lint/6.3.0/libexec/bin
WARNING  Overriding detected file kind 'yaml' with 'playbook' for given positional argument: sample.yml
WARNING  Listing 6 violation(s) that are fatal
yaml: truthy value should be one of [false, true] (yaml[truthy])
sample.yml:3

fqcn-builtins: Use FQCN for builtin actions.
sample.yml:6 Task/Handler: Copy Local

yaml: trailing spaces (yaml[trailing-spaces])
sample.yml:13

fqcn-builtins: Use FQCN for builtin actions.
sample.yml:15 Task/Handler: Apache Install

package-latest: Package installs should not use latest.
sample.yml:15 Task/Handler: Apache Install

yaml: trailing spaces (yaml[trailing-spaces])
sample.yml:16

You can skip specific rules or tags by adding them to your configuration file:
# .config/ansible-lint.yml
warn_list:  # or 'skip_list' to silence them completely
  - fqcn-builtins  # Use FQCN for builtin actions.
  - package-latest  # Package installs should not use latest.
  - yaml  # Violations reported by yamllint.

Finished with 6 failure(s), 0 warning(s) on 1 files.

こんな感じで指摘が上がりました。今出ているのはとりあえず

・ture/falseで書きなさい
・FQCNを使って修飾子を明記しなさい
・いらないスペースが入っている

あたりで色々調べながら直したのですが、そこは省略して以下修正版です。

sample.yml
---
- hosts: all
  gather_facts: false
  become: true

  tasks:
    - name: Copy Local
      ansible.builtin.copy:
        src={{ playbook_dir }}/tmp/test.txt
        dest=/tmp
        owner=root
        group=root
        mode=0700
      tags: upload

    - name: Apache Install
      ansible.builtin.yum:
        name: httpd

再び実行してみます。

MacBook-Pro Downloads % ansible-lint sample.yml
WARNING: PATH altered to include /opt/homebrew/Cellar/ansible-lint/6.3.0/libexec/bin
WARNING  Overriding detected file kind 'yaml' with 'playbook' for given positional argument: site_org.yml

警告は残っているものの、記述の問題は無くなりました。

使い方としてはCIに組み込むまれるのが一般的だと思いますが、まずは引っかからないようにローカルでも実行できるようにしておくと便利だと思います。ただ、このFQCNは既存のplaybookのチェックにはちょっといけてない気がします。が、ansible-lint6からはデフォルトだそうです。

ade fqcn-builtins rule implicit

なので、ひとまずルール変更でこれを無視するように設定したいと思います。

ルールの変更

ルールの変更は.ansible-lintを作成してカスタムルールを記述するだけです。
(自分で作成したルールの追加も可能です)

.ansible-lint
skip_list:
  - 'fqcn-builtins'
sample.yml
---
- hosts: all
  gather_facts: false
  become: true

  tasks:
    - name: Copy Local
      copy:
        src={{ playbook_dir }}/tmp/test.txt
        dest=/tmp
        owner=root
        group=root
        mode=0700
      tags: upload

    - name: Apache Install
      yum:
        name: httpd
MacBook-Pro Downloads % ansible-lint sample.yml
WARNING: PATH altered to include /opt/homebrew/Cellar/ansible-lint/6.3.0/libexec/bin
WARNING  Overriding detected file kind 'yaml' with 'playbook' for given positional argument: site_org.yml

(FQCNはいずれ対応するとして)これで、問題なく実行できるようになりました。
CI管理者側でカスタムしたルールを配布するだけでいいので非常に便利です。

ルールが変えれるので導入時の負担は調整しやすく、導入も簡単なため標準化を目指して持ち込みやすいツールだと思いました。

参考文献

1
3
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
1
3