こんにちは、lisです!
Ansibleという設定やアプリインストールを自動化できるものを知ったので試してみました。
Ansibleとは?
すごくざっくりですが、こんなものです。
- サーバやネットワーク機器の設定やアプリのインストールを自動化する
- yaml形式の設定ファイルを使用する
- 他にも自動設定ツールはあるけど、導入/設定が容易らしい
何やらAnsibleは、ControlNode→TargetNodeという指揮系統が必要そう……?
と思ったら、localhostに対してでも制御可能みたいでした。
これを試していきます。
Ansibleインストール
pipからAnsibleをインストールします。
aptでもできるようですが、+α操作が必要なのでpipの方が楽そうです。
$ python3 -m pip install --user ansible
ansibleコマンドを試すと実行できました。
$ ansible --version ansible [core 2.12.5] config file = None ...
イベントリーファイルの作成
Ansibleで制御するホスト一覧を記述するファイルを作成します。
$ sudo mkdir /etc/ansible $ sudo vi /etc/ansible/hosts # localhost ansible_connection=local $ cat /etc/ansible/hosts localhost ansible_connection=local
動作確認。
$ ansible localhost -m ping localhost | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python3" }, "changed": false, "ping": "pong" }
成功していますね。
playbookの作成
Ansibleはplaybookというものに即して、設定等を自動実行してくれるようです。
playbookはYAML形式で記述します。
試しにtmuxをインストールするplaybookを作成してみました。
$ cat playbook.yml - name: hoge hosts: localhost connection: local become: yes tasks: - name: install tmux apt: name: tmux state: latest
playbookの実行
ansible-playbook {playbook-file}
で、playbookを実行してくれます。
$ ansible-playbook playbook.yml PLAY [hoge] ************************************************************************************************************************************************** TASK [Gathering Facts] *************************************************************************************************************************************** ok: [localhost] TASK [install tmux] ****************************************************************************************************************************************** changed: [localhost] PLAY RECAP *************************************************************************************************************************************************** localhost : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
記述していたタスクが成功しました。
ansible-lint
ansible-lintを使うと、lintチェックができるようなので試してみました。
$ pip install ansible-lint
実行してみる。
$ ansible-lint playbook.yml WARNING: PATH altered to include /usr/bin WARNING Listing 3 violation(s) that are fatal yaml: truthy value should be one of [false, true] (truthy) [playbook.yml:4:embed:cite] fqcn-builtins: Use FQCN for builtin actions. playbook.yml:6 Task/Handler: install tmux package-latest: Package installs should not use latest. playbook.yml:6 Task/Handler: install tmux 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 3 failure(s), 0 warning(s) on 1 files.
色々指摘されました。
yaml: truthy value should be one of [false, true] (truthy)
yaml: truthy value should be one of [false, true] (truthy) [playbook.yml:4:embed:cite]
yaml:真の値は[false、true](truthy)のいずれかである必要があります
- name: hoge hosts: localhost connection: local - become: yes + become: true tasks: - name: install tmux apt:
yes/noでなく、true/falseにする必要があるようです。
fqcn-builtins: Use FQCN for builtin actions.
fqcn-builtins: Use FQCN for builtin actions. playbook.yml:6 Task/Handler: install tmux
fqcn-builtins:組み込みアクションにFQCNを使用します。
なにやら完全修飾コレクション名(FQCN)の仕様を推奨されるようなのですが、解決するのが難しかったのでとりあえず諦めました。
package-latest: Package installs should not use latest.
package-latest: Package installs should not use latest. playbook.yml:6 Task/Handler: install tmux
package-latest:パッケージのインストールではlatestを使用しないでください。
tasks: - name: install tmux apt: name: tmux - state: latest + state: present
latestでなくpresentやバージョン指定をした方がよいようで。
lint修正後
とりあえずこんな感じになりました。
- name: hoge hosts: localhost connection: local become: true tasks: - name: install tmux apt: name: tmux state: present
$ ansible-lint playbook.yml WARNING: PATH altered to include /usr/bin WARNING Listing 1 violation(s) that are fatal fqcn-builtins: Use FQCN for builtin actions. playbook.yml:6 Task/Handler: install tmux 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. Finished with 1 failure(s), 0 warning(s) on 1 files.
この1件のlintエラーは諦めます。
参考
コチラを参考にしました。ありがとうございました!
あとがき
よくよく考えたらSSHだけ設定して、WSLとかからSSH経由で実行……なんて方法でもよかったのかなーと思いました。
ちゃんと作れば、再インストールや新しい機材をセットアップするときに楽だろうなーと感じました。