BREAKING NEWS

2019/06/02

Raspberry PiのプロビジョニングをAnsibleで行う

Raspberry PiのプロビジョニングをAnsibleで行う

この記事のまとめ:
  • Ansibleを使ってRaspberry Piのプロビジョニングのやり方を紹介しています。
背景

前回の記事に引き続いて家の中の温湿度を可視化するためにRaspberry PiとBluetooth対応の温湿度センサーを使ってシステムを構築しようと思います。

また、ちょうどAnsibleの使い方を勉強しておく必要があったので、ちょうど手に入ったRaspberry Piを使って勉強がてらにプロビジョニングやプログラムのデプロイをやってみようと思います。今回はプロビジョニングのみです。

今持っているRaspberry Piは2台だけですが今後増えた時や、Raspberry Piは再設定し直す可能性の高いデバイスなのでAnsibleなどでプロビジョニングできるようにしておくと便利です。

Ansibleでプロビジョニングしたいこと

私が必要とする最低限のプロビジョニングの項目は次の通りです。

  1. Linuxの基本設定
    1. NTP (タイムゾーン)設定
  2. dockerのインストール
前提条件

OSとしてRaspbianを使います。 また、Raspberry PiにSSHでログインでき、固定IPアドレスが割り振られている前提とします。

ここまでのやり方は下記の記事で書いてあるので必要であればご覧ください。

Ansibleの環境設定
コントローラーノードの設定

ターゲットノードとなるRaspberry PiのRaspbianにはPythonがデフォルトで入っているので特に設定不要ですが最小構成でOSをインストールしている場合などPythonのインストールが必要になる可能性がありますので必要に応じてインストールしてください。

それではコントローラーノードの設定を見ていきます。 今回コントローラーノードとしてUbuntu 16.04を使用しています。なお、すべて ansible ユーザーでの実行を想定しています。

  • Ansibleのインストール

今回は apt コマンドでAnsibleをインストールします。ほかにも pip コマンドでインストールする方法もあります。

$ sudo apt-add-repository -y ppa:ansible/ansible
$ sudo apt update
$ sudo apt install ansible
$ sudo apt install python-dev
$ ansible --version
ansible 2.8.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/home/ansible/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/dist-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.12 (default, Nov 12 2018, 14:36:49) [GCC 5.4.0 20160609]
  • ansible.cfgの設定

作業ディレクトリにAnsibleの実行に関する基本的な設定ファイルを作ります。

$ cat ansible.cfg
[defaults]
forks = 4  # ターゲットノードで処理を行うプロセス数
host_key_checking = False  # ターゲットノードにSSH接続する際にフィンガープリントチェックをしない
gathering = smart  # ターゲットノードの詳細情報取得は新規接続時のみ
gather_subset = all  # ターゲットノードから取得する詳細情報はすべて
transport = smart  # OpenSSHが使えるときは使い、使えないときはPython paramikoを使う
  • 接続テスト

インベントリーファイルを作成し、pingモジュールを実行してみます。なお、Raspberry Piが複数台ある場合にはここに対象デバイスをすべて書き込めばすべてのデバイスに対して実行できます。

$ cat inventory/inventory.ini
[raspberry_pi]
192.168.0.31 ansible_user=pi ansible_password=raspberry
 
$ ansible -i inventory/inventory.ini raspberripi -m ping
 [WARNING]: Platform linux on host pi@192.168.0.31 is using the discovered Python interpreter at /usr/bin/python, but future installation of another Python interpreter
could change this. See https://docs.ansible.com/ansible/2.8/reference_appendices/interpreter_discovery.html for more information.
 
pi@192.168.0.31 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}

警告が出ておりますが、これはRaspberry PiでPythonの実行ファイルが想定したディレクトリになく、Ansible 2.8で実装されたPython interpreterによってPythonの実行ファイルを特定しましたよ、ってやつらしいですがデフォルトで探しに行く場所は /usr/bin/python なのでは…?ちゃんとそこにあるけど…。

警告に関しては正直ちょっと何が起こっているかわかりませんが、とりあえずこれで準備が終わりました。

Ansibleの実行

今回Ansibleでプロビジョニングする内容として、NTPの設定とDockerのインストールを行います。

こういった共通的な設定については自分でAnsibleのコードを書くのも手間なので、基本的にはAnsible Galaxyで共有されているロールをダウンロードしてきて使おうと思います。

最終的なディレクトリー構成としては次のようになります。

$ tree
.
|-- ansible.cfg
|-- inventory
|   `-- inventory.ini
|-- raspbian_provisioning.yml
`-- roles
    `-- common
        |-- geerlingguy.docker_arm
        `-- geerlingguy.ntp

