@kzm0211 いなむーパイセンにansibleのレビューにて
「空白を入れてください!!!」
と何回も言われないようにするためにはCIで検知して自分で直したいなあと。
今回は個人のリポジトリにてCircleCIでansible-lintを使って、
文末に空白があったら検知するという方法をいなむーさんに伝授してもらいやした!
結構面白いのでブログします。
■CircleCI Setting
とりあえずCircleCIは同時にCIしなければ無料なので、(1コンテナのみ)
ギッハブでログインしてプロジェクトを追加しましょう。
今回はansible_blog.adachin.meを追加します。
ansible-limitはPythonなのでSubmit押しやす。
ここでまだStart buildingは押さないように!
■make branch
・mkdir .circleci
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
[~/git/RVIRUS0817/ansible_blog.adachin.me] $ ls -la total 40 drwxr-xr-x 11 adachin staff 352 6 20 20:42 ./ drwxr-xr-x 4 adachin staff 128 6 20 20:14 ../ drwxr-xr-x 5 adachin staff 160 6 20 20:35 .circleci/ drwxr-xr-x 13 adachin staff 416 6 20 21:04 .git/ -rw-r--r-- 1 adachin staff 8 6 20 20:14 .gitignore -rw-r--r-- 1 adachin staff 647 6 20 20:14 README.md -rw-r--r-- 1 adachin staff 97 6 20 20:14 ansible.cnf -rwxr-xr-x 1 adachin staff 228 6 20 20:42 blog.adachin.me.yml* drwxr-xr-x 3 adachin staff 96 6 20 20:14 group_vars/ -rw-r--r-- 1 adachin staff 44 6 20 20:14 hosts drwxr-xr-x 13 adachin staff 416 6 20 20:14 roles/ |
.circleciディレクトリを作ったら下記のようにconfigファイルを作りましょう。
(今更ですがCircleCIはdockerでできています)
・config.yml
1 2 3 4 5 6 7 8 9 10 11 12 |
$ cat config.yml --- version: 2 jobs: build: docker: - image: python:2.7 working_directory: ~/git/RVIRUS0817 steps: - checkout - run: pip install -r ./.circleci/requirements.txt - run: ansible-lint blog.adachin.me.yml -c .circleci/.ansible-lint |
・requirements.txt
1 |
ansible-lint |
・.ansible-lint
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
parseable: true quiet: false use_default_rules: true skip_list: - ANSIBLE0004 - ANSIBLE0005 - ANSIBLE0006 - ANSIBLE0007 - ANSIBLE0008 - ANSIBLE0009 - ANSIBLE0010 - ANSIBLE0011 - ANSIBLE0012 - ANSIBLE0013 - ANSIBLE0014 - ANSIBLE0015 - ANSIBLE0016 - ANSIBLE0017 - ANSIBLE0018 verbosity: 1 |
何やってるのかわからんと思うので、上記でやっていることを実際にローカルで試してみます。
・install Mac ansible-lint
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
$ pip install -r requirements.txt Collecting ansible-lint (from -r requirements.txt (line 1)) Downloading https://files.pythonhosted.org/packages/ac/37/5680eb8e4325120457bf177d7c3fe81a29eeeeb37f8fe0fc3131cd9ff721/ansible-lint-3.4.23.tar.gz Collecting ansible (from ansible-lint->-r requirements.txt (line 1)) Downloading https://files.pythonhosted.org/packages/93/53/925881db1049134679908698115752ad0b0fe95c82b3055a632476cd3687/ansible-2.5.5.tar.gz (10.2MB) 100% |████████████████████████████████| 10.2MB 3.6MB/s Collecting pyyaml (from ansible-lint->-r requirements.txt (line 1)) Downloading https://files.pythonhosted.org/packages/4a/85/db5a2df477072b2902b0eb892feb37d88ac635d36245a72a6a69b23b383a/PyYAML-3.12.tar.gz (253kB) 100% |████████████████████████████████| 256kB 15.9MB/s Collecting six (from ansible-lint->-r requirements.txt (line 1)) Downloading https://files.pythonhosted.org/packages/67/4b/141a581104b1f6397bfa78ac9d43d8ad29a7ca43ea90a2d863fe3056e86a/six-1.11.0-py2.py3-none-any.whl Collecting jinja2 (from ansible->ansible-lint->-r requirements.txt (line 1)) Downloading https://files.pythonhosted.org/packages/7f/ff/ae64bacdfc95f27a016a7bed8e8686763ba4d277a78ca76f32659220a731/Jinja2-2.10-py2.py3-none-any.whl (126kB) 100% |████████████████████████████████| 133kB 10.7MB/s Collecting paramiko (from ansible->ansible-lint->-r requirements.txt (line 1)) Downloading https://files.pythonhosted.org/packages/3e/db/cb7b6656e0e7387637ce850689084dc0b94b44df31cc52e5fc5c2c4fd2c1/paramiko-2.4.1-py2.py3-none-any.whl (194kB) 100% |████████████████████████████████| 194kB 20.6MB/s Collecting cryptography (from ansible->ansible-lint->-r requirements.txt (line 1)) Downloading https://files.pythonhosted.org/packages/58/c1/23bea66007d4be75ce02056fac665f9a207535e89fb3c7931420fa4a5f57/cryptography-2.2.2-cp27-cp27m-macosx_10_6_intel.whl (1.5MB) 100% |████████████████████████████████| 1.5MB 9.3MB/s Requirement already satisfied: setuptools in /usr/local/lib/python2.7/site-packages (from ansible->ansible-lint->-r requirements.txt (line 1)) (39.2.0) Collecting MarkupSafe>=0.23 (from jinja2->ansible->ansible-lint->-r requirements.txt (line 1)) Downloading https://files.pythonhosted.org/packages/4d/de/32d741db316d8fdb7680822dd37001ef7a448255de9699ab4bfcbdf4172b/MarkupSafe-1.0.tar.gz Collecting pyasn1>=0.1.7 (from paramiko->ansible->ansible-lint->-r requirements.txt (line 1)) Downloading https://files.pythonhosted.org/packages/a0/70/2c27740f08e477499ce19eefe05dbcae6f19fdc49e9e82ce4768be0643b9/pyasn1-0.4.3-py2.py3-none-any.whl (72kB) 100% |████████████████████████████████| 81kB 17.2MB/s Collecting bcrypt>=3.1.3 (from paramiko->ansible->ansible-lint->-r requirements.txt (line 1)) Downloading https://files.pythonhosted.org/packages/a1/9c/c89411a505dca5ae822a28c6de6946583ff8a1d5d9190292f301d28dcf85/bcrypt-3.1.4-cp27-cp27m-macosx_10_6_intel.whl (51kB) 100% |████████████████████████████████| 61kB 10.0MB/s Collecting pynacl>=1.0.1 (from paramiko->ansible->ansible-lint->-r requirements.txt (line 1)) Downloading https://files.pythonhosted.org/packages/7b/b2/8588fb7665c8b4f6b46921ead952dcd28ec15ed66d686b4ae69d6f0aed3c/PyNaCl-1.2.1-cp27-cp27m-macosx_10_6_intel.whl (243kB) 100% |████████████████████████████████| 245kB 20.6MB/s Collecting cffi>=1.7; platform_python_implementation != "PyPy" (from cryptography->ansible->ansible-lint->-r requirements.txt (line 1)) Downloading https://files.pythonhosted.org/packages/7e/4a/b647e46faaa2dcfb16069b6aad2d8509982fd63710a325b8ad7db80f18be/cffi-1.11.5-cp27-cp27m-macosx_10_6_intel.whl (238kB) 100% |████████████████████████████████| 245kB 11.4MB/s Collecting enum34; python_version < "3" (from cryptography->ansible->ansible-lint->-r requirements.txt (line 1)) Downloading https://files.pythonhosted.org/packages/c5/db/e56e6b4bbac7c4a06de1c50de6fe1ef3810018ae11732a50f15f62c7d050/enum34-1.1.6-py2-none-any.whl Collecting asn1crypto>=0.21.0 (from cryptography->ansible->ansible-lint->-r requirements.txt (line 1)) Downloading https://files.pythonhosted.org/packages/ea/cd/35485615f45f30a510576f1a56d1e0a7ad7bd8ab5ed7cdc600ef7cd06222/asn1crypto-0.24.0-py2.py3-none-any.whl (101kB) 100% |████████████████████████████████| 102kB 21.6MB/s Collecting idna>=2.1 (from cryptography->ansible->ansible-lint->-r requirements.txt (line 1)) Downloading https://files.pythonhosted.org/packages/4b/2a/0276479a4b3caeb8a8c1af2f8e4355746a97fab05a372e4a2c6a6b876165/idna-2.7-py2.py3-none-any.whl (58kB) 100% |████████████████████████████████| 61kB 14.4MB/s Collecting ipaddress; python_version < "3" (from cryptography->ansible->ansible-lint->-r requirements.txt (line 1)) Downloading https://files.pythonhosted.org/packages/fc/d0/7fc3a811e011d4b388be48a0e381db8d990042df54aa4ef4599a31d39853/ipaddress-1.0.22-py2.py3-none-any.whl Collecting pycparser (from cffi>=1.7; platform_python_implementation != "PyPy"->cryptography->ansible->ansible-lint->-r requirements.txt (line 1)) Downloading https://files.pythonhosted.org/packages/8c/2d/aad7f16146f4197a11f8e91fb81df177adcc2073d36a17b1491fd09df6ed/pycparser-2.18.tar.gz (245kB) 100% |████████████████████████████████| 256kB 16.2MB/s Building wheels for collected packages: ansible-lint, ansible, pyyaml, MarkupSafe, pycparser Running setup.py bdist_wheel for ansible-lint ... done Stored in directory: /Users/pc-342/Library/Caches/pip/wheels/41/26/0e/3f39ea2147a84ef53beac8e033cdf35aa8ba4b2a47279e9bbd Running setup.py bdist_wheel for ansible ... done Stored in directory: /Users/pc-342/Library/Caches/pip/wheels/3c/99/fc/e9a65e79ddc32c3b956dccb2aa085dc2b7a52fb76112f6fe47 Running setup.py bdist_wheel for pyyaml ... done Stored in directory: /Users/pc-342/Library/Caches/pip/wheels/03/05/65/bdc14f2c6e09e82ae3e0f13d021e1b6b2481437ea2f207df3f Running setup.py bdist_wheel for MarkupSafe ... done Stored in directory: /Users/pc-342/Library/Caches/pip/wheels/33/56/20/ebe49a5c612fffe1c5a632146b16596f9e64676768661e4e46 Running setup.py bdist_wheel for pycparser ... done Stored in directory: /Users/pc-342/Library/Caches/pip/wheels/c0/a1/27/5ba234bd77ea5a290cbf6d675259ec52293193467a12ef1f46 Successfully built ansible-lint ansible pyyaml MarkupSafe pycparser Installing collected packages: MarkupSafe, jinja2, pyyaml, pyasn1, six, pycparser, cffi, bcrypt, enum34, asn1crypto, idna, ipaddress, cryptography, pynacl, paramiko, ansible, ansible-lint Successfully installed MarkupSafe-1.0 ansible-2.5.5 ansible-lint-3.4.23 asn1crypto-0.24.0 bcrypt-3.1.4 cffi-1.11.5 cryptography-2.2.2 enum34-1.1.6 idna-2.7 ipaddress-1.0.22 jinja2-2.10 paramiko-2.4.1 pyasn1-0.4.3 pycparser-2.18 pynacl-1.2.1 pyyaml-3.12 six-1.11.0 |
・ansible-lint
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
$ ansible-lint -L ANSIBLE0002: Trailing whitespace There should not be any trailing whitespace ANSIBLE0004: Git checkouts must contain explicit version All version control checkouts must point to an explicit commit or tag, not just "latest" ANSIBLE0005: Mercurial checkouts must contain explicit revision All version control checkouts must point to an explicit commit or tag, not just "latest" ANSIBLE0006: Using command rather than module Executing a command when there is an Ansible module is generally a bad idea ANSIBLE0007: Using command rather than an argument to e.g. file Executing a command when there is are arguments to modules is generally a bad idea ANSIBLE0008: Deprecated sudo Instead of sudo/sudo_user, use become/become_user. ANSIBLE0009: Octal file permissions must contain leading zero Numeric file permissions without leading zero can behave in unexpected ways. See http://docs.ansible.com/ansible/file_module.html ANSIBLE0010: Package installs should not use latest Package installs should use state=present with or without a version ANSIBLE0011: All tasks should be named All tasks should have a distinct name for readability and for --start-at-task to work ANSIBLE0012: Commands should not change things if nothing needs doing Commands should either read information (and thus set changed_when) or not do something if it has already been done (using creates/removes) or only do it if another check has a particular result (when) ANSIBLE0013: Use shell only when shell functionality is required Shell should only be used when piping, redirecting or chaining commands (and Ansible would be preferred for some of those!) ANSIBLE0014: Environment variables don't work as part of command Environment variables should be passed to shell or command through environment argument ANSIBLE0015: Using bare variables is deprecated Using bare variables is deprecated. Update your playbooks so that the environment value uses the full variable syntax ("{{your_variable}}"). ANSIBLE0016: Tasks that run when changed should likely be handlers If a task has a `when: result.changed` setting, it's effectively acting as a handler ANSIBLE0017: become_user requires become to work as expected become_user without become will not actually change user ANSIBLE0018: Deprecated always_run Instead of always_run, use check_mode. |
今回空白だけ検知したいので、
ANSIBLE0002: Trailing whitespace
There should not be any trailing whitespace
(訳)
ANSIBLE0002:末尾の空白末尾に空白があってはいけません
上記以外はskipさせるように書いています。
そのままansible-lintコマンドで確認してみると…
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
$ ansible-lint -c .circleci/.ansible-lint blog.adachin.me.yml Examining blog.adachin.me.yml of type playbook Examining /Users/adachin/git/RVIRUS0817/ansible_blog.adachin.me/roles/common/tasks/main.yml of type tasks Examining /Users/adachin/git/RVIRUS0817/ansible_blog.adachin.me/roles/mackerel-agent/tasks/main.yml of type tasks Examining /Users/adachin/git/RVIRUS0817/ansible_blog.adachin.me/roles/mackerel-agent/handlers/main.yml of type handlers Examining /Users/adachin/git/RVIRUS0817/ansible_blog.adachin.me/roles/mysql/tasks/main.yml of type tasks Examining /Users/adachin/git/RVIRUS0817/ansible_blog.adachin.me/roles/mysql/handlers/main.yml of type handlers Examining /Users/adachin/git/RVIRUS0817/ansible_blog.adachin.me/roles/h2o/tasks/main.yml of type tasks Examining /Users/adachin/git/RVIRUS0817/ansible_blog.adachin.me/roles/h2o/handlers/main.yml of type handlers Examining /Users/adachin/git/RVIRUS0817/ansible_blog.adachin.me/roles/php7/tasks/main.yml of type tasks Examining /Users/adachin/git/RVIRUS0817/ansible_blog.adachin.me/roles/php7/handlers/main.yml of type handlers Examining /Users/adachin/git/RVIRUS0817/ansible_blog.adachin.me/roles/cron/tasks/main.yml of type tasks Examining /Users/adachin/git/RVIRUS0817/ansible_blog.adachin.me/roles/sysctl/tasks/main.yml of type tasks Examining /Users/adachin/git/RVIRUS0817/ansible_blog.adachin.me/roles/logrotate/tasks/main.yml of type tasks Examining /Users/adachin/git/RVIRUS0817/ansible_blog.adachin.me/roles/fail2ban/tasks/main.yml of type tasks Examining /Users/adachin/git/RVIRUS0817/ansible_blog.adachin.me/roles/fail2ban/handlers/main.yml of type handlers Examining /Users/adachin/git/RVIRUS0817/ansible_blog.adachin.me/roles/letsencrypt/tasks/main.yml of type tasks |
とりあえず空白はないですな!!ここで、
空白を入れてgit pushしてCircleCi検知してみます。
■Go!!!!!! CircleCI
Start building押すと!!!
ムムム…
キタ━━━━(゚∀゚)━━━━!!
failedしてるうううううううう!!!
■まとめ
これぞインフラCI!!ansibleコード規約みたいなものを社内で作るといいですね。
ansible-lintいろいろ試して触ってみます!
オリジナルのも作ってギッハブで公開すると激アツ!
空白チェックはyaml-lintを使えばいいみたい?
Syntax errorも検知してくれるのでいい!!
参考
https://github.com/willthames/ansible-lint
0件のコメント