DEV Community

koh-sh
koh-sh

Posted on • Originally published at koh-sh.hatenablog.com

Automatic ansible-lint with Github Actions

Now my account has access to Github Actions beta so I am trying to use it for syntax check and run ansible-lint for my Ansible playbook repository.
This article is based on the specification of Github Actions on 7th Sep 2019.

Configuration

I referred to the official doc for setting up.
https://help.github.com/en/categories/automating-your-workflow-with-github-actions

Until the first run

This time I use my repository which has Ansible Playbook to set up my MacBook.

GitHub logo koh-sh / macbook-playbook

ansible playbook to setup macbook

macbook-playbook

Ansible playbook to setup macbook




Clicking Actions tab which is newly added.
Alt Text

You can use many templates of workflow provided by Github and I am using python package.
After clicking the template, you can modify the workflow template for your repo.
I edited the file like below. (I also changed the name of the file too)

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
Enter fullscreen mode Exit fullscreen mode

The Playbook is for macOS, so I changed runs-on to macOS-latest.

Below tasks are supposed to run.

  • Install ansible and ansible-lint with pip
  • run ansible-playbook with --syntax-check option
  • run ansible-lint

After committing this file, tests were run automatically.

You can see the result at Actions tab.
Alt Text

https://github.com/koh-sh/macbook-playbook/commit/3daeb98a056981335938e74c530ebb5f6ae1f6e3/checks

The number of python-version is 4 and max-parallel is 4 too so 4 tests were run concurrently.
You can refer to the official doc about the limit of resources.
https://help.github.com/en/articles/workflow-syntax-for-github-actions#usage-limits

You can click each version of builds for details.
This time ansible-lint threw some errors so the status is failed.

Until test success

ansible-lint threw errors so fixing with this commit.
Removing trailing space and omit the error about shell modules with .ansible-lint file as I needed them.

https://github.com/koh-sh/macbook-playbook/commit/577519c6213c4a1e7a3c047808ef146ca2d67f86

I pushed to the master branch and the tests run automatically.

Alt Text

https://github.com/koh-sh/macbook-playbook/commit/577519c6213c4a1e7a3c047808ef146ca2d67f86/checks

Tests are completed without problems.
Alt Text

Also, you can see the results of tests for each commit with symbols.

Trying Pull Requests

How about pull requests?
Let's see how they work.

to trigger github actions test #1

This is test PR to trigger Github actions test

By opening a pull request, tests run automatically and the results are available at PR summary.
Alt Text

And this repository is integrated into my slack workspace and the result was notified too.
https://slack.github.com

Alt Text
But when I pushed to master, the test result was not notified.
Another setting might be necessary but I haven't checked yet.

Testing with multiple version of Ansible

Currently, these tests are run with the latest version of Ansible and 4 versions of python.
Let's change it to 2.7.x, 2.8.x of Ansible and 2.x and 3.x of python matrix.

I modified the workflow file as below.

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

Enter fullscreen mode Exit fullscreen mode

After a push, the test setting was updated and run as I intended.
Alt Text

https://github.com/koh-sh/macbook-playbook/commit/0ffab4e73391ab8f3d39d5c149307bab9c06714f/checks

Conclusion

With Github Actions, I can ansible-lint automatically for each commit.
All of the settings in this article took only 1 hour since these are very simple and easy to use.
Also, Github Actions are available not only tests but also deploys too so I will keep trying them out.

Top comments (0)