ansible.cfginventory.ini はすでに作りましたので、残りについて見ていきます。

まずは各種ロールとして、NTP ([geerlingguy.ntp](https://galaxy.ansible.com/geerlingguy/ntp)) とDocker ([geerlingguy.docker_arm](https://galaxy.ansible.com/geerlingguy/docker_arm)) の紹介をして最後にプレイブック (raspbian_provisioning.yml) の内容を紹介します。

AnsibleでNTPの設定

NTPの設定には geerlingguy.ntp を使わせてもらいます。

$ ansible-galaxy install -p roles/common/ geerlingguy.ntp

こちらも./roles/common/geerlingguy.ntp/defaults/main.yml のパラメーターをいくつか変更します。

タイムゾーンを東京にします。

ntp_timezone: Asia/Tokyo

Ansibleに /etc/ntp.conf の変更を許可します。

ntp_manage_config: True

NTPのエリアを指定します。

ntp_area: 'asia'

より正確な時間が欲しい場合は、ntp_servers にNICT (ntp.nict.jp) など遅延の短いNTPサーバーのURLを追加するとよいかもしれません。

AnsibleでDocker設定

Dockerの設定には geerlingguy.docker_arm を使わせてもらいます。

$ ansible-galaxy install -p roles/common/ geerlingguy.docker_arm

RaspBerry Pi ZeroにDockerをインストールする場合、Dockerバージョンによってはcore dumpによってインストールできないことがありましたので、roles/common/geerlingguy.docker_arm/defaults/main.ymlファイルでDockerのバージョンを指定します。

docker_version: 18.06.1~ce~3-0~raspbian

また、pipでdocker-composeをインストールするオプションがありますが、私の環境ではインストールできませんでしたので、この設定はfalseにしておきます。

docker_install_compose: false
Ansibleプレイブック

今回は実行するものが少ないので非常にシンプルです。

$ cat raspbian_provisioning.yml
- name: Provisioning Raspberry Pi
  hosts: raspberry_pi
  become: true
  roles:
    - name: common/geerlingguy.ntp
      tags: ntp
    - name: common/geerlingguy.docker_arm
      tags: docker

プレイブックの実行は次の通りです。

$ ansible-playbook  -i inventory/inventory.ini raspbian_provisioning.yml
最後に

上記の設定ファイルをGitHubに置いておきました。


今回は以上です。 最後まで読んでいただき、ありがとうございます。


その他

いくつか試行錯誤する中で、今回私が必要とする環境には不要だったものの設定してしまったものがいくつかあるのでついでなので紹介します。

SSH公開鍵認証の登録

パスワードをインベントリーに書き込むことがセキュリティ上問題がある場合には公開鍵認証でも対応できます。 ※公開鍵認証の設定したからといってパスワード認証ができなくなるわけではないです。

# 公開鍵と秘密鍵の作成
$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/ansible/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/ansible/.ssh/id_rsa.
Your public key has been saved in /home/ansible/.ssh/id_rsa.pub.
 
 
# 作成した公開鍵をコントローラーノードの~/.ssh/authorized_keysに追加
$ ssh-copy-id -o StrictHostKeyChecking=no -i ~/.ssh/id_rsa.pub localhost
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/ansible/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
pi@192.168.0.31's password:
 
Number of key(s) added: 1
 
Now try logging into the machine, with:   "ssh -o 'StrictHostKeyChecking=no' 'localhost'"
and check to make sure that only the key(s) you wanted were added.
 
 
# 作成した公開鍵をRaspberry Piの~/.ssh/authorized_keysに転送
$ ssh-copy-id -o StrictHostKeyChecking=no -i ~/.ssh/id_rsa.pub pi@192.168.0.31
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/ansible/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
pi@192.168.0.31's password:
 
Number of key(s) added: 1
 
Now try logging into the machine, with:   "ssh -o 'StrictHostKeyChecking=no' 'pi@192.168.0.31'"
and check to make sure that only the key(s) you wanted were added.
locale設定

Raspberry Piの中で日本語を使う場合、locale設定を ja_JP.UTF-8 に変更します。

locale設定には robertdebock.locale を使わせてもらおうと思います。

$ ansible-galaxy install -p roles/common/ robertdebock.locale

デフォルトでは en_US.UTF-8 に設定するようになっているので ja_JP.UTF-8 に設定するように変更します。変更するファイルは ./roles/common/robertdebock.locale/defaults/main.yml です。

その中から、下記の項目を変更します。

locale_lang: ja_JP.UTF-8
関連記事



Share this:

 
Copyright © 2014 hassiweb. Designed by OddThemes