koh’s blog

Sys Admin who loves automation

Github Actionsでansible-lintを自動で実施する

For English

申し込んでいたGithub Actionsのbetaが使えるようになったのでAnsibleのsyntax checkとlintをさせてみようと思います。
なお本記事の内容は2019年9月7日時点のものであり、今後変更される可能性があります。

設定

公式ドキュメントを適宜参照しながら作業を進めていきます。

Automating your workflow with GitHub Actions - GitHub Help

とりあえず動かす

今回は私個人で使用しているmacbookのセットアップ用のPlaybookのリポジトリに設定していきます。
github.com

Actionsの項目が増えているのでこちらを選択します。
f:id:koh-sh:20190907163335p:plain

いろんなworkflowのテンプレートが選択できるので、今回はpython packageを使用します。
クリックするとworkflowの定義ファイルの修正画面に遷移するので下記のように修正します(ファイル名も修正しました)。

.github/workflows/ansiblelint.yml(クリックで展開)

name: Ansible lint

  on: [push]

  jobs:
   build:

      runs-on: macOS-latest
     strategy:
       max-parallel: 4
       matrix:
         python-version: [2.7, 3.5, 3.6, 3.7]

      steps:
     - uses: actions/checkout@v1
     - name: Set up Python ${{ matrix.python-version }}
       uses: actions/setup-python@v1
       with:
         python-version: ${{ matrix.python-version }}
     - name: Install dependencies
       run: |
         python -m pip install --upgrade pip
         pip install ansible ansible-lint
     - name: Lint playbook
       run: |
         ansible-playbook site.yml --syntax-check
         ansible-lint site.yml

macos用のplaybookなのでruns-onをmacOS-latestにしてみます。
流れとしては下記になります。

  • pipでansibleとansible-lintをインストール
  • ansible-playbookの--syntax-checkオプションをつけて実行
  • ansible-lint実行

これでcommitすると自動でテストが走ります。
再度Actionsを見てみるとテストの結果が見られます。
f:id:koh-sh:20190907164722p:plain

create github action workflow for ansible lint · koh-sh/macbook-playbook@3daeb98 · GitHub

python-versionが4つでmax-parallelを4にしているので4種類のテストが並行して実施されます。
リソースの上限についてはDocumentを確認ください。
https://help.github.com/en/articles/workflow-syntax-for-github-actions#usage-limits

各バージョンのbuildの各工程をクリックすると詳細が見られます。
今回はansible-lintでエラーが出ているのでテストに失敗しています。

テスト成功パターン

ansible-lintでエラーが出ているので下記commitでエラーを消します。
文末の余計なスペースを消すのと、shellモジュールの箇所は必要なので.ansible-lintを置いて除外してあげます。

github.com

masterブランチに直接pushしていますがちゃんと自動でテストは実施されます。
f:id:koh-sh:20190907170056p:plain

fix 201 and skip 305 · koh-sh/macbook-playbook@577519c · GitHub

テストはすべて問題なく完了しました。
f:id:koh-sh:20190907170209p:plain

ちなみにcommitログからもテスト成功失敗がわかるようになります。

プルリクエストも試す

プルリクエストを送る際の挙動も見てみます。
github.com

適当なPRを作成するとテストが実行されPRの画面から結果が確認できます。
f:id:koh-sh:20190907171027p:plain

ちなみにslackとgithubを連携させていて、PRの場合テストの結果もslackに通知されました。
Slack + GitHub

f:id:koh-sh:20190907171556p:plain masterへの直接pushだとテスト結果は表示されませんでした。
何か追加の設定が必要かもしれないですが確認できていないです。

複数バージョンのAnsibleでテストする

今の設定だとpythonの4つのバージョンで最新版のAnsibleのテストをしています。
これをpythonの2系、3系とAnsibleの2.7系、2.8系の組み合わせのテストをするように変更してみます。

workflowの定義ファイルを下記のように修正してみます。

.github/workflows/ansiblelint.yml(クリックで展開)

name: Ansible lint

on: [push]

jobs:
  build:

    runs-on: macOS-latest
    strategy:
      max-parallel: 4
      matrix:
        python-version: [2.7, 3.7]
        ansible-version: [2.7.13, 2.8.4]

    steps:
    - uses: actions/checkout@v1
    - name: Set up Python ${{ matrix.python-version }}
      uses: actions/setup-python@v1
      with:
        python-version: ${{ matrix.python-version }}
    - name: Install Ansible ${{ matrix.ansible-version }}
      run: |
        python -m pip install --upgrade pip
        pip install ansible-lint ansible==${{ matrix.ansible-version }}
    - name: Lint playbook
      run: |
        ansible-playbook site.yml --syntax-check
        ansible-lint site.yml

これでpushするとテスト内容が修正され、うまく目論見通りに動きました。
f:id:koh-sh:20190907172837p:plain

to test with multiple version of ansible · koh-sh/macbook-playbook@0ffab4e · GitHub

まとめ

無事にGithub Actionsでansible-lintを実行できました。
この記事にある内容は大体1時間程度ですべて試せました。かなりシンプルで直感的です。
テスト以外にもdeploy等もGithub Actionsでできるみたいなので積極的に試していきたいです。